-
BeanPostProcessor : bean后处理器
-
执行的时机 : bean实例化后(属性已经注入了),在bean的init方法(bean配置了init-method)之前调用postProcessBeforeInitialization , init方法调用后在调用postProcessBeforeInitialization
-
注意::如果注册多个BeanPostProcessor实现类,并且需要按顺序执行,那么需要实现Order接口(org.springframework.core.Ordered)
-
示例 :
-
BeanPostProcessor实现类:
package com.study.beanPost;
import com.study.annotation.MyBean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
/**
* @author Jun
* data 2019-04-07 19:53
* bean后处理器
* 注意::如果注册多个BeanPostProcessor实现类,并且需要按顺序执行,
* 那么需要实现Order接口(org.springframework.core.Ordered)
*
* 执行的时机 : bena实例化后(属性已经注入了),在bean的init方法(bean配置了init-method)之前调用postProcessBeforeInitialization ,
* init方法调用后在调用postProcessBeforeInitialization
*/
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
/**
* @param bean 需要处理的bean
* @param beanName 需要处理的bean的名称
*/
@Nullable
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
Class<?> aClass = bean.getClass();
MyBean annotation = aClass.getAnnotation(MyBean.class);
if (annotation == null) return bean;
System.out.println("postProcessAfterInitialization : " + beanName);
return bean;
}
@Nullable
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
Class<?> aClass = bean.getClass();
MyBean annotation = aClass.getAnnotation(MyBean.class);
if (annotation == null) return bean;
System.out.println("postProcessBeforeInitialization : " + beanName);
return bean;
}
}
- bean类
package com.study.beanPost;
import com.study.annotation.MyBean;
/**
* @author Jun
* data 2019-04-07 19:58
*/
@MyBean
public class Person {
private int age ;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
System.out.println(age);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private void init() {
System.out.println("Person init");
}
}
- 注解
package com.study.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyBean {
String value() default "myBean";
}
- spring配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
xmlns:p="http://www.springframework.org/schema/p"
default-autowire="byName"
>
<context:component-scan base-package="com.study"/>
<bean id="person" class="com.study.beanPost.Person" init-method="init">
<property name="name" value="大宝"></property>
<property name="age" value="18"></property>
</bean>
<!--开启AOP对注解的支持持-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
- 测试:
package com.study.beanPost;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author Jun
* data 2019-04-07 20:00
*/
public class TestBean {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = applicationContext.getBean("person", Person.class);
}
}
- 结果
18
postProcessBeforeInitialization : person
Person init
postProcessAfterInitialization : person