您好,登錄后才能下訂單哦!
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對自定義方法實現的調用過程。希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。