Spring BeanPostProcessor 中的 postProcessBeforeInitialization 是什么意思?
What does postProcessBeforeInitialization means in Spring BeanPostProcessor?
在XML文件中
<bean id="triangle" class="com.company.aop.model.Triangle">
<property name="name" value="myTriangle"></property>
</bean>
<bean class="com.company.aop.DisplayNameBeanPostProcessor"></bean>
在DisplayNameBeanPostProcessor.javaclass
public class DisplayNameBeanPostProcessor implements BeanPostProcessor{
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
// TODO Auto-generated method stub
if(bean instanceof Triangle) {
// System.out.println("Tr "+(((Triangle) bean).getName().toString()));
System.out.println("I am after intialisation");
}
return bean;
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
// TODO Auto-generated method stub
if(bean instanceof Triangle) {
System.out.println("Tr "+(((Triangle) bean).getName().toString()));
}
return bean;
}
}
现在,当我 运行 这段代码时,它会转到带有参数 bean 和 beanName 的 postProcessBeforeInitialization() 方法并打印消息 "myTriangle"。在我的例子中,这个 bean 有一些信息,比如它的名称字段,值为 "myTriangle"。但是方法签名说它是在初始化之前,那么如果这个 bean 还没有被初始化,那么传递给它的是什么?
和有什么区别
public Object postProcessAfterInitialization(Object bean, String beanName)
和
public Object postProcessBeforeInitialization(Object bean, String beanName)
为什么这条线
System.out.println("Tr "+(((Triangle) bean).getName().toString()));
如果在初始化之前调用了方法,则打印方法 postProcessBeforeInitialization 中的名称?
您也可以参考 Spring 文档 here. The key point here is that the "postProcessBeforeInitialization" should be read as "postProcessBeforeInitializationCallback" and "postProcessAfterInitialization" should be read as "postProcessAfterInitializationCallback". So these are pre and post processeors after the before/after initialization callback are run by the Spring container on the bean. This is what is conveyed in the docs of these methods here 的第 5.8.1 节。
Object postProcessBeforeInitialization(Object bean,
String beanName)
throws BeansException
Apply this BeanPostProcessor to the given new bean instance before any
bean initialization callbacks (like InitializingBean's
afterPropertiesSet or a custom init-method). The bean will already be
populated with property values.
请注意,它说“bean 已经填充了 属性 个值”。
同样
Object postProcessAfterInitialization(Object bean,
String beanName)
throws BeansException
Apply this BeanPostProcessor to the given new bean instance after any
bean initialization callbacks (like InitializingBean's
afterPropertiesSet or a custom init-method). The bean will already be
populated with property values.
BeanPostProcessor bean 是一种特殊的 bean,它在任何其他 bean 之前创建并与新创建的 bean 交互。 postProcessBeforeInitialization
和 postProcessAfterInitialization
应用于 spring 创建的任何其他 bean。
但是方法签名说它是在初始化之前,如果还没有初始化,那么传递给它的这个bean是什么?
这里的初始化指的是调用你的triangle bean的init方法,所以before和post调用包装了对bean的init方法的调用。在此之前,构造函数和 setter 注入已经执行,这就是 bean 具有实际值的原因。
在XML文件中
<bean id="triangle" class="com.company.aop.model.Triangle">
<property name="name" value="myTriangle"></property>
</bean>
<bean class="com.company.aop.DisplayNameBeanPostProcessor"></bean>
在DisplayNameBeanPostProcessor.javaclass
public class DisplayNameBeanPostProcessor implements BeanPostProcessor{
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
// TODO Auto-generated method stub
if(bean instanceof Triangle) {
// System.out.println("Tr "+(((Triangle) bean).getName().toString()));
System.out.println("I am after intialisation");
}
return bean;
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
// TODO Auto-generated method stub
if(bean instanceof Triangle) {
System.out.println("Tr "+(((Triangle) bean).getName().toString()));
}
return bean;
}
}
现在,当我 运行 这段代码时,它会转到带有参数 bean 和 beanName 的 postProcessBeforeInitialization() 方法并打印消息 "myTriangle"。在我的例子中,这个 bean 有一些信息,比如它的名称字段,值为 "myTriangle"。但是方法签名说它是在初始化之前,那么如果这个 bean 还没有被初始化,那么传递给它的是什么?
和有什么区别public Object postProcessAfterInitialization(Object bean, String beanName)
和
public Object postProcessBeforeInitialization(Object bean, String beanName)
为什么这条线
System.out.println("Tr "+(((Triangle) bean).getName().toString()));
如果在初始化之前调用了方法,则打印方法 postProcessBeforeInitialization 中的名称?
您也可以参考 Spring 文档 here. The key point here is that the "postProcessBeforeInitialization" should be read as "postProcessBeforeInitializationCallback" and "postProcessAfterInitialization" should be read as "postProcessAfterInitializationCallback". So these are pre and post processeors after the before/after initialization callback are run by the Spring container on the bean. This is what is conveyed in the docs of these methods here 的第 5.8.1 节。
Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException
Apply this BeanPostProcessor to the given new bean instance before any bean initialization callbacks (like InitializingBean's afterPropertiesSet or a custom init-method). The bean will already be populated with property values.
请注意,它说“bean 已经填充了 属性 个值”。 同样
Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException
Apply this BeanPostProcessor to the given new bean instance after any bean initialization callbacks (like InitializingBean's afterPropertiesSet or a custom init-method). The bean will already be populated with property values.
BeanPostProcessor bean 是一种特殊的 bean,它在任何其他 bean 之前创建并与新创建的 bean 交互。 postProcessBeforeInitialization
和 postProcessAfterInitialization
应用于 spring 创建的任何其他 bean。
但是方法签名说它是在初始化之前,如果还没有初始化,那么传递给它的这个bean是什么?
这里的初始化指的是调用你的triangle bean的init方法,所以before和post调用包装了对bean的init方法的调用。在此之前,构造函数和 setter 注入已经执行,这就是 bean 具有实际值的原因。