Spring的后处理器
Spring的后处理器是Spring对外开放的重要扩展点,允许我们介入到Bean的整个实例化流程中来,以达到动态注册BeanDefinition,动态修改BeanDefinition,以及动态修改Bean的作用。Spring主要有两种后处理器:
- BeanFactoryPostProcessor:Bean工厂后处理器,在BeanDefinitionMap填充完毕,Bean实例化之前执行;
- BeanPostProcessor:Bean后处理器,一般在Bean实例化之后,填充到单例池singletonObjects之前执行。
Bean工厂后处理器-BeanFactoryPostProcessor
BeanFactoryPostProcessor是一个接口规范,实现了该接口的类只要交由Spring容器管理的话,那么Spring就会回调该接口的方法,用于对BeanDefinition注册和修改的功能。
BeanFactoryPostProcessor定义如下:
public interface BeanFactoryPostProcessor {
void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory);
}
使用方法:
package com.luxifa.processor;
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("beanDefinitionMap填充完毕后回调该方法")
}
}
xml中:
<bean class="com.luxifa.processor.MyBeanFactoryPostProcessor"></bean>
案例:修改某个BeanDifinition
package com.luxifa.processor;
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("beanDefinitionMap填充完毕后回调该方法")
//修改某个BeanDifinition
BeanDefinition beanDefintion = beanFactory.getBeanDefinition("userService");
beanDefintion.setBeanClassName("com.luxifa.dao.impl.UserDaoImpl");
}
}
<bean id="userDao" name="aaa,bbb" class="com.luxifa.dao.impl.UserDaoImpl"></bean>
<bean class="userService" class="com.luxifa.service.impl.UserServeiceImpl"></bean>
public class ApplicationContextTest {
public static void main(String[] args) throws Exception{
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
Object userService = applicationContext.getBean("userServeice");
System.out.println(userService);
}
}
打印信息:
beanDefinitionMap填充完毕后回调该方法
com.luxifa.dao.impl.UserDaoImpl@544fe44a
Spring的Bean工厂后处理器注册BeanDefinition
public interface PersonDao {
}
public class PersonDaoImpl implements PersonDao {
}
package com.luxifa.processor;
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("beanDefinitionMap填充完毕后回调该方法")
//注册BeanDifinition
BeanDefinition beanDefintion = new RootBeanDefinition();
beanDefintion.setBeanClassName("com.luxifa.dao.impl.PersonDaoImpl");
DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) beanFactory;
defaultListableBeanFactory.registerBeanDefinition(beanName:"personDao",beanDefinition);
}
}
xml中:
<bean class="com.luxifa.processor.MyBeanFactoryPostProcessor"></bean>
public class ApplicationContextTest {
public static void main(String[] args) throws Exception{
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
PersonDao userService = applicationContext.getBean(PersonDao.class);
System.out.println(userService);
}
}
控制台打印:
beanDefinitionMap填充完毕后回调该方法
com.luxifa.dao.impl.PersonDaoImpl@4e1d422d
Bean工厂后处理器 - BeanFactoryPostProcessor
Spring提供了一个BeanFactoryPostProcessor的子接口BeanDefinitionRegisterPostProcessor专门用来注册BeanDefinition操作
public class MyBeanFactoryPostProcessor2 implements BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeanException {}
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeanException {
BeanDefinitionRegistry beanDefinitionRegistry = new RootBeanDefinition();
beanDefinitionRegistry.setBeanClassName(com.luxifa.dao.userDaoImpl2);
beanDefinitionRegistry.registerBeanDefinition("userDao2",beanDefinition);
}
}
xml中:
<bean class="com.luxifa.processor.MyBeanDefinitionRegistryPostProcessor"></bean>
postProcessBeanDefinitionRegistry是BeanDefinitionRegistryPostProcessor父接口中的方法,也需要实现。
执行顺序 先执行postProcessBeanDefinitionRegistry 方法,后执行 postProcessBeanFactory方法。
Spring的bean工厂后处理器-完善实例化基本流程图
Spring的Bean工厂后处理器-自定义@Component
案例:使用Spring的BeanFactoryPostProcessor扩展点完成自定义注解扫描
- 自定义@MyConponent注解,使用在类上
- 完成指定包的类扫描
- 自定义BeanFactoryPostProcessor完成注解@MyComponent的解析,解析后最终被Spring管理。
package com.luxifa.beans;
@MyComponent("otherBean")
public class OtherBean {
}
package com.luxifa.anno;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyComponent {
String value;
}
package com.luxifa.processor;
public class ComponentBeanFactoryPostProcessor implements BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeanException {}
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeanException {
//通过扫描工具去扫描指定包及其子包下的所有类,收集使用@Mycomponent的注解的类
Map<String, Class> myComponentAnnotationMap = BaseClassScanUtils.scanMyComponentAnnotation(basePackage:"com.luxifa");
//遍历Map,组装BeanDefinition进行注册
myComponentAnnotationMap.forEach((beanName,clazz)->{
//获得beanClassName
String beanClassName = clazz.getName(); //com.luxifa.beans.OtherBean
//创建BeanDefinition
BeanDefinition beanDefinition = new RootBeanDefinition();
beanDefinition.setBeanClassName(beanClassName);
//注册
beanDefinitionRegistry.registerBeanDefinition(beanName,beanDefinition);
});
}
}
xml中:
<bean class="com.luxifa.processor.MyComponentBeanFactoryPostProcessor"></bean>
public class ApplicationContextTest {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
OtherBean otherBean = applicationContext.getBean(OtherBean.class);
System.out.println(otherBean);
}
}