Spring BeanPostProcessor接口使用详解

网友投稿 458 2023-03-01


Spring BeanPostProcessor接口使用详解

Spring中提供了很多PostProcessor供开发者进行拓展,例如:BeanPostProcessor、BeanFactoryPostProcessor、BeanValidationPostProcessor等一系列后处理器。他们的使用方式大多类似,了解其中一个并掌握他的使用方式,其他的可以触类旁通。

这里以BeanPostProcessor为例展示其使用方式。

BeanPostProcessor接口提供了两个供开发者自定义的方法:postProcessBeforeInitialization、postProcessAfterInitialization。

postProcessBeforeInitialization:该方法主要针对spring在bean初始化时调用初始化方法前进行自定义处理。

postProcessAfterInitialization:该方法主要针对spring在bean初始化时调用初始化方法后进行自定义处理。

示例代码:

/**

* 测试bean

*/

public class Cat {

private String name;

private int age;

public void say() {

System.out.println("name:" + napCICFLMnme);

System.out.println("age:" + age);

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

}

/**

* 自定义后处理器

*/

public class CatBeanPostProcessor implements BeanPostProcessor {

@Nullable

@Override

public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

if (bean instanceof Cat) {

//输出原始属性

Cat cat = (Cat) bean;

cat.say();

return bean;

}

return bean;

}

@Nullable

@Override

public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

if (bean instanceof Cat) {

//修改属性值,并返回

Cat cat = (Cat) bean;

cat.setName("hello maomi");

cat.setAge(3);

return cat;

}

return bean;

}

}

/**

* 运行

*/

public class Run {

public static void main(String[] args) {

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-bean.xml");

Cat cat = (Cat) applicationContext.getBean("cat");

cat.say();

}

}

xml配置信息

输出结果:

name:HelloKitty

age:1

name:hello maomi

age:3

可以看到通过后处理器处理过后的bean信息已经改变。最后,看看源码中如何调用自定义实现的。

在初始化bean方法中:AbstractAutowireCapableBeanFactory.java

/**

* 初始化bean

*/

protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) {

//省略部分无关代码

Object wrappedBean = bean;

//初始化前

if (mbd == null || !mbd.isSynthetic()) {

wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);

}

try {

//调用初始化方法初始化bean

invokeInitMethods(beanName, wrappedBean, mbd);

}

catch (Throwable ex) {

pCICFLMn throw new BeanCreationException(

(mbd != null ? mbd.getResourceDescription() : null),

beanName, "Invocation of init method failed", ex);

}

//初始化后

if (mbd == null || !mbd.isSynthetic()) {

wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);

}

return wrappedBean;

}

//postProcessBeforeInitialization方法调用

@Override

public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)

throws BeansException {

Object result = existingBean;

for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {

//调用自定义postProcessBeforeInitialization方法

Object current = beanProcessor.postProcessBeforeInitialization(result, beanName);

if (current == null) {

return result;

}

result = current;

}

return result;

}

//postProcessAfterInitialization方法调用

@Override

public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, StpCICFLMnring beanName)

throws BeansException {

Object result = existingBean;

for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {

//自定义postProcessAfterInitialization方法调用

Object current = beanProcessor.postProcessAfterInitialization(result, beanName);

if (current == null) {

return result;

}

result = current;

}

return result;

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:vue脚手架中配置Sass的方法
下一篇:SpringMVC处理multipart请求的示例代码
相关文章

 发表评论

暂时没有评论,来抢沙发吧~