Spring 的核心容器

Spring 的核心容器实现了IOC,其目的是提供一种无侵入式框架。

BeanFactory和ApplicationContext是了解Spring核心的关键。

org.springframework.beans和org.springframework.context这两个包是Spring最基本、最重要的包,为了实现一种无侵入式的框架,代码中大量引用java中的反射机制,通过动态调用的方式避免了硬编码,为Spring的反向控制特性提供了基础。在这两个包中,最重要的类是BeanFactory:提供一种先进的配置机制来管理任何种类的Bean。ApplicationContext:建立在BeanFactory的基础上,并增加了其他的功能。例如对于国际化的支持、获取资源、事件传递等。

<?xml version="1.0" encoding="UTF-8"?>

在Spring中,Bean的属性值有两种注入方式:基于setter的依赖注入和基于构造函数的依赖注入。

Bean 的初始化可以依赖depends-on属性来执行。

Bean的生命周期:

1、bean的定义

2、bean的初始化

/**
     * Bean的初始化
     * 第一种方式:采用init-method属性来初始化Bean
     */
    public void beanInit(){
        this.msg = "我是通过init-method属性来初始化Bean的哦";
    }

    /**
     * Bean的初始化
     * 第二种方式:实现org.springframework.beans.factory.InitializingBean接口
     * 实现afterPropertiesSet方法来完成初始化bean
     */
    public void afterPropertiesSet() throws Exception {
        this.msg = "我是通过实现org.springframework.beans.factory.InitializingBean接口来初始化Bean哦";
    }
<!-- 采用init-method方法初始化bean的配置 -->
    <!-- 采用destroy-method方法注销bean的配置 -->
    <bean id="initbean" class="spring.init.bean.InitBean" init-method="beanInit" destroy-method="cleanup">

3、bean的使用

 1 /**
 2          * bean 的使用
 3          */
 4         System.out.println("=================BeanWrapper方式使用bean====================");
 5         InitBean ibean = new InitBean();
 6         BeanWrapper bw = new BeanWrapperImpl(ibean);
 7         bw.setPropertyValue("msg","BeanWrapper方式使用bean");
 8         ibean.printBeanInfo();
 9         
10         System.out.println("==================BeanFactory方式使用bean===========================");
11         ClassPathResource  is = new ClassPathResource("initbean.xml");
12         XmlBeanFactory factory = new XmlBeanFactory(is);
13         InitBean bfib = (InitBean) factory.getBean("initbean2");
14         bfib.printBeanInfo();
15         
16         System.out.println("=================ApplicationContext方式使用Bean================================");
17         ApplicationContext acbean = new FileSystemXmlApplicationContext("conf/xml/initbean.xml");
18         InitBean acbeanid = (InitBean) acbean.getBean("initbean");
19         acbeanid.printBeanInfo();
View Code
 1 <?xml version="1.0" encoding="UTF-8"?><!-- 首先定义为XML的方式来存储Bean的配置-->
 2 <!--声明使用的是http://www.springframework.org/dtd/spring-beans-2.0.dtd-->
 3 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" >
 4 <!--配置bean的开始,根节点beans-->
 5 <beans>
 6 
 7     <!-- 采用init-method方法初始化bean的配置 -->
 8     <bean id="initbean" class="spring.init.bean.InitBean" init-method="beanInit">
 9         <property name="msg">
10             <value>bean的使用</value>
11         </property>
12     </bean>
13     <!-- org.springframework.beans.factory.InitializingBean接口初始化bean的配置 -->
14     <bean id="initbean2" class="spring.init.bean.InitBean">
15     </bean>
16     
17     
18     
19     
20 </beans>
View Code

4、bean的销毁

 1 /**
 2          * bean的注销
 3          */
 4         //通过Bean的Id来获取Bean。从而完成了JavaBean与Xml之间的关系建立
 5         //第一种方式采用destroy-method属性进行注销
 6         InitBean ib3 = (InitBean) ac.getBean("initbean");
 7         ib3.cleanup();
 8         ib3.printBeanInfo();
 9         //实现DisposableBean接口的destroy()注销bean"
