【发布时间】: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