07点睛Spring 4.1-BeanPostProcessor
20284 ワード
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を2つ新規作成する
7.2.1.2すべてのbeanを処理するBeanPostProcessorを作成する
7.2.1.3テスト
出力結果:
7.2.2指定されたbeanの処理
7.2.2.2新規指定処理のbean
OSとnum属性に値が割り当てられており、BeanPostProcessorの実装クラスでクラスの属性が変更されます.
7.2.2.3指定beanのBeanPostProcessorの作成
7.2.2.4テスト
出力結果
7.2例
7.2.1全ての処理bean
7.2.1.1試験用のbeanを2つ新規作成する
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