Bean生命周期及BeanFactory

1、spring通过BeanFactory灵活配置、管理bean,Spring对管理的bean没有任何特别的要求,完全支持对POJO的管理;

2、BeanFactory有个ApplicationContext子接口,该接口比BeanFactory增强了更多的功能,该接口的实例有时被称为Spring的上下文,在通常的J2EE应用中,都应该考虑ApplicationContext接口,只有在J2EE应用非常关注内存的情况下,才考虑使用BeanFactory

3、Spring以bean方式管理组件,J2EE的全部组件都被当做bean来管理,Spring中的组件可以是Java对象或者Java组件;

4、在Spring整个应用中,各层的对象都处在Spring的管理下,这些对象以Bean的方式存在,Spring负责创建bean,并且管理其生命周期,bean在Spring容器中运行,无需感受Spring容器的存在,一样可以运行,接受Spring的依赖注入,包括bean的属性注入,合作者注入,依赖关系注入等

  注:Spring上下文(ApplicationContext)是产生bean的工厂,bean是工厂产生的实例,产生bean的实例时,需要知道bean的实现类,bean实例的使用者(即调用程序)面向的是接口,无需关心bean实例的实现类,Spring工厂(ApplicationContext)负责维护bean实例的实例化,使用者无需关心实例化。


 BeanFactory接口:

  BeanFactroy是一个Spring容器,用于创建,配置,管理bean,bean之间的依赖关系也有BeanFactory负责维护;

  BeanFactory对bean的实例化过程,无须bean的调用者关心,调用者只需要通过getBean()方法获得指定bean的引用;

 创建BeanFactory的实例时,必须提供bean的详细配置信息,bean的详细信息通过XML文件描述,创建BeanFactory实例时,需要提供XML文件作为参数


 Bean生命周期:

下图描述了 Bean 的生命周期。它是由 IoC 容器控制。IoC 容器定义 Bean 操作的规则,即 Bean 的定义(BeanDefinition)。Bean 的定义包含了 BeanFactory 在创建 Bean 实例时需要的所有信息。BeanFactory 首先通过构造函数创建一个 Bean 实例,之后它会执行 Bean 实例的一系列之前初始化动作,初始化结束 Bean 将进入准备就绪(ready)状态,这时应用程序就可以获取这些 Bean 实例了。最后,当你销毁单例(Singleton)Bean 时,它会调用相应的销毁方法,结束 Bean 实例的生命周期。

原理图:

详细步骤:

1、实例化(当程序加载applicationContext.xml文件),把bean(前提是scope=singleton)实例化到内存。

2、调用该Bean的setXxx()方法,设置属性。

3、如果实现了bean名字关注接口(BeanNameAware),则可以通过setBeanName获取id号。

4、如果实现了bean工厂关注接口(BeanFactoryAware),则可以获取BeanFactory。

5、如果实现了ApplicationContextAware接口,则可以获得ApplicationContext。

6、如果bean和一个后置处理器关联,则会自动去调用postProcessBeforeInitialization()方法。

7、如果实现InitializingBean接口,则会调用afterPropertiesSet方法。

8、如果配置<bean init-method=”init” /> 则可以在bean自定义初始化方法。

9、如果bean和一个后置处理器关联,则会自动去调用postProcessAfterInitialization()方法。

10、使用bean。

11、容器关闭。

12、可以通过实现DisposableBean接口来调用destory()方法。

13、可以在<bean destory-method=”fun1”/> 调用自定义的销毁方法。

 


 

Spring配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="utf-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation=" 
            http://www.springframework.org/schema/beans  
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
            http://www.springframework.org/schema/aop  
            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
            http://www.springframework.org/schema/tx  
            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 
    <!-- 在容器文件中配置bean(service/dao/domain/action/数据源) --> 
    <bean id="personService" init-method="init" destroy-method="myDestory" class="com.pc.beanlife.PersonService"
        <!-- 这里注入属性,前提是有setName才能成功 --> 
        <property name="name" value="zs" /> 
    </bean> 
    <!-- 配置后置处理器(类似filter) --> 
    <bean id="myBeanPostProcesseor" class="com.pc.beanlife.MyBeanPostProcesseor" /> 
</beans> 

Bean类文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
public class PersonService implements BeanNameAware,BeanFactoryAware,ApplicationContextAware,InitializingBean,DisposableBean{ 
    private String name; 
       
