英文:
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 :
<!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>
</aspectj>
Application-context.xml file in m1/src/main/webapp/WEB-INF as follows :
<?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"/>
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("execution(public * *(..))")
public void combinedPointcut() {}
@Around("combinedPointcut()")
public void aroundMapper(ProceedingJoinPoint joinPoint) {
...
}
@Around("cflow(combinedPointcut())")
public void aroundSetter(ProceedingJoinPoint joinPoint) {
...
}
}
AspectJ dependencies in pom.xml in m2:
<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>
When I run it in tomcat environment, I get the following error :
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
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配置,请删除<aop:aspectj-autoproxy />
。尽管名称中包含"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 <aop:aspectj-autoproxy />
. 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 'cflow(combinedPointcut())'
contains unsupported pointcut primitive 'cflow'
... 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论