Spring的发布处理器(BeanPostProcessor)
2016-07-10 10:27
543 查看
BeanFactoryPostProcessor和BeanPostProcessor都是spring初始化bean的扩展点,两个接口非常相似。
一、BeanFactoryPostProcessor可以对bean的定义(配置元数据)进行处理。也就是说,Spring IoC容器允许BeanFactoryPostProcessor在容器实际实例化任何其它的bean之前读取配置元数据,并有可能修改它。通过beanFactory可以获取bean的示例或定义等,同时可以修改bean的属性,这是和BeanPostProcessor最大的区别。
BeanDefinition bd = beanFactory.getBeanDefinition("xxBean");
MutablePropertyValues mpv = bd.getPropertyValues();
if(pv.contains("xxName")) {
pv.addPropertyValue("xxName", "icoder");
}
如果你愿意,你可以配置多个BeanFactoryPostProcessor。通过设置'order'属性来控制BeanFactoryPostProcessor的执行次序。
注册BeanFactoryPostProcessor的实例,需要重写void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException方法;
二、注册BeanPostProcessor的实例,需要重载
Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException方法;
和
Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException方法;
示例:
bean:
processor:
applicationContext.xml:
程序主入口:
package com.yourcompany.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Hello{
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
HelloBean hello=(HelloBean)context.getBean("helloBean");
hello.getMessage();
}
}<span style="font-size:18px;">
</span>
三、还有一点区别就是BeanFactoryPostProcessor的回调比BeanPostProcessor要早。
参考:http://blog.csdn.net/mn11201117/article/details/24986325
BeanFactoryPostProcessor和BeanPostProcessor都是spring初始化bean的扩展点,两个接口非常相似。
一、BeanFactoryPostProcessor可以对bean的定义(配置元数据)进行处理。也就是说,Spring IoC容器允许BeanFactoryPostProcessor在容器实际实例化任何其它的bean之前读取配置元数据,并有可能修改它。通过beanFactory可以获取bean的示例或定义等,同时可以修改bean的属性,这是和BeanPostProcessor最大的区别。
BeanDefinition bd = beanFactory.getBeanDefinition("xxBean");
MutablePropertyValues mpv = bd.getPropertyValues();
if(pv.contains("xxName")) {
pv.addPropertyValue("xxName", "icoder");
}
如果你愿意,你可以配置多个BeanFactoryPostProcessor。通过设置'order'属性来控制BeanFactoryPostProcessor的执行次序。
注册BeanFactoryPostProcessor的实例,需要重写void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException方法;
二、注册BeanPostProcessor的实例,需要重载
Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException方法;
和
Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException方法;
示例:
bean:
package com.yourcompany.spring; public class HelloBean{ private String message; public void setMessage(String message){ this.message = message; } public void getMessage(){ System.out.println("Your Message : " + message); } public void init(){ System.out.println("Bean is going through init."); } }
processor:
package com.yourcompany.spring; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; public class Processor implements BeanPostProcessor{ public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException { System.out.println("After Initialization : " + arg0); return arg0; } public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException { System.out.println("Before Initialization : " + arg0); return arg0; } }
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="processor" class="com.yourcompany.spring.Processor"></bean> <bean id="helloBean" class="com.yourcompany.spring.HelloBean" init-method="init"> <property name="message" value="Hello,World!"/> </bean> </beans>
程序主入口:
package com.yourcompany.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Hello{
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
HelloBean hello=(HelloBean)context.getBean("helloBean");
hello.getMessage();
}
}<span style="font-size:18px;">
</span>
三、还有一点区别就是BeanFactoryPostProcessor的回调比BeanPostProcessor要早。
参考:http://blog.csdn.net/mn11201117/article/details/24986325
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- Spring AOP动态代理-切面
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- 模拟Spring的简单实现
- Spring整合WebSocket应用示例(上)
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- 监听器获取Spring配置文件的方法
- Java利用Sping框架编写RPC远程过程调用服务的教程
- springmvc 发送ajax出现中文乱码的解决方法汇总
- Spring MVC中Ajax实现二级联动的简单实例
- SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的
- 详解Java的MyBatis框架和Spring框架的整合运用
- struts2 spring整合fieldError问题