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

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. Django 第一天 开端

    今日内容: 一.HTTP协议 1.HTTP协议简介 参考博客:https://www.cnblogs.com/clschao/articles/9230431.html 是超文本传输协议 现在使用最广 ...

  2. linux ps命令查看最消耗CPU、内存的进程

    1.CPU占用最多的前10个进程: ps auxw|head -1;ps auxw|sort -rn -k3|head -10 2.内存消耗最多的前10个进程 ps auxw|head -1;ps a ...

  3. learning java AWT 布局管理器 GridBagLayout

    在GridBagLayout布局管理器中,一个组件可以跨越一个或多个网格,并可以设置各网格的大小互不相关. import java.awt.*; public class GridBagTest { ...

  4. GDB十分钟教程【转载于网络爱好者】

    本文写给主要工作在Windows操作系统下而又需要开发一些跨平台软件的程序员朋友,以及程序爱好者. GDB是一个由GNU开源组织发布的.UNIX/LINUX操作系统下的.基于命令行的.功能强大的程序调 ...

  5. Maven 异常 druid jar冲突

    异常: 十二月 25, 2017 11:04:41 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin 警告: [SetPropert ...

  6. +和join的区别

    + 每次进行相加都会开辟新的空间,回收旧的空间join 一次性开辟好空间,一次性添加进去,一次性回收

  7. 「雅礼集训 2017 Day10」拍苍蝇

    传送门 Description 有一天,小 A 的母亲对他家里的卫生状况非常不满意,他的房间里有非常多的苍蝇.在母亲的威逼利诱下,小 A 拿起了苍蝇拍去消灭家里的苍蝇.然而,小 A 以前从来没有亲手消 ...

  8. java非空判断

    是否为 null 是否为 "" 是否为空字符串(引号中间有空格)  如: "     ". 制表符.换行符.换页符和回车 一. 字符串 1. if(str == ...

  9. DM当中用文本输入点【转载】

    摘自<ANSYS 13.0 Workbench数值模拟技术> 通过XYZ坐标的文本文件创建3D曲线,文本需要满足一定的格式,格式化文本中,#表示此行是注释,忽略空行,数据行包括5个数据域, ...

  10. 简要介绍 X Window System (又称为X11 or X)

    X11是一个应用程序,而不是一个操作系统,主要功能是提供图形界面,实现架构和网络架构相同,有X Client和X Server组件,另外还有Window Manager和Display Manager ...