7.1 BeanPostProcessor

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

7.2 示例

7.2.1 处理全部bean

7.2.1.1 新建两个测试用的bean

  1. package com.wisely.beanpostprocessor;
  2.  
  3. import org.springframework.stereotype.Service;
  4.  
  5. @Service
  6. public class DemoNormal1Service {
  7.  
  8. }
  1. package com.wisely.beanpostprocessor;
  2.  
  3. import org.springframework.stereotype.Service;
  4.  
  5. @Service
  6. public class DemoNormal2Service {
  7.  
  8. }

7.2.1.2 编写处理所有bean的BeanPostProcessor

  1. package com.wisely.beanpostprocessor;
  2.  
  3. import org.springframework.beans.BeansException;
  4. import org.springframework.beans.factory.config.BeanPostProcessor;
  5. import org.springframework.stereotype.Component;
  6.  
  7. @Component
  8. public class DemoAllBeanPostProcessor implements BeanPostProcessor{
  9.  
  10. public Object postProcessBeforeInitialization(Object bean, String beanName)
  11. throws BeansException {
  12. System.out.println("在 DemoAllBeanPostProcessor的"
  13. +postProcessBeforeInitialization方法里处理bean: " + beanName
  14. +" bean的类型为:"+bean.getClass());
  15. return bean;
  16. }
  17.  
  18. public Object postProcessAfterInitialization(Object bean, String beanName)
  19. throws BeansException {
  20. System.out.println(" DemoAllBeanPostProcessor"+
  21. postProcessAfterInitialization方法里处理bean: " + beanName
  22. +" bean的类型为:"+bean.getClass());
  23. return bean;
  24. }
  25.  
  26. }

7.2.1.3 测试

  1. package com.wisely.beanpostprocessor;
  2.  
  3. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  4.  
  5. public class Main {
  6.  
  7. public static void main(String[] args) {
  8. AnnotationConfigApplicationContext context =
  9. new AnnotationConfigApplicationContext("com.wisely.beanpostprocessor");
  10. context.close();
  11.  
  12. }
  13.  
  14. }

输出结果为:

  1. DemoAllBeanPostProcessorpostProcessBeforeInitialization方法里处理bean:
  2. demoNormal1Service bean的类型为:class com.wisely.beanpostprocessor.DemoNormal1Service
  3. DemoAllBeanPostProcessorpostProcessAfterInitialization方法里处理bean:
  4. demoNormal1Service bean的类型为:class com.wisely.beanpostprocessor.DemoNormal1Service
  5. DemoAllBeanPostProcessorpostProcessBeforeInitialization方法里处理bean:
  6. demoNormal2Service bean的类型为:class com.wisely.beanpostprocessor.DemoNormal2Service
  7. DemoAllBeanPostProcessorpostProcessAfterInitialization方法里处理bean:
  8. demoNormal2Service bean的类型为:class com.wisely.beanpostprocessor.DemoNormal2Service

7.2.2 处理指定的bean

7.2.2.2 新建指定处理的bean

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

  1. package com.wisely.beanpostprocessor;
  2.  
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.stereotype.Service;
  5.  
  6. @Service
  7. public class DemoSelectedService {
  8. @Value("#{systemProperties['os.name']}")
  9. private String os;
  10. @Value("123")
  11. private Long num;
  12.  
  13. public String getOs() {
  14. return os;
  15. }
  16.  
  17. public void setOs(String os) {
  18. this.os = os;
  19. }
  20.  
  21. public Long getNum() {
  22. return num;
  23. }
  24.  
  25. public void setNum(Long num) {
  26. this.num = num;
  27. }
  28.  
  29. }

7.2.2.3 编写指定bean的BeanPostProcessor

  1. packagecom.wisely.beanpostprocessor;
  2.  
  3. importorg.springframework.beans.BeansException;
  4. importorg.springframework.beans.factory.config.BeanPostProcessor;
  5. importorg.springframework.stereotype.Component;
  6. @Component
  7.  
  8. public class DemoSelectedBeanPostProcessor implements BeanPostProcessor {
  9.  
  10. public Object postProcessBeforeInitialization(Objectbean, StringbeanName)
  11. throwsBeansException {
  12. if(bean instanceof DemoSelectedService){
  13. ((DemoSelectedService) bean).setOs("Linux");
  14. System.out.println("在DemoSelectedBeanPostProcessor的"+"postProcessBeforeInitialization中将os从windows修改成了Linux" );
  15. }
  16. return bean;
  17. }
  18.  
  19. public Object postProcessAfterInitialization(Objectbean, StringbeanName)
  20. throwsBeansException {
  21. if(bean instanceof DemoSelectedService){
  22. ((DemoSelectedService) bean).setNum(456);
  23. System.out.println("在DemoSelectedBeanPostProcessor的"+"postProcessBeforeInitialization中将num从123修改成了456" );
  24. }
  25. return bean;
  26. }
  27.  
  28. }

7.2.2.4 测试

  1. package com.wisely.beanpostprocessor;
  2.  
  3. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  4.  
  5. public class Main {
  6.  
  7. public static void main(String[] args) {
  8. AnnotationConfigApplicationContext context =
  9. new AnnotationConfigApplicationContext("com.wisely.beanpostprocessor");
  10. DemoSelectedService dss = context.getBean(DemoSelectedService.class);
  11. System.out.println("os确实被修改成了"+dss.getOs());
  12. System.out.println("num确实被修改成了"+dss.getNum());
  13. context.close();
  14.  
  15. }
  16.  
  17. }

