在Tomcat中没有用Spring配置AspectJ LTW [英] AspectJ LTW not getting configured with Spring in Tomcat
问题描述
我遵循了以下春季文档中给出的步骤: https://docs.spring.io/spring/docs/4.3.14.RELEASE/spring-framework-reference/html/aop.html#aop-aj-ltw
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. 子模块m2和父级m1.(m1对m2有依赖性)
ApplicationService in module m1. Child module m2 with parent m1.(m1 has a dependency on m2)
aop.xml文件如下:
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>
m1/src/main/webapp/WEB-INF中的Application-context.xml文件如下:
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"/>
我在m2.security中的方面如下:
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) {
...
}
}
m2中pom.xml中的AspectJ依赖项:
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>
在tomcat环境中运行它时,出现以下错误:
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
我认为这是因为spring在我的方面仍在使用Spring AOP而不是AspectJ.我在这里想念什么?
I think it's because spring is still using Spring AOP instead of AspectJ in my aspect. What am I missing here?
推荐答案
如果要使用AspectJ LTW而不是Spring AOP,则不应使用Spring AOP配置.所以请摆脱<aop:aspectj-autoproxy />
.尽管名称是关于Spring AOP,而不是AspectJ. AspectJ不使用任何代理.
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, ...
Caused by: org.aspectj.weaver.tools.UnsupportedPointcutPrimitiveException:
Pointcut expression 'cflow(combinedPointcut())'
contains unsupported pointcut primitive 'cflow'
...因为您仍在使用Spring AOP而发生,所以没有使用AspectJ LTW.因此,您遇到了配置问题.如果您使用以下容器启动容器,它会起作用吗?
... 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
-javaagent:/path/to/aspectjweaver.jar
在Java命令行上?
最后但并非最不重要的一点,就像我在您的上一个问题中已经说过3倍一样,请提供一个MCVE ,那么我可以分析您的问题.除了推测您在此处提供的信息外,我真的无能为力.因此,请按照我的要求做,并帮助我为您提供帮助.谢谢.
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.
这篇关于在Tomcat中没有用Spring配置AspectJ LTW的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!