Man*_*ani 1 java spring spring-mvc javabeans
在XML文件中
<bean id="triangle" class="com.company.aop.model.Triangle">
<property name="name" value="myTriangle"></property>
</bean>
<bean class="com.company.aop.DisplayNameBeanPostProcessor"></bean>
Run Code Online (Sandbox Code Playgroud)
在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;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,当我运行此代码时,它将使用参数bean和beanName转到postProcessBeforeInitialization()方法,并打印消息"myTriangle".在我的例子中,这个bean的名称字段的信息值为"myTriangle".但是方法签名说它是在初始化之前然后如果尚未初始化那么已经传入它的bean是什么?有什么区别
public Object postProcessAfterInitialization(Object bean, String beanName)
Run Code Online (Sandbox Code Playgroud)
和
public Object postProcessBeforeInitialization(Object bean, String beanName)
Run Code Online (Sandbox Code Playgroud)
为什么这一行
System.out.println("Tr "+(((Triangle) bean).getName().toString()));
Run Code Online (Sandbox Code Playgroud)
如果在初始化之前调用了方法,则在postProcessBeforeInitialization方法中打印名称?
您可以在这里参考Spring文档的5.8.1节.这里的关键点是"postProcessBeforeInitialization"应该被读作"postProcessBeforeInitializationCallback",而"postProcessAfterInitialization"应该被读作"postProcessAfterInitializationCallback".所以这些是在bean之前的Spring容器运行之前/之后的初始化回调之后的前后处理器.这就是在这些方法的文档传送这里了.
Object postProcessBeforeInitialization(Object bean,String beanName)抛出BeansException
在任何bean初始化回调之前将此BeanPostProcessor应用于给定的新bean实例(如InitializingBean的afterPropertiesSet或自定义init方法).bean已经填充了属性值.
请注意,它说" bean已经填充了属性值 ".同样
Object postProcessAfterInitialization(Object bean,String beanName)抛出BeansException
在任何bean初始化回调(如InitializingBean的afterPropertiesSet或自定义init方法)之后,将此BeanPostProcessor应用于给定的新bean实例.bean已经填充了属性值.
归档时间: |
|
查看次数: |
3859 次 |
最近记录: |