07点睛Spring4.1-BeanPostProcessor
7.1 BeanPostProcessor
- spring通过BeanPostProcessor接口可以对所有bean或者指定的某些bean的初始化前后对bean的检查或者修改提供支持;
- 使用postProcessBeforeInitialization和postProcessAfterInitialization对bean进行操作;
- postProcessBeforeInitialization和postProcessAfterInitialization返回值是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
07点睛Spring4.1-BeanPostProcessor的更多相关文章
- 18点睛Spring4.1-Meta Annotation
18.1 Meta Annotation 元注解:顾名思义,就是注解的注解 当我们某几个注解要在多个地方重复使用的时候,写起来比较麻烦,定义一个元注解可以包含多个注解的含义,从而简化代码 下面我们用& ...
- 04点睛Spring4.1-资源调用
转发:https://www.iteye.com/blog/wiselyman-2210666 4.1 Resource spring用来调用外部资源数据的方式 支持调用文件或者是网址 在系统中调用p ...
- 07点睛Spring MVC4.1-ContentNegotiatingViewResolver
转发地址:https://www.iteye.com/blog/wiselyman-2214965 7.1 ContentNegotiatingViewResolver ContentNegotiat ...
- 14点睛Spring4.1-脚本编程
转发:https://www.iteye.com/blog/wiselyman-2212678 14.1 Scripting脚本编程 脚本语言和java这类静态的语言的主要区别是:脚本语言无需编译,源 ...
- 00点睛Spring4.1-环境搭建
转载:https://www.iteye.com/blog/wiselyman-2210250 0.1 前置条件 Spring 4.1提倡基于Java Config和注解的配置,所以本教程通篇不会采用 ...
- 17点睛Spring4.1-@Conditional
17.1 @Conditional @Conditional为按照条件配置spring的bean提供了支持,即满足某种条件下,怎么配置对应的bean; 应用场景 当某一个jar包在classpath中 ...
- 16点睛Spring4.1-TaskScheduler
转发:https://www.iteye.com/blog/wiselyman-2213049 16.1 TaskScheduler 提供对计划任务提供支持; 使用@EnableScheduling开 ...
- 15点睛Spring4.1-TaskExecutor
转发:https://www.iteye.com/blog/wiselyman-2212679 15.1 TaskExecutor spring的TaskExecutor为在spring环境下进行并发 ...
- 13点睛Spring4.1-Spring EL
13.1 Spring EL Spring EL-Spring表达式语言,支持在xml和注解中使用表达式,类似jsp的EL表达式语言; 本教程关注于在注解中使用Spring EL; Spring EL ...
随机推荐
- Oracle 重新编译存储过程/函数等
第一种 如果你使用 PL/SQL Developer工具 左侧工具栏中选择“存储过程”->选择已经失效的procedure->右键->选择重新编译 即可完成 第二 ...
- csp-s模拟测试93T2口胡(蒟蒻的口胡大家显然就不用看了吧
我们先证正确性,再证复杂度 以下记$\left \langle i,j \right \rangle$为考虑$\left [ i,j \right ]$的点时的最优决策 $\left \langle ...
- codevs1580单词游戏
题目描述中说: 单词为at,k=8则新单词为ib 推移规则是:如果k为正数则下推,否则上推,当推移超越边界时回到另一端继续推移. 但在我做题时发现: 这个描述与数据所要求的是完全相反的!!!! 样例1 ...
- 洛谷P3124被困在haybales
题目 按理来说是可以二分的,但是发现其实直接暴力然后注意细节就可以了. 先找到牛所在的起点,然后分别向右找和向左找. 第一次查找从\(r\)点冲到\(l\)点时,突破不了\(l\),从\(l\)点冲到 ...
- 数据结构实验之排序七:选课名单 (SDUT 3404)
#include <stdio.h> #include <string.h> #include <stdlib.h> struct node { char data ...
- vue-d2admin-axios异步请求登录,先对比一下Jquery ajax, Axios, Fetch区别
先说一下对比吧 Jquery ajax, Axios, Fetch区别之我见 引言 前端技术真是一个发展飞快的领域,我三年前入职的时候只有原生XHR和Jquery ajax,我们还曾被JQuery 1 ...
- jmeter正则中常见的转义字符-笔记三
背景和目的 接口测试过程中难免会遇到由于有转义符号正则表达式提取不出来的情况,根据小伙伴们的分享和参考自己实践总结了多种情况 参考 首先,感谢如下常见转义字符,感谢提供参考的小伙伴 参考:https ...
- mysql忘记密码恢复
MySQL忘记密码恢复密码的实现方法 作者:mdxy-dxy 流传较广的方法,mysql中文参考手册上的,各位vps主机租用客户和服务器托管用户忘记mysql5.1管理员密码时,可以使用这种方法破解下 ...
- Windows下基于IIS服务的SSL服务器的配置
Windows下基于IIS服务的SSL服务器的配置 实验环境 Windows Server 2008 R1(CA) Windows Server 2008 R2(web服务器) Windows 7 x ...
- fastdfs通过docker安装
安装前准备 # yum install -y git #下载git # cd /data # mkdir fastdfs # cd fastdfs # git clone https://github ...