Tomcat 中的 Spring 未配置 AspectJ LTW

AspectJ LTW not getting configured with Spring in Tomcat

我已按照以下 spring 文档中给出的步骤进行操作: https://docs.spring.io/spring/docs/4.3.14.RELEASE/spring-framework-reference/html/aop.html#aop-aj-ltw

我的项目是一个带有模块的单体:

模块 m1 中的应用程序服务。 具有父模块 m1 的子模块 m2。(m1 依赖于 m2)

aop.xml 文件在 m1/WebContent/META-INF/aop.xml 如下:

<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">

<aspectj>    
    <weaver>
        <!-- only weave classes in our application-specific packages -->
        <include within="m2.*"/>
    </weaver>

<aspects>
    <!-- weave in just this aspect -->
    <aspect name="m2.security.FieldPermissionAspect"/>
</aspects>

Application-context.xml 文件在 m1/src/main/webapp/WEB-INF 如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
...

<mvc:annotation-driven />
<aop:aspectj-autoproxy />
<task:annotation-driven />
<!-- this switches on the load-time weaving -->
<context:load-time-weaver weaver-class="org.springframework.instrument.classloading.tomcat.TomcatLoadTimeWeaver"/>

我在m2.security的方面如下:

package m2.security;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;


@Aspect
@Component
public class FieldPermissionAspect {


    @Pointcut("execution(public * *(..))")
    public void combinedPointcut() {}

    @Around("combinedPointcut()")
    public void aroundMapper(ProceedingJoinPoint joinPoint) {
        ...
    }

    @Around("cflow(combinedPointcut())")
    public void aroundSetter(ProceedingJoinPoint joinPoint) {
        ...
    }
}

m2 中 pom.xml 中的 AspectJ 依赖项:

<dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.7</version>
        <scope>runtime</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.7</version>
    </dependency>

</dependencies>

当我在 tomcat 环境中 运行 时,出现以下错误:

Caused by: org.aspectj.weaver.tools.UnsupportedPointcutPrimitiveException: Pointcut expression 'cflow(combinedPointcut())' contains unsupported pointcut primitive 'cflow'
at org.aspectj.weaver.tools.PointcutParser.validateAgainstSupportedPrimitives(PointcutParser.java:425)
at org.aspectj.weaver.tools.PointcutParser.resolvePointcutExpression(PointcutParser.java:311)
at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:294)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:207)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.checkReadyToMatch(AspectJExpressionPointcut.java:193)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.getClassFilter(AspectJExpressionPointcut.java:170)
at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:208)
at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:262)
at org.springframework.aop.support.AopUtils.findAdvisorsThatCanApply(AopUtils.java:294)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findAdvisorsThatCanApply(AbstractAdvisorAutoProxyCreator.java:118)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findEligibleAdvisors(AbstractAdvisorAutoProxyCreator.java:88)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(AbstractAdvisorAutoProxyCreator.java:69)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:330)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:293)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:422)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1577)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
... 66 more

我认为这是因为 spring 在我的方面仍在使用 Spring AOP 而不是 AspectJ。我在这里错过了什么?

如果您想使用 AspectJ LTW 而不是 Spring AOP,则不应使用 Spring AOP 配置。所以请去掉 <aop:aspectj-autoproxy />。尽管名称是关于 Spring AOP,而不是 AspectJ。 AspectJ 不使用任何代理。

至于你的错误信息,...

Caused by: org.aspectj.weaver.tools.UnsupportedPointcutPrimitiveException:
  Pointcut expression 'cflow(combinedPointcut())'
  contains unsupported pointcut primitive 'cflow'

... 出现这种情况是因为您仍在使用 Spring AOP,未使用 AspectJ LTW。所以你遇到了配置问题。如果你用

启动你的容器,它会工作吗
-javaagent:/path/to/aspectjweaver.jar

在 Java 命令行上?

最后但同样重要的是,就像我在 GitHub 上的 , please provide an MCVE 中所说的 3x,那么我可以分析您的问题。我真的只能用你在这里提供的信息来推测。所以请按照我的要求去做,帮助我帮助你。谢谢