Java 如果在sprig引导项目中添加@EnableTransactionManagement,则@Aspectj日志记录存在问题

Java 如果在sprig引导项目中添加@EnableTransactionManagement,则@Aspectj日志记录存在问题,java,spring-boot,aop,aspectj,spring-aop,Java,Spring Boot,Aop,Aspectj,Spring Aop,我尝试使用@Aspect记录所有请求和响应。代码运行良好,使用一个简单的spring boot项目,使用公共配置,并且没有任何init方法或DB调用 下面的代码不起作用,其中包括init配置和DB调用,用于运行我的应用程序时使用@EnableTransactionManagement获取所需数据时出错 import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.

我尝试使用
@Aspect
记录所有请求和响应。代码运行良好,使用一个简单的
spring boot
项目,使用公共配置,并且没有任何init方法或DB调用

下面的代码不起作用,其中包括init配置和DB调用,用于运行我的应用程序时使用
@EnableTransactionManagement
获取所需数据时出错

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
public class MyContextClass implements ApplicationListener<ContextRefreshedEvent>{

@Autowire
private MyServiceRepository myServiceRepo;
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
          // Added Db call
          CommonUtils.setYears(myServiceRepo.getTotalYears());
    }
}
这段代码构建成功。但是当我运行下面的应用程序时,错误显示在setYears中

2021-05-07 20:14:58 [restartedMain] ERROR o.s.boot.SpringApplication-[SpringApplication.java:856]-Application run failed
java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
    at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)
    at org.springframework.web.context.support.WebApplicationContextUtils.currentRequestAttributes(WebApplicationContextUtils.java:313)
    at org.springframework.web.context.support.WebApplicationContextUtils.access$400(WebApplicationContextUtils.java:66)
    at org.springframework.web.context.support.WebApplicationContextUtils$RequestObjectFactory.getObject(WebApplicationContextUtils.java:329)
    at org.springframework.web.context.support.WebApplicationContextUtils$RequestObjectFactory.getObject(WebApplicationContextUtils.java:324)
    at org.springframework.beans.factory.support.AutowireUtils$ObjectFactoryDelegatingInvocationHandler.invoke(AutowireUtils.java:292)
    at com.sun.proxy.$Proxy120.getRequestURL(Unknown Source)
    at com.opl.ans.config.utils.LoggingAspect.logAround(LoggingAspect.java:137)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:634)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:624)
    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:72)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691)


这样做对吗?有什么建议吗?请提供帮助。

根据错误堆栈,问题在于
请求
实例无效。我猜自动连接到
方面的
请求
实例可能已经过时,或者与当前线程没有关联。这意味着
request
实例不为null,并且
logAround()
方法中的检查会给出未预期的结果

如果当前没有请求属性绑定到线程,则返回
null
。修改

if(request!=null){..}
检查
if(RequestContextHolder.getRequestAttributes()!=null){..}
应该可以解决这个问题

此外,SpringBoot还为您当前通过AOP尝试的内容提供了开箱即用的解决方案。也一定要检查一下


logAround()
advice中,将条件修改为
RequestContextHolder.getRequestAttributes()!=空
而不是
请求!=空
。在控件返回之前记录
响应
也不是正确的方法。请查看和。如果没有进一步的问题,并且问题已得到解决,我可以更新答案,您可以关闭问题此解决方案正在运行,但如果我从DB进行事务处理,则会出现一个问题。我的实体类为
@ToString
lombok``java.lang.StackOverflower错误:null位于java.lang.Long.ToString(Long.java:396)位于java.lang.Long.ToString(Long.java:1032)在java.lang.String.valueOf(String.java:2994)在java.lang.StringBuilder.append(StringBuilder.java:131)中,``请通读一遍,我没有意识到您将我的答案标记为非答案。您当前的问题与lombok相关,与Aspect或我共享的解决方案无关。好的,我会将此标记为已完成,并为此lombok相关问题创建另一个。您的问题的解决方案是排除我在分享的答案中指出的toString()。祝你好运
2021-05-07 20:14:58 [restartedMain] ERROR o.s.boot.SpringApplication-[SpringApplication.java:856]-Application run failed
java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
    at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)
    at org.springframework.web.context.support.WebApplicationContextUtils.currentRequestAttributes(WebApplicationContextUtils.java:313)
    at org.springframework.web.context.support.WebApplicationContextUtils.access$400(WebApplicationContextUtils.java:66)
    at org.springframework.web.context.support.WebApplicationContextUtils$RequestObjectFactory.getObject(WebApplicationContextUtils.java:329)
    at org.springframework.web.context.support.WebApplicationContextUtils$RequestObjectFactory.getObject(WebApplicationContextUtils.java:324)
    at org.springframework.beans.factory.support.AutowireUtils$ObjectFactoryDelegatingInvocationHandler.invoke(AutowireUtils.java:292)
    at com.sun.proxy.$Proxy120.getRequestURL(Unknown Source)
    at com.opl.ans.config.utils.LoggingAspect.logAround(LoggingAspect.java:137)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:634)
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:624)
    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:72)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691)