BeanPostProcessor的用法
BeanPostProcessor也稱為Bean后置處理器,它是Spring中定義的接口凑保,在Spring容器的創(chuàng)建過程中(具體為Bean初始化前后)會回調(diào)BeanPostProcessor中定義的兩個方法。BeanPostProcessor的源碼如下:
public interface BeanPostProcessor {
Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
}
其中postProcessBeforeInitialization方法會在每一個bean對象的初始化方法調(diào)用之前回調(diào)犯助;postProcessAfterInitialization方法會在每個bean對象的初始化方法調(diào)用之后被回調(diào)。
提出的問題
在Spring開發(fā)過程中剂买,存在同一個接口有多個實現(xiàn)類的情況癌蓖,根據(jù)不同的應用場景瞎颗,通常在具體調(diào)用的地方來選擇不同的接口實現(xiàn)類疯特,雖然可以在抽象出一個工廠方法蜘腌,但是還是感覺不夠優(yōu)雅。如果通過注解的方式解決這個問題呢责循,Spring的BeanPostProcessor是個好的選擇。
具體實現(xiàn)
聲明接口
public interface HelloService{
public void sayHello();
}
接口實現(xiàn)類1
@Service
public class HelloServiceImpl1 implements HelloService {
@Override
public void sayHello() {
System.out.println("你好我是HelloServiceImpl1");
}
}
接口實現(xiàn)類2
@Service
public class HelloServiceImpl2 implements HelloService {
@Override
public void sayHello() {
System.out.println("你好我是HelloServiceImpl2");
}
}
自定義注解
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface RountingInjected {
String value() default "helloServiceImpl1";
}
自定義的BeanPostProcessor類
@Component
public class HelloServiceInjectProcessor implements BeanPostProcessor {
@Autowired
private ApplicationContext applicationContext;
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
Class<?> targetCls = bean.getClass();
Field[] targetFld = targetCls.getDeclaredFields();
for (Field field : targetFld) {
//找到制定目標的注解類
if (field.isAnnotationPresent(RountingInjected.class)) {
if (!field.getType().isInterface()) {
throw new BeanCreationException("RoutingInjected field must be declared as an interface:" + field.getName()
+ " @Class " + targetCls.getName());
}
try {
this.handleRoutingInjected(field, bean, field.getType());
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
return bean;
}
/**
* @param field
* @param bean
* @param type
* @throws IllegalAccessException
*/
private void handleRoutingInjected(Field field, Object bean, Class type) throws IllegalAccessException {
Map<String, Object> candidates = this.applicationContext.getBeansOfType(type);
field.setAccessible(true);
if (candidates.size() == 1) {
field.set(bean, candidates.values().iterator().next());
} else if (candidates.size() == 2) {
String injectVal = field.getAnnotation(RountingInjected.class).value();
Object proxy = RoutingBeanProxyFactory.createProxy(injectVal, type, candidates);
field.set(bean, proxy);
} else {
throw new IllegalArgumentException("Find more than 2 beans for type: " + type);
}
}
}
代理實現(xiàn)類
public class RoutingBeanProxyFactory {
private final static String DEFAULT_BEAN_NAME = "helloServiceImpl1";
public static Object createProxy(String name, Class type, Map<String, Object> candidates) {
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setInterfaces(type);
proxyFactory.addAdvice(new VersionRoutingMethodInterceptor(name, candidates));
return proxyFactory.getProxy();
}
static class VersionRoutingMethodInterceptor implements MethodInterceptor {
private Object targetObject;
public VersionRoutingMethodInterceptor(String name, Map<String, Object> beans) {
this.targetObject = beans.get(name);
if (this.targetObject == null) {
this.targetObject = beans.get(DEFAULT_BEAN_NAME);
}
}
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
return invocation.getMethod().invoke(this.targetObject, invocation.getArguments());
}
}
}
SpringBoot入口類
@SpringBootApplication
@MapperScan("com.lx.mapper")
public class MlxcApplication {
public static void main(final String[] args) {
try (ConfigurableApplicationContext applicationContext = SpringApplication.run(MlxcApplication.class, args)) {
HelloServiceTest helloService = applicationContext.getBean(HelloServiceTest.class);
helloService.testSayHello();
}
}
}
總結(jié)
上述是整個解決方案的示例流程歹垫,其核心思想就是根據(jù)自定義注解攔截要注入的接口實現(xiàn)類,運用java反射和代理的知識點來進行有效的實現(xiàn)類注入吭敢。