SpringMVC系列:BeanPostProcessor的小秘密

本文详细介绍了Spring中的BeanPostProcessor接口,它允许在bean初始化前后进行自定义处理。通过实现该接口,可以在不修改原有代码的情况下,动态注入数据或调整结果。文中提供了一个具体的配置类和使用案例,展示了如何通过@ProxyMethod注解来动态注入方法,并创建SysServiceHandler以执行逻辑。此外,还展示了如何在不同的模块间协调处理,降低依赖关系。
摘要由CSDN通过智能技术生成

常规解释

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();
            }
        }
    }

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值