10         InitBean ib4 = (InitBean) ac.getBean("initbean2");
11         try {
12             ib4.destroy();
13         } catch (Exception e) {
14             // TODO Auto-generated catch block
15             e.printStackTrace();
16         }
17         ib4.printBeanInfo();
View Code
    <!-- 采用init-method方法初始化bean的配置 -->
    <!-- 采用destroy-method方法注销bean的配置 -->
    <bean id="initbean" class="spring.init.bean.InitBean" init-method="beanInit" destroy-method="cleanup">
        <property name="msg">
            <value>bean的使用</value>
        </property>
    </bean>
    <!-- org.springframework.beans.factory.InitializingBean接口初始化bean的配置 -->
    <bean id="initbean2" class="spring.init.bean.InitBean">
    </bean>
    

Bean<!-- ref的属性指定依赖的3种模式 -->

bean、local、parent

Bean通过autowire属性来自动装配,自动装配有的5种模式

不建议使用自动装配模式

bean 通过属性来依赖检查

一般依赖检查和自动装配是结合在一起用的

集合的注入方式

 1 <bean id="initbean" class="spring.init.bean.InitBean" init-method="beanInit" destroy-method="cleanup">
 2         <property name="msg">
 3             <value>bean的使用</value>
 4         </property>
 5         <property name="date">
 6         <!-- ref的属性指定依赖的3种模式 -->
 7             <!-- ref bean="date"/-->
 8             <ref local="date"/>
 9 <!--             <ref parent="date"/> -->
10         </property>
11         
12         <!-- 集合形式的依赖注入 -->
13         <!-- list集合 -->
14         <property name="msgList">
15             <list>
16                 <value>list1</value>
17                 <value>list2</value>
18                 <value>list3</value>
19                 <value>list4</value>
20             </list>
21         </property>
22         <!-- Set集合 -->
23         <property name="msgSet">
24             <set>
25                 <value>set1</value>
26                 <value>set2</value>
27                 <value>set3</value>
28                 <value>set4</value>
29             </set>
30         </property>
31         
32         <!-- Map集合 -->
33         <property name="msgMap">
34             <map>
35                 <entry key="MAP1">
36                     <value>MAPVAL1</value>
37                 </entry>
38             </map>
39         </property>
40         
41         <!-- Properties类型 -->
42         <property name="mapProperties">
43             <props>
44                 <prop key="gh">123</prop>
45             </props>
46         </property>
47     </bean>
View Code

管理Bean,。。。的内容参照Bean的使用,常用的有BeanFactory和ApplicationContext

ApplicationContext支持国际化

1 <!-- 负责国际化的支持 的bean-->
2     <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
3         <property name="basename">
4         <!-- 属性文件的文件名称 -->
5             <value>message</value>
6         </property>
7     </bean>
View Code
/**
         * 国际化支持
         * 获取属性文件的值
         */
        ApplicationContext proAc = new FileSystemXmlApplicationContext("conf/xml/initbean.xml");
        //设定当前时间
        Object[] timeObj = new Object[]{"HelloWord",new Date()};
        
        //国际化支持
        String msg = proAc.getMessage("HelloWord", timeObj, Locale.CHINA);
        System.out.println(msg);

ApplicationContext对资源的访问

/**
         * 资源访问
         */
        
        //第一种通过虚拟路径来取
        ApplicationContext xnljAc = new FileSystemXmlApplicationContext("conf/xml/initbean.xml");
        Resource r = xnljAc.getResource("classpath:message.properties");
        //通过实际路径
        ApplicationContext xnljAc2 = new FileSystemXmlApplicationContext("conf/xml/initbean.xml");
        Resource r2 = xnljAc2.getResource("file:D:/MyEcliceWorkPlace/springInit/conf/properties/message.properties");
        //通过相对路径
        ApplicationContext xnljAc3 = new FileSystemXmlApplicationContext("conf/xml/initbean.xml");
        Resource r3 = xnljAc2.getResource("springInit/conf/properties/message.properties");
        //通过getFile()取得文件内容
        
此笔记用来自我学习和分享知识,有不对的地方还请大家互相指教
原文地址:https://www.cnblogs.com/willbesuccess/p/3454454.html