【问题标题】:Confused!! Bean's lifecycle method chain使困惑!​​! Bean的生命周期方法链
【发布时间】:2011-11-05 13:12:35
【问题描述】:
public class Life implements BeanNameAware, BeanFactoryAware,  
    ApplicationContextAware, BeanPostProcessor, InitializingBean,  
    DisposableBean {  
private int counter; // counter  

public int getCounter() {  
    return counter;  
}  

public void setCounter(int counter) {  
    this.counter = counter;  
    System.out.println("1. Spring setter DI:" + this.counter);  
}  

public Life() {  
    System.out.println("0. Spring calls constructor");  
}  

@Override  
public void destroy() throws Exception {  
    System.out.println("8. DisposableBean#destroy:" + ++counter);  
}  

public void _destroy() throws Exception {  
    System.out.println("8'. bean#_destroy:" + ++counter);  
}  

@Override  
public void afterPropertiesSet() throws Exception {  
    System.out.println(new Date().getTime());  
    System.out.println("6. InitializingBean#afterPropertiesSet:"  
            + ++counter);  
}  

public void init() throws Exception {  
    System.out.println("6'. bean#init:" + ++counter);  
}  

@Override  
public Object postProcessBeforeInitialization(Object bean, String beanName)  
        throws BeansException {  
    System.out.println(new Date().getTime());  
    System.out  
            .println("5. BeanPostProcessor#postProcessBeforeInitialization:"  
                    + ++counter);  
    return bean;  
}  

@Override  
public Object postProcessAfterInitialization(Object bean, String beanName)  
        throws BeansException {  
    System.out  
            .println("7. BeanPostProcessor#postProcessAfterInitialization:"  
                    + ++counter);  
    return bean;  
}  

@Override  
public void setApplicationContext(ApplicationContext applicationContext)  
        throws BeansException {  
    System.out.println("4. ApplicationContextAware#setApplicationContext:"  
            + ++counter);  
}  

@Override  
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {  
    System.out.println("3. BeanFactoryAware#setBeanFactory:" + ++counter);  
}  

@Override  
public void setBeanName(String name) {  
    System.out.println("2. BeanNameAware#setBeanName:" + ++counter);  
}  
}

代码:

public class Test {  
    public static void main(String[] args) {  
        ApplicationContext context = new ClassPathXmlApplicationContext("life.xml");  
        context.getBean("holder");  
    }  
}

代码:

<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-3.0.xsd">  
    <bean id="life" class="test.Life" init-method="init"  
        destroy-method="_destroy">  
        <property name="counter" value="1" />  
    </bean>  
    <bean id="holder" class="java.lang.String" />  

为什么输出是:

0. Spring调用构造函数 1. 弹簧定位器DI:1 2. BeanNameAware#setBeanName:2 3. BeanFactoryAware#setBeanFactory:3 4.ApplicationContextAware#setApplicationContext:4 **1320491454906 6. InitializingBean#afterPropertiesSet:5 6'。 bean#init:6** 1320491454921 5。 BeanPostProcessor#postProcessBeforeInitialization: 7 7. BeanPostProcessor#postProcessAfterInitialization:8

根据 Spring in Action 3rd 和 Spring-Reference:

6 如果任何 bean 实现了 BeanPostProcessor 接口,Spring 调用 他们的 postProcessBeforeInitialization() 方法。

7 如果有任何 bean 实现 InitializingBean 接口,Spring 会调用它们的 afterPropertiesSet() 方法。类似地,如果 bean 被声明为 init-method,则调用指定的初始化方法。

好像命令有问题?!

【问题讨论】:

    标签: spring


    【解决方案1】:

    BeanPostProcessor (postProcess...Initialization) 的方法在其他 bean 的初始化期间被调用,而不是后处理器本身。同样,在您引用 Spring in Action postProcessBeforeInitialization() 在第 6 点调用的方法是上下文中声明的其他 BeanPostProcessors 的方法中,而不是正在初始化的 bean 的方法。

    在您的情况下,这些方法在 holder 的初始化期间被调用。

    所以,BeanPostProcessor 是一种允许专用 bean(后处理器)拦截其他 bean 初始化的机制。

    【讨论】:

    • 非常感谢axtavt!我现在明白了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多