Spring BeanPostProcessor接口使用详解
程序员文章站
2023-12-17 11:15:46
spring中提供了很多postprocessor供开发者进行拓展,例如:beanpostprocessor、beanfactorypostprocessor、beanva...
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:" + name); 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配置信息
<!--配置bean并初始化--> <bean id="cat" class="com.source.postprocessor.cat" > <property name="name" value="hellokitty" /> <property name="age" value="1" /> </bean> <bean id="catbeanpostprocessor" class="com.source.postprocessor.catbeanpostprocessor" />
输出结果:
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) { 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, string 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; }
以上就是spring对自定义方法实现的调用过程。希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
Spring BeanPostProcessor接口使用详解
-
详解如何使用Jersey客户端请求Spring Boot(RESTFul)服务
-
详解spring cloud中使用Ribbon实现客户端的软负载均衡
-
Spring中property-placeholder的使用与解析详解
-
Spring Cloud Alibaba使用Sentinel实现接口限流
-
详解Spring中使用xml配置bean的细节
-
详解如何在spring boot中使用spring security防止CSRF攻击
-
Spring Cache的基本使用与实现原理详解
-
详解使用spring boot admin监控spring cloud应用程序
-
Spring Security使用中Preflight请求和跨域问题详解