    public PersonService() { 
        System.out.println("无参构造方法被调用"); 
    
       
    public PersonService(String name) { 
        System.out.println("有参构造方法被调用"); 
    
       
    public void printOk(){ 
        System.out.println(name + " is Ok"); 
    
   
    public String getName() { 
        return name; 
    
   
    public void setName(String name) { 
        System.out.println("调用set方法设置属性"); 
        this.name = name; 
    
   
    // 该方法可以给name表示正被实例化的Bean的id 
    @Override 
    public void setBeanName(String name) { 
        // TODO Auto-generated method stub 
        System.out.println("setBeanName 被调用,值是" + name); 
    
   
    // 该方法可以传递beanFactory 
    @Override 
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException { 
        // TODO Auto-generated method stub 
        System.out.println("setBeanFactory " + beanFactory); 
           
    
   
    // 该方法可以设置上下文 
    @Override 
    public void setApplicationContext(ApplicationContext applicationContext) 
            throws BeansException { 
        // TODO Auto-generated method stub 
        System.out.println("setApplicationContext " + applicationContext); 
    
   
    // 该方法可以在InitializingBean使用 
    @Override 
    public void afterPropertiesSet() throws Exception { 
        // TODO Auto-generated method stub 
        System.out.println("afterPropertiesSet"); 
    
       
    // 自定义的初始化方法 
    public void init() { 
        System.out.println("调用自定义的初始化方法"); 
    
   
    // 可以在这关闭数据连接,socket,文件流,释放资源等等。。。 
    @Override 
    public void destroy() throws Exception { 
        // TODO Auto-generated method stub 
        System.out.println("调用destroy()"); 
    
       
    // 自定义我们的销毁方法 
    public void myDestory() { 
        System.out.println("调用自定义的销毁方法"); 
    

 后置处理器文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class MyBeanPostProcesseor implements BeanPostProcessor { 
    // 后置处理之后的过滤 
    @Override 
    public Object postProcessBeforeInitialization(Object bean, String beanName) 
            throws BeansException { 
        // TODO Auto-generated method stub 
        System.out.println("postProcessBeforeInitialization方法被调用"); 
        return bean; 
    
   
    // 后置处理之前的过滤 
    @Override 
    public Object postProcessAfterInitialization(Object bean, String beanName) 
            throws BeansException { 
        // TODO Auto-generated method stub 
        System.out.println("postProcessAfterInitialization方法被调用"); 
        return bean; 
    

 测试类: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Test { 
    public static void main(String[] args) { 
        // TODO Auto-generated method stub 
        // 通过ApplicationContext加载Bean 
        System.out.println("------------在ApplicationContext中Bean的生命周期------------"); 
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/pc/beanlife/beans.xml"); 
        // 获取Bean 
        PersonService personService = (PersonService) applicationContext.getBean("personService"); 
        personService.printOk(); 
           
        // 通过BeanFactory加载Bean 
        System.out.println("------------在BeanFactory中Bean的生命周期------------"); 
        BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("com/pc/beanlife/beans.xml")); 
        PersonService personService2 = (PersonService) beanFactory.getBean("personService"); 
        personService2.printOk(); 
    

日志文件: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
------------在ApplicationContext中Bean的生命周期------------ 
无参构造方法被调用 
调用set方法设置属性 
setBeanName 被调用,值是personService 
setBeanFactory org.springframework.beans.factory.support.DefaultListableBeanFactory@1f8b81e3: defining beans [personService,myBeanPostProcesseor]; root of factory hierarchy 
setApplicationContext org.springframework.context.support.ClassPathXmlApplicationContext@771c8a71: display name [org.springframework.context.support.ClassPathXmlApplicationContext@771c8a71]; startup date [Sat Feb 20 11:34:05 CST 2016]; root of context hierarchy 
postProcessBeforeInitialization方法被调用 
afterPropertiesSet 
调用自定义的初始化方法 
postProcessAfterInitialization方法被调用 
zs is Ok 
------------在BeanFactory中Bean的生命周期------------ 
无参构造方法被调用 
调用set方法设置属性 
setBeanName 被调用,值是personService 
setBeanFactory org.springframework.beans.factory.xml.XmlBeanFactory@6c4fc156: defining beans [personService,myBeanPostProcesseor]; root of factory hierarchy 
afterPropertiesSet 
调用自定义的初始化方法 
posted @ 2016-10-24 17:35  美味的你  阅读(166)  评论(0编辑  收藏  举报