1/2 高层模块不应该依赖低层模块,而是依赖抽象,低层模块也应该依赖抽象。 比如 controller 依赖 IXXXService ,而不是依赖 XXXServiceImpl。
public class DefaultMessage { private String message = "Spring is fun."; /** * Gets message. */ public String getMessage() { return message; } /** * Sets message. */ public void setMessage(String message) { this.message = message; } } 复制代码
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="message" class="org.springbyexample.di.xml.DefaultMessage" /> </beans> 复制代码
public class ConstructorMessage { private String message = null; /** * Constructor */ public ConstructorMessage(String message) { this.message = message; } /** * Gets message. */ public String getMessage() { return message; } /** * Sets message. */ public void setMessage(String message) { this.message = message; } } 复制代码
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="message" class="org.springbyexample.di.xml.ConstructorMessage"> <constructor-arg value="Spring is fun." /> </bean> </beans> 复制代码
public class SetterMessage { private String message = null; /** * Gets message. */ public String getMessage() { return message; } /** * Sets message. */ public void setMessage(String message) { this.message = message; } } 复制代码
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="message" class="org.springbyexample.di.xml.SetterMessage"> <property name="message" value="Spring is fun." /> </bean> </beans> 复制代码
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="springMessage" class="java.lang.String"> <constructor-arg value="Spring is fun." /> </bean> <bean id="message" class="org.springbyexample.di.xml.SetterMessage"> <property name="message" ref="springMessage" /> </bean> </beans> 复制代码
Spring可以加载来自
ApplicationContext是应用程序级别,BeanFactory是系统级别。 使用 AppCtx 不同实现,比如 ClassPathXmlApplicationContext
public class MessageRunner { final static Logger logger = LoggerFactory.getLogger(MessageRunner.class); /** * Main method. */ public static void main(String[] args) { logger.info("Initializing Spring context."); ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/application-context.xml"); logger.info("Spring context initialized."); Message message = (Message) applicationContext.getBean("message"); logger.debug("message='" + message.getMessage() + "'"); } } 复制代码
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="message" class="org.springbyexample.di.app.Message"> <property name="message" value="Spring is fun." /> </bean> </beans> 复制代码
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class SetterMessageTest { final Logger logger = LoggerFactory.getLogger(SetterMessageTest.class); @Autowired private SetterMessage message = null; /** * Tests message. */ @Test public void testMessage() { assertNotNull("Constructor message instance is null.", message); String msg = message.getMessage(); assertNotNull("Message is null.", msg); String expectedMessage = "Spring is fun."; assertEquals("Message should be '" + expectedMessage + "'.", expectedMessage, msg); logger.info("message='{}'", msg); } } 复制代码
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="message" class="org.springbyexample.di.xml.SetterMessage"> <property name="message" value="Spring is fun." /> </bean> </beans> 复制代码