输出结果

  1. DemoSelectedBeanPostProcessorpostProcessBeforeInitialization中将oswindows修改成了Linux
  2. DemoSelectedBeanPostProcessorpostProcessBeforeInitialization中将num123修改成了456
  3. os确实被修改成了Linux
  4. num确实被修改成了123

07点睛Spring4.1-BeanPostProcessor的更多相关文章

  1. 18点睛Spring4.1-Meta Annotation

    18.1 Meta Annotation 元注解:顾名思义,就是注解的注解 当我们某几个注解要在多个地方重复使用的时候,写起来比较麻烦,定义一个元注解可以包含多个注解的含义,从而简化代码 下面我们用& ...

  2. 04点睛Spring4.1-资源调用

    转发:https://www.iteye.com/blog/wiselyman-2210666 4.1 Resource spring用来调用外部资源数据的方式 支持调用文件或者是网址 在系统中调用p ...

  3. 07点睛Spring MVC4.1-ContentNegotiatingViewResolver

    转发地址:https://www.iteye.com/blog/wiselyman-2214965 7.1 ContentNegotiatingViewResolver ContentNegotiat ...

  4. 14点睛Spring4.1-脚本编程

    转发:https://www.iteye.com/blog/wiselyman-2212678 14.1 Scripting脚本编程 脚本语言和java这类静态的语言的主要区别是:脚本语言无需编译,源 ...

  5. 00点睛Spring4.1-环境搭建

    转载:https://www.iteye.com/blog/wiselyman-2210250 0.1 前置条件 Spring 4.1提倡基于Java Config和注解的配置,所以本教程通篇不会采用 ...

  6. 17点睛Spring4.1-@Conditional

    17.1 @Conditional @Conditional为按照条件配置spring的bean提供了支持,即满足某种条件下,怎么配置对应的bean; 应用场景 当某一个jar包在classpath中 ...

  7. 16点睛Spring4.1-TaskScheduler

    转发:https://www.iteye.com/blog/wiselyman-2213049 16.1 TaskScheduler 提供对计划任务提供支持; 使用@EnableScheduling开 ...

  8. 15点睛Spring4.1-TaskExecutor

    转发:https://www.iteye.com/blog/wiselyman-2212679 15.1 TaskExecutor spring的TaskExecutor为在spring环境下进行并发 ...

  9. 13点睛Spring4.1-Spring EL

    13.1 Spring EL Spring EL-Spring表达式语言,支持在xml和注解中使用表达式,类似jsp的EL表达式语言; 本教程关注于在注解中使用Spring EL; Spring EL ...

随机推荐

  1. Oracle 重新编译存储过程/函数等

    第一种  如果你使用 PL/SQL Developer工具          左侧工具栏中选择“存储过程”->选择已经失效的procedure->右键->选择重新编译 即可完成 第二 ...

  2. csp-s模拟测试93T2口胡(蒟蒻的口胡大家显然就不用看了吧

    我们先证正确性,再证复杂度 以下记$\left \langle i,j \right \rangle$为考虑$\left [ i,j \right ]$的点时的最优决策 $\left \langle ...

  3. codevs1580单词游戏

    题目描述中说: 单词为at,k=8则新单词为ib 推移规则是:如果k为正数则下推,否则上推,当推移超越边界时回到另一端继续推移. 但在我做题时发现: 这个描述与数据所要求的是完全相反的!!!! 样例1 ...

  4. 洛谷P3124被困在haybales

    题目 按理来说是可以二分的,但是发现其实直接暴力然后注意细节就可以了. 先找到牛所在的起点,然后分别向右找和向左找. 第一次查找从\(r\)点冲到\(l\)点时,突破不了\(l\),从\(l\)点冲到 ...

  5. 数据结构实验之排序七:选课名单 (SDUT 3404)

    #include <stdio.h> #include <string.h> #include <stdlib.h> struct node { char data ...

  6. vue-d2admin-axios异步请求登录,先对比一下Jquery ajax, Axios, Fetch区别

    先说一下对比吧 Jquery ajax, Axios, Fetch区别之我见 引言 前端技术真是一个发展飞快的领域,我三年前入职的时候只有原生XHR和Jquery ajax,我们还曾被JQuery 1 ...

  7. jmeter正则中常见的转义字符-笔记三

    背景和目的 接口测试过程中难免会遇到由于有转义符号正则表达式提取不出来的情况,根据小伙伴们的分享和参考自己实践总结了多种情况 参考 首先,感谢如下常见转义字符,感谢提供参考的小伙伴  参考:https ...

  8. mysql忘记密码恢复

    MySQL忘记密码恢复密码的实现方法 作者:mdxy-dxy 流传较广的方法,mysql中文参考手册上的,各位vps主机租用客户和服务器托管用户忘记mysql5.1管理员密码时,可以使用这种方法破解下 ...

  9. Windows下基于IIS服务的SSL服务器的配置

    Windows下基于IIS服务的SSL服务器的配置 实验环境 Windows Server 2008 R1(CA) Windows Server 2008 R2(web服务器) Windows 7 x ...

  10. fastdfs通过docker安装

    安装前准备 # yum install -y git #下载git # cd /data # mkdir fastdfs # cd fastdfs # git clone https://github ...