`
wiselyman
  • 浏览: 2081061 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
博客专栏
Group-logo
点睛Spring4.1
浏览量:81086
74ae1471-94c5-3ae2-b227-779326b57435
点睛Spring MVC4...
浏览量:130134
社区版块
存档分类
最新评论

07点睛Spring4.1-BeanPostProcessor

 
阅读更多

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修改成了LinuxDemoSelectedBeanPostProcessor的postProcessBeforeInitialization中将num从123修改成了456
os确实被修改成了Linux
num确实被修改成了123

新书推荐《JavaEE开发的颠覆者: Spring Boot实战》,涵盖Spring 4.x、Spring MVC 4.x、Spring Boot企业开发实战。

 

京东地址:http://item.jd.com/11894632.html

当当地址:http://product.dangdang.com/23926195.html

亚马逊地址:http://www.amazon.cn/图书/dp/B01D5ZBFUK/ref=zg_bsnr_663834051_6 

淘宝地址:https://item.taobao.com/item.htm?id=528426235744&ns=1&abbucket=8#detail

 

 

 

或自己在京东、淘宝、亚马逊、当当、互动出版社搜索自选。

 


1
0
分享到:
评论
2 楼 wiselyman 2015-06-16  
FondaWu 写道
写的很好,赞一个
但也发现个小问题:
7.2.2.3例子中public Object postProcessAfterInitialization函数实现写错了吧
按上下文理解,应该是:
((DemoSelectedService) bean).setNum(456);
System.out.println("在DemoSelectedBeanPostProcessor的"+
  "postProcessBeforeInitialization中将num从123修改成了456" );
楼主粗心了



确实粗心了,已修改!
1 楼 FondaWu 2015-06-16  
写的很好,赞一个
但也发现个小问题:
7.2.2.3例子中public Object postProcessAfterInitialization函数实现写错了吧
按上下文理解,应该是:
((DemoSelectedService) bean).setNum(456);
System.out.println("在DemoSelectedBeanPostProcessor的"+
  "postProcessBeforeInitialization中将num从123修改成了456" );
楼主粗心了

相关推荐

Global site tag (gtag.js) - Google Analytics