gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:32:29 26 4
gpt4 key购买 nike

我正在编写一个简单的函数,通过从 BeanPostProcessor 创建一个代理来记录 @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();

}
}

当我尝试使用时,在 Autowiring 包含@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 启动时查看了调试中的 BeanPostProcessor 列表,发现 MetricMethodBeanPostProcessor 的成本介于中间。

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

因此,问题是:如何正确地编写 BeanPostProcessor 进行日志记录,以免中断?

附言谷歌没有帮助

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

最佳答案

)

首先,正如 M. Deinum 所说,Proxy == AOP 会降低性能

当我运行我得到的代码时

*************************** APPLICATION FAILED TO START


Description:

The bean 'xxxxxxx' could not be injected as a 'com.spring.demo.demo.tasks.xxxxxx' because it is a JDK dynamic proxy that implements:

Action:

Consider injecting the bean as one of its interfaces or forcing the use of CGLib-based proxies by setting proxyTargetClass=true on @EnableAsync and/or @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();
}

关于java - 如何正确地从 BeanPostProcessor 创建代理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47304414/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com