AspectJ LTW 未在 Tomcat 中配置 Spring [英] AspectJ LTW not getting configured with Spring in Tomcat

查看:25
本文介绍了AspectJ LTW 未在 Tomcat 中配置 Spring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已按照以下 spring 文档中给出的步骤进行操作: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 :

模块 m1 中的 ApplicationService.子模块 m2 与父模块 m1.(m1 依赖于 m2)

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

m1/WebContent/META-INF/aop.xml中的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 配置.所以请去掉.尽管名称是关于 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 命令行上?

on the Java command line?

最后但并非最不重要的一点,就像我在您的上一个问题中已经说过的 3x 一样,请提供一个 MCVE 在 GitHub 上,然后我可以分析您的问题.我真的只能用你在这里提供的一些信息进行推测.所以请按照我的要求去做,并帮助我帮助你.谢谢.

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.

这篇关于AspectJ LTW 未在 Tomcat 中配置 Spring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