gpt4 book ai didi

java - 错误无法注册 'com.temp.request.util.aspect.TraceLogger',因为使用该名称找到的类型不是方面

转载 作者:行者123 更新时间:2023-11-30 05:30:19 25 4
gpt4 key购买 nike

我正在使用面向方面的编程来分离项目中的日志记录问题,但在部署时我在部署应用程序时在 tomcat 中遇到以下错误

error Cannot register 'com.temp.request.util.aspect.TraceLogger' because the type found with that name is not an aspect

TraceLogger类如下

package com.temp.request.util.aspect;

import java.util.Arrays;
import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TraceLogger {

public Object logMethodEntryAndExit(ProceedingJoinPoint joinPoint) throws Throwable {
LOG.trace(joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "() begins with " + Arrays.toString(joinPoint.getArgs()));
Object result = joinPoint.proceed();
LOG.trace(joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "() ends with " + result);
return result;
}
}

我已在 spring.xml 文件中声明了切面和 TraceLogger bean,如下所示

<bean id="TraceLogger" class="com.temp.request.util.aspect.TraceLogger" />

<aop:config>
<aop:aspect id="methodLogger" ref="TraceLogger">
<aop:pointcut id="logMethod"
expression="execution(* com.temp.request..*.*(..)) and !execution(* com.temp.request.listener.*.*(..)) and !execution(* com.temp.request.ws.*.*(..)) and !execution(* .util.*.*(..))" />
<aop:around pointcut-ref="logMethod" method="logMethodEntryAndExit" />
</aop:aspect>
</aop:config>

dependency.gradle 中声明的依赖关系如下

compile (
'org.aspectj:aspectjrt:1.8.4',
'org.aspectj:aspectjweaver:1.8.4',
'org.springframework:spring-instrument-tomcat:4.0.9.RELEASE'
)

使用加载时编织并在 spring.xml 中指定以下行,如下所示

 <context:load-time-weaver aspectj-weaving="on" weaver-class="org.springframework.instrument.classloading.tomcat.TomcatLoadTimeWeaver" />

通过在web.xml中指定contextConfigLocation来加载spring.xml,如下

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring.xml</param-value>
</context-param>

通过在位于项目 META-INF 文件夹中的 aop.xml 中指定方面名称来加载方面,如下所示

<aspectj>
<weaver options="-Xset:weaveJavaxPackages=true">
<include within="javax.jms.*"/>
</weaver>
<aspects>
<aspect name="com.temp.request.util.aspect.TraceLogger" />
</aspects>
</aspectj>

为什么 tomcat 在执行加载时间编织时无法找到 TraceLogger 方面?

最佳答案

我认为您在这里遇到了概念问题:

  • 一方面,您希望通过 <aop:config> 将普通 POJO 类注册为 Spring AOP 方面.
  • 另一方面,您希望通过 aop.xml 使用 AspectJ 加载时编织。

请注意,AspectJ 与 Spring AOP 不同,您应该决定使用两者中的哪一个。我建议暂时坚持使用 Spring AOP,放弃所有 LTW 的东西。

查看您的 Spring AOP 方面声明 <aop:aspect id="methodLogger" ref="TraceLogger">以及您收到的错误消息,我认为您忘记声明一个名为“TraceLogger”的 Spring bean。如果切面不是 Spring bean/组件,则不能将其用作切面。

我强烈推荐阅读Spring AOP manual .

如果您达到了 Spring AOP 的极限并想要探索更强大的 AspectJ,还有一章介绍如何配置 AspectJ via LTW 。请注意,AspectJ 完全独立于 Spring,您可以在 Spring 项目内部或您喜欢的任何 POJO 或非 Spring 容器应用程序中使用它,甚至适用于 Java 以外的 JVM 语言。

关于java - 错误无法注册 'com.temp.request.util.aspect.TraceLogger',因为使用该名称找到的类型不是方面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57693020/

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