我遵循了以下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 / WebContent / META-INF / aop.xml中的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>
m1 / src / main / webapp / WEB-INF中的Application-context.xml文件如下:
<?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不使用任何代理。
<aop:aspectj-autoproxy/>
至于您的错误消息,…
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命令行上?
最后但并非最不重要的一点,就像我在上一个问题中已经说过3x一样,请在GitHub上提供一个MCVE,这样我就可以分析您的问题了。除了推测您在此处提供的信息外,我真的不能做其他事情。因此,请按照我的要求做,并帮助我为您提供帮助。谢谢。