7.1 BeanPostProcessor

  • spring通过BeanPostProcessor接口可以对所有bean或者指定的某些bean的初始化前后对bean的检查或者修改提供支持;
  • 使用postProcessBeforeInitializationpostProcessAfterInitialization对bean进行操作;
  • postProcessBeforeInitializationpostProcessAfterInitialization返回值是bean;

7.2 示例

7.2.1 处理全部bean

7.2.1.1 新建两个测试用的bean

package com.wisely.beanpostprocessor;

import org.springframework.stereotype.Service;

@Service
public class DemoNormal1Service { }
package com.wisely.beanpostprocessor;

import org.springframework.stereotype.Service;

@Service
public class DemoNormal2Service { }

7.2.1.2 编写处理所有bean的BeanPostProcessor

package com.wisely.beanpostprocessor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component; @Component
public class DemoAllBeanPostProcessor implements BeanPostProcessor{ public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("在 DemoAllBeanPostProcessor的"
+postProcessBeforeInitialization方法里处理bean: " + beanName
+" bean的类型为:"+bean.getClass());
return bean;
} public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("在 DemoAllBeanPostProcessor的"+
postProcessAfterInitialization方法里处理bean: " + beanName
+" bean的类型为:"+bean.getClass());
return bean;
} }

7.2.1.3 测试

package com.wisely.beanpostprocessor;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {

    public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("com.wisely.beanpostprocessor");
context.close(); } }

输出结果为:

在 DemoAllBeanPostProcessor的postProcessBeforeInitialization方法里处理bean:
demoNormal1Service bean的类型为:class com.wisely.beanpostprocessor.DemoNormal1Service
在 DemoAllBeanPostProcessor的postProcessAfterInitialization方法里处理bean:
demoNormal1Service bean的类型为:class com.wisely.beanpostprocessor.DemoNormal1Service
在 DemoAllBeanPostProcessor的postProcessBeforeInitialization方法里处理bean:
demoNormal2Service bean的类型为:class com.wisely.beanpostprocessor.DemoNormal2Service
在 DemoAllBeanPostProcessor的postProcessAfterInitialization方法里处理bean:
demoNormal2Service bean的类型为:class com.wisely.beanpostprocessor.DemoNormal2Service

7.2.2 处理指定的bean

7.2.2.2 新建指定处理的bean

已经给os和num属性赋值,将在BeanPostProcessor的实现类对类的属性进行修改

package com.wisely.beanpostprocessor;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; @Service
public class DemoSelectedService {
@Value("#{systemProperties['os.name']}")
private String os;
@Value("123")
private Long num; public String getOs() {
return os;
} public void setOs(String os) {
this.os = os;
} public Long getNum() {
return num;
} public void setNum(Long num) {
this.num = num;
} }

7.2.2.3 编写指定bean的BeanPostProcessor

packagecom.wisely.beanpostprocessor;

importorg.springframework.beans.BeansException;
importorg.springframework.beans.factory.config.BeanPostProcessor;
importorg.springframework.stereotype.Component;
@Component public class DemoSelectedBeanPostProcessor implements BeanPostProcessor { public Object postProcessBeforeInitialization(Objectbean, StringbeanName)
throwsBeansException {
if(bean instanceof DemoSelectedService){
((DemoSelectedService) bean).setOs("Linux");
System.out.println("在DemoSelectedBeanPostProcessor的"+"postProcessBeforeInitialization中将os从windows修改成了Linux" );
}
return bean;
} public Object postProcessAfterInitialization(Objectbean, StringbeanName)
throwsBeansException {
if(bean instanceof DemoSelectedService){
((DemoSelectedService) bean).setNum(456);
System.out.println("在DemoSelectedBeanPostProcessor的"+"postProcessBeforeInitialization中将num从123修改成了456" );
}
return bean;
} }

7.2.2.4 测试

package com.wisely.beanpostprocessor;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {

    public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("com.wisely.beanpostprocessor");
DemoSelectedService dss = context.getBean(DemoSelectedService.class);
System.out.println("os确实被修改成了"+dss.getOs());
System.out.println("num确实被修改成了"+dss.getNum());
context.close(); } }

输出结果

在DemoSelectedBeanPostProcessor的postProcessBeforeInitialization中将os从windows修改成了Linux
在DemoSelectedBeanPostProcessor的postProcessBeforeInitialization中将num从123修改成了456
os确实被修改成了Linux
num确实被修改成了123

最新文章

  1. POJ1742Coins(并不理解是什么意思)
  2. NOIP复习赛20161117
  3. windows下利用virtual 安装 flask
  4. Solr:Schema设计
  5. 南邮CTF隐写之丘比龙的女神
  6. Java学习随笔1:Java是值传递还是引用传递?
  7. SharePoint 基于 REST API使用简介
  8. github和bitbucket
  9. python交互模式下cp65001异常
  10. Hadoop中的InputFormat解析
  11. SharePoint 2010 PowerShell 系列 之 备份、还原、部署 .WSP
  12. 一般处理程序在VS2012中打开问题
  13. 零基础学Python--------第8章 模块
  14. RDKIT+postgresql做化合物数据存储与查找
  15. Python中os.system和os.popen区别
  16. SqlDataHelper
  17. PHP正则表达式匹配俄文字符
  18. Jersey框架
  19. 20个可能你不知道Linux网路工具
  20. ADS1.2使用

热门文章

  1. Oracle CAST() 函数 数据类型的转换
  2. 洛谷P1039侦探推理题解
  3. 自行撰写Grasshopper电池
  4. Prometheus告警规则增删改自动化
  5. border-radius后面写px/rem与百分比有什么区别?
  6. Python自动化测试常用库
  7. [spring-boot] 配置随机端口
  8. office project visio 2019
  9. 教你如何使用QBDI动态二进制检测框架
  10. ThinkPHP5使用phpspreadsheet导入导出Excel