- 容器后处理器针对的是容器本身,可以对整个容器进行修修补补。
- 用法是让容器后处理器类实现BeanFactoryPostProcessor接口,重写postProcessBeanFactory方法,又是这个套路啦~~
void postProcessBeanFactory(ConfigurableListableBeanFactory var1) throws BeansException;
把实现了容器后处理器的类按照普通bean一样在配置文件中配置,然后spring在完成容器初始化之后就对自动调用postProcessBeanFactory方法,把容器当作参数传进去。
-
例子在这~~
package TestPackage; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; //测试类 public class SpringTest { public static void main(String [] args){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); } }
package util; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; //容器后处理器类,打印出容器的相关信息 public class FactoryPostProcessor implements BeanFactoryPostProcessor { @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { System.out.println("spring容器是" + beanFactory); } }
<?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd "> <bean id="FactoryPostProcessor" class="util.FactoryPostProcessor"/> <!--把容器后处理器当作普通的bean配置就好了--> </beans>
这是我看李刚编著的《轻量级javaEE企业应用实战(第五版)-Struts2+Spring5+Hibernate5/JAP2》后总结出来的。
Spring 进阶(2) 容器后处理器
猜你喜欢
转载自blog.csdn.net/weixin_39452731/article/details/84889965
今日推荐
周排行