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

Java 错误无法注册';com.temp.request.util.aspect.TraceLogger';因为使用该名称找到的类型不是方面,java,aop,aspectj,spring-aop,Java,Aop,Aspectj,Spring Aop,我在项目中使用面向方面编程来分离日志记录问题,但在部署应用程序时,我在tomcat中遇到以下错误 错误无法注册'com.temp.request.util.aspect.TraceLogger' 因为使用该名称找到的类型不是方面 TraceLogger类如下所示 package com.temp.request.util.aspect; import java.util.Arrays; import org.aspectj.lang.ProceedingJoinPoint; import or

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

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

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;
            }
        }
<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>
compile (
'org.aspectj:aspectjrt:1.8.4',
'org.aspectj:aspectjweaver:1.8.4',
'org.springframework:spring-instrument-tomcat:4.0.9.RELEASE'
)
 <context:load-time-weaver aspectj-weaving="on" weaver-class="org.springframework.instrument.classloading.tomcat.TomcatLoadTimeWeaver" />
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring.xml</param-value>
</context-param>
<aspectj>
    <weaver options="-Xset:weaveJavaxPackages=true">        
        <include within="javax.jms.*"/>
    </weaver>
    <aspects>
        <aspect name="com.temp.request.util.aspect.TraceLogger" />
    </aspects>
</aspectj>
我在spring.xml文件中声明了aspect和TraceLogger bean,如下所示

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;
            }
        }
<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>
compile (
'org.aspectj:aspectjrt:1.8.4',
'org.aspectj:aspectjweaver:1.8.4',
'org.springframework:spring-instrument-tomcat:4.0.9.RELEASE'
)
 <context:load-time-weaver aspectj-weaving="on" weaver-class="org.springframework.instrument.classloading.tomcat.TomcatLoadTimeWeaver" />
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring.xml</param-value>
</context-param>
<aspectj>
    <weaver options="-Xset:weaveJavaxPackages=true">        
        <include within="javax.jms.*"/>
    </weaver>
    <aspects>
        <aspect name="com.temp.request.util.aspect.TraceLogger" />
    </aspects>
</aspectj>
使用加载时编织,并在spring.xml中指定以下行,如下所示

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;
            }
        }
<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>
compile (
'org.aspectj:aspectjrt:1.8.4',
'org.aspectj:aspectjweaver:1.8.4',
'org.springframework:spring-instrument-tomcat:4.0.9.RELEASE'
)
 <context:load-time-weaver aspectj-weaving="on" weaver-class="org.springframework.instrument.classloading.tomcat.TomcatLoadTimeWeaver" />
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring.xml</param-value>
</context-param>
<aspectj>
    <weaver options="-Xset:weaveJavaxPackages=true">        
        <include within="javax.jms.*"/>
    </weaver>
    <aspects>
        <aspect name="com.temp.request.util.aspect.TraceLogger" />
    </aspects>
</aspectj>

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

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;
            }
        }
<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>
compile (
'org.aspectj:aspectjrt:1.8.4',
'org.aspectj:aspectjweaver:1.8.4',
'org.springframework:spring-instrument-tomcat:4.0.9.RELEASE'
)
 <context:load-time-weaver aspectj-weaving="on" weaver-class="org.springframework.instrument.classloading.tomcat.TomcatLoadTimeWeaver" />
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring.xml</param-value>
</context-param>
<aspectj>
    <weaver options="-Xset:weaveJavaxPackages=true">        
        <include within="javax.jms.*"/>
    </weaver>
    <aspects>
        <aspect name="com.temp.request.util.aspect.TraceLogger" />
    </aspects>
</aspectj>

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

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;
            }
        }
<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>
compile (
'org.aspectj:aspectjrt:1.8.4',
'org.aspectj:aspectjweaver:1.8.4',
'org.springframework:spring-instrument-tomcat:4.0.9.RELEASE'
)
 <context:load-time-weaver aspectj-weaving="on" weaver-class="org.springframework.instrument.classloading.tomcat.TomcatLoadTimeWeaver" />
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring.xml</param-value>
</context-param>
<aspectj>
    <weaver options="-Xset:weaveJavaxPackages=true">        
        <include within="javax.jms.*"/>
    </weaver>
    <aspects>
        <aspect name="com.temp.request.util.aspect.TraceLogger" />
    </aspects>
</aspectj>


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

我认为您在这里遇到了一个概念上的问题:

  • 一方面,您希望通过
    将普通POJO类注册为SpringAOP方面


    如果您曾经达到SpringAOP的极限,并且想要探索更强大的AspectJ,那么还有一章介绍如何配置。请注意,AspectJ完全独立于Spring,您可以在Spring项目中使用它,也可以在您喜欢的任何POJO或非Spring容器应用程序中使用它,即使是Java以外的JVM语言。

    我没有忘记声明Spring bean TraceLogger。它已经存在于我的spring.xml中。更新了问题我是AspectJ的人,不是Spring用户。但是,在spring.xml中声明方面的方法是可行的,即使不将它们注释为
    @Aspect
    @Component
    ,这是目前与组件扫描相关的标准方法。如果这些注释有用,您仍然可以尝试,但这只是一个猜测。不过我还是认为你应该去掉aop.xml之类的东西。你的目标是什么?拦截Spring组件或非Spring内容?在项目中,这一行出现在Spring.xml中,所以我猜加载时编织用于事务管理为什么即使我在Spring.xml中将TraceLogger指定为bean,它也会给出此错误?我应该在TraceLogger类中加入@Aspect注释吗?你可以尝试一下
    @Aspect
    ,看看你是否幸运。否则,最简单的方法是将您的问题提取到一个复制有问题行为并将其发布到GitHub上,然后其他人可以查看。