如何正确地从BeanPostProcessor创建代理?

时间:2017-11-15 10:04:02

标签: java spring

我正在编写一个简单的函数,用于通过从BeanPostProcessor创建代理来记录@Metric注释的方法的性能:

Metric.java

@Retention(RUNTIME)
@Target({METHOD, ANNOTATION_TYPE})
public @interface Metric {}

MetricMethodBeanPostProcessor.java

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.util.ClassUtils;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class MetricMethodBeanPostProcessor implements BeanPostProcessor{

private static final Logger log = LoggerFactory.getLogger(MetricMethodBeanPostProcessor.class);

private final Map<String, Class<?>> originalMetricClasses = new HashMap<>();

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    Class<?> originalMetricClass = bean.getClass();
    for (Method method : originalMetricClass.getMethods()) {
        if (method.isAnnotationPresent(Metric.class)) {
            originalMetricClasses.put(beanName, originalMetricClass);
            break;
        }
    }

    return bean;
}

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    Class<?> originalBeanClass = originalMetricClasses.get(beanName);
    if (originalBeanClass != null) {
        return Proxy.newProxyInstance(originalBeanClass.getClassLoader(), ClassUtils.getAllInterfacesForClass(originalBeanClass), (proxy, method, args) -> {
            if (originalBeanClass.getMethod(method.getName(), method.getParameterTypes()).isAnnotationPresent(Metric.class)) {
                commons.utils.Metric metric = new commons.utils.Metric();
                metric.start();
                try {
                    return method.invoke(proxy, args);
                } finally {
                    metric.finish();
                    log.debug("{}: {}", prettyMethodName(method), metric.getElapsedTime());
                }
            }
            return method.invoke(proxy, args);
        });
    }
    return bean;
}

private String prettyMethodName(Method method) {
    return new StringBuilder(method.getDeclaringClass().getSimpleName())
            .append(".")
            .append(method.getName())
            .append("(")
            .append(Arrays.stream(method.getParameterTypes())
                    .map(Class::getSimpleName)
                    .collect(Collectors.joining(", ")))
            .append(")").toString();

}
}

当我尝试使用时,在包含@Metric的bean的自动装配期间出现错误:

    15.11.2017 11:23:55 [WARN ] support.XmlWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tokenAuthController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private abac.authentication.tokenauth.service.TokenAuthService abac.authentication.controllers.TokenAuthController.tokenService; nested exception is java.lang.IllegalArgumentException: Can not set abac.authentication.tokenauth.service.TokenAuthService field abac.authentication.controllers.TokenAuthController.tokenService to com.sun.proxy.$Proxy106
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:664)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:630)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:678)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:549)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:490)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:244)
    at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:626)
    at org.eclipse.jetty.servlet.ServletHolder.initialize(ServletHolder.java:405)
    at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:875)
    at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:346)
    at org.eclipse.jetty.webapp.WebAppContext.startWebapp(WebAppContext.java:1380)
    at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1342)
    at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:772)
    at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:259)
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:518)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
    at org.eclipse.jetty.server.Server.start(Server.java:405)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:106)
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
    at org.eclipse.jetty.server.Server.doStart(Server.java:372)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at org.hc.jp.WebServer.start(WebServer.java:62)
    at org.hc.jp.Main.start(Main.java:42)
    at org.hc.jp.Main.main(Main.java:30)
    at abac.Main.main(Main.java:5)

在启动bean的过程中查看了调试中的BeanPostProcessors列表,发现MetricMethodBeanPostProcessor在中间某处花费了。

我得出的结论是,创建的代理会覆盖原始类及其所有注释,并且所有后续后处理器都不再看到必要的注释。

因此问题:如何正确编写BeanPostProcessor以进行日志记录,以便不会中断?

P.S。谷歌没有帮助

P.P.S不会使用AOP,因为以降低性能的方式测量性能并不好

1 个答案:

答案 0 :(得分:0)

首先,正如M. Deinum所说的那样,性能代价为Proxy == AOP

当我运行代码时

  

***************************申请失败

           

说明

     

bean'xxxxxxx'无法注入   'com.spring.demo.demo.tasks.xxxxxx'因为它是JDK动态代理   实现:

     

动作:

     

考虑将bean注入其接口之一或强制使用   通过设置proxyTargetClass = true来使用基于CGLib的代理   @EnableAsync和/或@EnableCaching。

转换为基于CGLib的代理,我能够运行并执行指标拦截。

    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>3.2.4</version>
    </dependency>
private Object createMetricProxy(Class<?> theOriginalBeanClass) {
      Enhancer enhancer = new Enhancer();
      enhancer.setSuperclass(theOriginalBeanClass);
      enhancer.setCallback((MethodInterceptor) (obj, method, args, proxy) -> {
         if (method.isAnnotationPresent(Metric.class)) {
            StopWatch timer = new StopWatch();
            timer.start();
            try {
               return proxy.invokeSuper(obj, args);
            } finally {
               timer.stop();
               myMetricService.addMethodExecution(prettyMethodName(method) ,timer.getTotalTimeMillis());
               System.out.println("method = " + prettyMethodName(method) + " , time = " +  timer.getTotalTimeMillis());
            }
         } else {
            return proxy.invokeSuper(obj, args);
         }

      });

      return enhancer.create();
   }