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 ...
随机推荐
- learning java Runtime类中的exec
var rt = Runtime.getRuntime(); // 类c语言当中的system()函数. rt.exec("notepad.exe");
- editplus 支持lua语言语法高亮显示
找到自己的安装目录 建一个这个名字的文件 里面写上 #TITLE=LUA ; LUA syntax file written by ES-Computing. ; This file is requi ...
- Codevs 3002 石子归并 3(DP四边形不等式优化)
3002 石子归并 3 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 有n堆石子排成一列,每堆石子有一个重量w[i], 每次 ...
- YII框架的依赖注入容器
依赖注入(Dependency Injection,DI)容器就是一个对象,它知道怎样初始化并配置对象及其依赖的所有对象. 所谓的依赖就是,一个对象,要使用另外一个对象才能完成某些功能.那么这个对象就 ...
- PHP chmod() 函数
chmod() 函数改变文件模式. 如果成功则返回 TRUE,否则返回 FALSE. 例子 <?php // 所有者可读写,其他人没有任何权限 chmod(); // 所有者可读写,其他人可读 ...
- SQL Server 2008R2安装
SQL Server 2008详细安装过程及配置 https://www.cnblogs.com/rewwensoftware/p/9580697.html SQL Server 2008R2 百 ...
- 微信小程序 图片设置为圆形
要图片圆形显示,需要设置border-radius:50%,还要设置overflow:hidden,具体如下: Tip:user-avatar是图片控件的class .user-avatar { wi ...
- 设置虚拟机ip地址
当我们在linux中输入命令ifconfig来查找ip地址的时候,可能会出现只有ipv6,而没有inet的情况,这时需要自己设定,有两种方式可供选择: 1,命令 ifconfig 设备名(如eth0) ...
- Linux下CRMEB环境搭建
环境准备:PHP7.0.33MySQL5.7Apache2.4 PHP环境安装: sudo apt--cli php7.-common php7.-curl \ php7.-dev php7.-fpm ...
- Requests库的主要方法:requests.request为requests.get和requests.post两个的汇总,只是需要传方法
1. requests.request(method,url,**kwargs) method:请求方式,对应get/put/post等七种 :拟获取页面的url链接 :控制访问参数,共13个 met ...