gpt4 book ai didi

java - Spring BeanPostProcessor中的postProcessBeforeInitialization是什么意思?

转载 作者:行者123 更新时间:2023-11-29 06:32:37 24 4
gpt4 key购买 nike

在 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.java 类中

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文档的5.8.1节here .这里的关键点是“postProcessBeforeInitialization”应该读作“postProcessBeforeInitializationCallback”,“postProcessAfterInitialization”应该读作“postProcessAfterInitializationCallback”。因此,在 before/after 初始化回调由 Spring 容器在 bean 上运行之后,这些是前后处理器。这就是这些方法的文档中传达的内容 here也是。

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.

关于java - Spring BeanPostProcessor中的postProcessBeforeInitialization是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29272624/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com