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. 从零开始开发一个Spring Boot Starter

    一.Spring Boot Starter简介 Starter是Spring Boot中的一个非常重要的概念,Starter相当于模块,它能将模块所需的依赖整合起来并对模块内的Bean根据环境( 条件 ...

  2. PureComponent下setstate不重新渲染

    https://blog.csdn.net/zhangheli123456/article/details/85053210 可以将  PureComponent 换成React.Component ...

  3. 【CPLEX教程01】Cplex介绍,下载和安装Cplex

    前言 最近学习列生成算法,需要用到优化求解器.所以打算学习一下cplex这个商业求解器. 当然也有其他更多的选择,这里暂时以比较容易上手和性能比较好的cplex开始吧.其实,小编也早就想学习使用这个c ...

  4. golang 斐波那契数

    golang 斐波那契数 package main import "fmt" /* 斐波那契数,亦称之为斐波那契数列(意大利语: Successione di Fibonacci) ...

  5. 「雅礼集训 2018 Day2」农民

    传送门 Description  「搞 OI 不如种田.」 小 D 在家种了一棵二叉树,第 ii 个结点的权值为 \(a_i\). 小 D 为自己种的树买了肥料,每天给树施肥. 可是几天后,小 D 却 ...

  6. 在Typora中使用Latex

    https://blog.csdn.net/happyday_d/article/details/83715440 今后可能更多在本地编辑出Markdown之后复制上博客或者上传到GitHub.

  7. python3 之metaclass

    如果希望创建某一批类全部具有某种特征,则可通过 metaclass 来实现.使用 metaclass 可以在创建类时动态修改类定义. 为了使用 metaclass 动态修改类定义,程序需要先定义 me ...

  8. Oracle的存储的三大物理文件

      分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 一. ...

  9. revenue

    美 ['revənju]   英 ['revənjuː]   n.收益:营业额:税务署 网络收入:税收:岁入

  10. 胶囊网络 -- Capsule Networks

    胶囊网络是 vector in vector out的结构,最后对每个不同的类别,输出不一个向量,向量的模长表示属于该类别的概率. 例如,在数字识别中,两个数字虽然重叠在一起,Capsule中的两个向 ...