AspectJ LTW没有在Tomcat中与Spring配置成功。

huangapple go评论88阅读模式
英文:

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 模块中的 ApplicationService。
m1 的子模块 m2。(m1 依赖于 m2)

m1 模块中的 aop.xml 文件位于 m1/WebContent/META-INF/aop.xml,内容如下:

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

<aspectj>
    <weaver>
        <!-- 只织入我们应用程序特定包中的类 -->
        <include within="m2.*"/>
    </weaver>

    <aspects>
        <!-- 织入这个切面 -->
        <aspect name="m2.security.FieldPermissionAspect"/>
    </aspects>
</aspectj>

m1 模块中的 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 />
	<!-- 启用加载时织入 -->
	<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'
...

我认为这是因为 Spring 仍然在我的切面中使用 Spring AOP 而不是 AspectJ。我在这里缺少什么?

英文:

I have followed the steps given in the following spring docs:
https://docs.spring.io/spring/docs/4.3.14.RELEASE/spring-framework-reference/html/aop.html#aop-aj-ltw

My project is a monolith with modules as such :

ApplicationService in module m1.
Child module m2 with parent m1.(m1 has a dependency on m2)

aop.xml file in m1/WebContent/META-INF/aop.xml as follows :

&lt;!DOCTYPE aspectj PUBLIC &quot;-//AspectJ//DTD//EN&quot; &quot;http://www.eclipse.org/aspectj/dtd/aspectj.dtd&quot;&gt;

&lt;aspectj&gt;    
    &lt;weaver&gt;
        &lt;!-- only weave classes in our application-specific packages --&gt;
        &lt;include within=&quot;m2.*&quot;/&gt;
    &lt;/weaver&gt;

&lt;aspects&gt;
    &lt;!-- weave in just this aspect --&gt;
    &lt;aspect name=&quot;m2.security.FieldPermissionAspect&quot;/&gt;
&lt;/aspects&gt;

</aspectj>

Application-context.xml file in m1/src/main/webapp/WEB-INF as follows :

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;beans xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
xmlns:mvc=&quot;http://www.springframework.org/schema/mvc&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xmlns:tx=&quot;http://www.springframework.org/schema/tx&quot; xmlns:task=&quot;http://www.springframework.org/schema/task&quot;
xmlns=&quot;http://www.springframework.org/schema/beans&quot; xmlns:aop=&quot;http://www.springframework.org/schema/aop&quot;
xsi:schemaLocation=&quot;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">
...

&lt;mvc:annotation-driven /&gt;
&lt;aop:aspectj-autoproxy /&gt;
&lt;task:annotation-driven /&gt;
&lt;!-- this switches on the load-time weaving --&gt;
&lt;context:load-time-weaver weaver-class=&quot;org.springframework.instrument.classloading.tomcat.TomcatLoadTimeWeaver&quot;/&gt;

My aspect in m2.security is as follows:

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(&quot;execution(public * *(..))&quot;)
    public void combinedPointcut() {}

    @Around(&quot;combinedPointcut()&quot;)
    public void aroundMapper(ProceedingJoinPoint joinPoint) {
        ...
    }

    @Around(&quot;cflow(combinedPointcut())&quot;)
    public void aroundSetter(ProceedingJoinPoint joinPoint) {
        ...
    }
}

AspectJ dependencies in pom.xml in m2:

&lt;dependencies&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.aspectj&lt;/groupId&gt;
        &lt;artifactId&gt;aspectjweaver&lt;/artifactId&gt;
        &lt;version&gt;1.8.7&lt;/version&gt;
        &lt;scope&gt;runtime&lt;/scope&gt;
    &lt;/dependency&gt;
    &lt;!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.aspectj&lt;/groupId&gt;
        &lt;artifactId&gt;aspectjrt&lt;/artifactId&gt;
        &lt;version&gt;1.8.7&lt;/version&gt;
    &lt;/dependency&gt;

&lt;/dependencies&gt;

When I run it in tomcat environment, I get the following error :

Caused by: org.aspectj.weaver.tools.UnsupportedPointcutPrimitiveException: Pointcut expression &#39;cflow(combinedPointcut())&#39; contains unsupported pointcut primitive &#39;cflow&#39;
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

I think it's because spring is still using Spring AOP instead of AspectJ in my aspect. What am I missing here?

答案1

得分: 3

如果您想使用AspectJ LTW而不是Spring AOP,您不应该使用Spring AOP配置,请删除&lt;aop:aspectj-autoproxy /&gt;。尽管名称中包含"AOP",但它与Spring AOP有关,而不是AspectJ。AspectJ不使用任何代理。

至于您的错误消息...

<!-- language: none -->

Caused by: org.aspectj.weaver.tools.UnsupportedPointcutPrimitiveException:
Pointcut expression 'cflow(combinedPointcut())'
包含不受支持的切点原语'cflow'

...这是因为您仍在使用Spring AOP,而没有使用AspectJ LTW。所以您可能有配置问题。如果您在Java命令行上使用以下方式启动容器,是否可以正常工作?

<!-- language: none -->

-javaagent:/path/to/aspectjweaver.jar

最后,但同样重要的是,就像我在您的上一个问题中已经说了三次,请在GitHub上提供一个MCVE,然后我可以分析您的问题。我真的无法根据您提供的片段信息做更多的猜测。所以请按照我的要求做,并帮助我帮助您。谢谢。

英文:

If you want to use AspectJ LTW instead of Spring AOP, you should not use Spring AOP configuration. so please get rid of &lt;aop:aspectj-autoproxy /&gt;. Despite the name it is about Spring AOP, not AspectJ. AspectJ does not use any proxies.

As for your error message, ...

<!-- language: none -->

Caused by: org.aspectj.weaver.tools.UnsupportedPointcutPrimitiveException:
  Pointcut expression &#39;cflow(combinedPointcut())&#39;
  contains unsupported pointcut primitive &#39;cflow&#39;

... it occurs because you are still using Spring AOP, AspectJ LTW is not used. So you are having a configuration issue. Does it work if you start your container with

<!-- language: none -->

-javaagent:/path/to/aspectjweaver.jar

on the Java command line?

Last, but not least, like I said 3x already in your previous question, please provide an MCVE on GitHub, then I can analyse your problem. I really cannot do more than speculate with the bits of information provided by you here. So please do what I asked you to and help me to help you. Thanks.

huangapple
  • 本文由 发表于 2020年1月3日 16:52:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575549.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定