BeanPostProcessor 详解
常规解释
BeanPostProcessor
后置处理器,再初始化前后进行处理,通过bean获取相关内容。
public interface BeanPostProcessor {
//bean初始化方法调用前 IOC容器实例化Bean->调用postProcessBeforeInitialization方法
Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
//bean初始化方法调用后 IOC容器实例化Bean初始化后->调用postProcessAfterInitialization方法
Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
}
Configuation
用于定义配置类,可替换xml配置文件,用于构建bean定义,初始化Spring容器。
@Configuration //用于初始化容器
public class SysHandlerConfig {
public SysHandlerConfig() {}
@Bean
public SysRegistHandler getSysHandler(){
return new SysRegistHandler();
}
}
使用案例
和AOP类似,但是没有AOP方便,确可以用来多个模块之间进行配合处理,降低依赖关系。
比如提供一个API给客户,但是可以兼容其他默认进行数据注入调整,类似动态代理修改结果。
实现接口动态修改数据
//执行后置处理器 注入数据
public class SysRegistHandler implements BeanPostProcessor, DisposableBean {
private static ConcurrentMap<String, SysServiceHandler> methodHandlerRepository = new ConcurrentHashMap();
@Override
public void destroy() throws Exception {}
public SysRegistHandler() { }
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
new Thread(new Runnable() {
@Override
public void run() {
SysRegistHandler.this.initElogHandlerMethodRepository(bean, beanName);
}
}).start();
return bean;
}
public static SysServiceHandler loadMethodHandler(String name) {
return (SysServiceHandler) methodHandlerRepository.get(name);
}
public SysServiceHandler registMethodHandler(String name, SysServiceHandler methodHandler) {
return (SysServiceHandler) methodHandlerRepository.put(name, methodHandler);
}
private void initElogHandlerMethodRepository(final Object bean, final String beanName) {
//版本spring-webmvc-4.1.6.RELEASE.jar
Set<Method> methods = HandlerMethodSelector.selectMethods(bean.getClass(), new ReflectionUtils.MethodFilter() {
@Override
public boolean matches(Method method) {
return AnnotationUtils.findAnnotation(method, ProxyMethod.class) != null;
}
});
if(methods != null && !methods.isEmpty()) {
Iterator iterator = methods.iterator();
while (iterator.hasNext()) {
Method executeMethod = (Method) iterator.next();
ProxyMethod batchImportHandler = executeMethod.getAnnotation(ProxyMethod.class);
if (batchImportHandler != null) {
//这里只是简单的测试,可以通过责任链
String name = batchImportHandler.value();
if (name.trim().length() == 0) throw new RuntimeException("sys method-importhandler name invalid, for[" + bean.getClass() + "#" + executeMethod.getName() + "] .");
if (loadMethodHandler(name) != null) throw new RuntimeException("sys method-importhandler[" + name + "] naming conflicts.");
executeMethod.setAccessible(true);
this.registMethodHandler(name, new SysServiceHandler(bean, executeMethod));
}
}
}
}
}
//通过注解注入方法 便于代码后期调整
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface ProxyMethod {
String value();
}
//注入数据管理 用来管理接口 管理执行逻辑 可以调整为链式模式
public class SysServiceHandler {
private final Object target;
private final Method method;
public SysServiceHandler(Object target, Method method) {
this.target = target;
this.method = method;
}
public Object execute(Object param) throws Exception {
Class<?>[] paramTypes = this.method.getParameterTypes();
return paramTypes.length > 0 ? this.method.invoke(this.target, param) : this.method.invoke(this.target);
}
public String toString() {
return super.toString() + "[" + this.target.getClass() + "#" + this.method.getName() + "]";
}
}
测试
@ProxyMethod("cc2018")
public String test(String str){
return str+":handler";
}
public static void main(String[] args) {
SysServiceHandler methodHandler = SysRegistHandler.loadMethodHandler("cc2018");
String userName = "";
if(methodHandler != null) {
try {
userName = (String) methodHandler.execute(userName);
} catch (Exception e) {
e.printStackTrace();
}
}
}