Spring 框架核心概念IoC 随笔

栏目: Java · 发布时间: 6年前

内容简介:1/2 高层模块不应该依赖低层模块,而是依赖抽象,低层模块也应该依赖抽象。 比如 controller 依赖 IXXXService ,而不是依赖 XXXServiceImpl。Spring可以加载来自ApplicationContext是应用程序级别,BeanFactory是系统级别。 使用 AppCtx 不同实现,比如 ClassPathXmlApplicationContext

1/2 高层模块不应该依赖低层模块,而是依赖抽象,低层模块也应该依赖抽象。 比如 controller 依赖 IXXXService ,而不是依赖 XXXServiceImpl。

DI注入方式

  • 1 Setter注入
  • 2 构造注入
  • 3 接口注入 spring没有实现 avalon有实现
  • go cloud —— wired 可能是第4种

POJO 注册成 Bean

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>
复制代码

Setter注入

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>
复制代码

Bean跟Bean之间依赖

<?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可以加载来自

  • classpath
  • file sys
  • ftp
  • http
  • ...任何外部的配置文件

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>
复制代码

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

计算复杂性

计算复杂性

阿罗拉 巴拉克 / 骆吉洲 / 机械工业出版社 / 2016-1-1 / 129元

《计算复杂性的现代方法》是一部将所有有关复杂度知识理论集于一体的教程。将最新进展和经典结果结合起来,是一部很难得的研究生入门级教程。既是相关科研人员的一部很好的参考书,也是自学人员很难得的一本很好自学教程。本书一开始引入该领域的最基本知识,然后逐步深入,介绍更多深层次的结果,每章末都附有练习。对复杂度感兴趣的人士,物理学家,数学家以及科研人员这本书都是相当受益。一起来看看 《计算复杂性》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

html转js在线工具
html转js在线工具

html转js在线工具