如何在基于Spring的应用程序上使用所有AspectJ切入点指示符?

时间:2017-06-21 13:13:43

标签: java spring aop aspectj spring-aop

我有一个由Maven管理的Spring项目。而不是使用execution切入点指示符,如下所示:

@Before("execution(* my.app.TestUtil.myStaticClass(..))")
public void logBeforeTestUtil(JoinPoint joinPoint) {
    System.out.println("*** hijacked : " + joinPoint.getSignature().getName());
}

我想使用call切入点指示符,如:

@Before("call(* my.app.TestUtil.myStaticClass(..))")
public void logBeforeTestUtil(JoinPoint joinPoint) {
    System.out.println("*** hijacked : " + joinPoint.getSignature().getName());
}

但是,即使我在call pointcut designator isn't supported by Spring中删除了spring-aop,我的IDE(IntelliJ)也会告诉我pom.xml

我想提一下上面的例子,execution切入点指示符正在运行,Spring AOP does not supports all AspectJ pointcut designators

This answer显示了如何在不使用Spring的情况下使用加载时间编织。我想知道我们是否可以在没有Spring AOP的情况下使用Spring + AspectJ。如果是的话,该怎么做?

编辑:添加pom.xml和上下文配置文件

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>my.app</groupId>
    <artifactId>myapp</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <spring.version>4.3.4.RELEASE</spring.version>
        <jackson.version>2.7.3</jackson.version>
        <aspectj.version>1.8.4</aspectj.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <aspectj.version>1.8.4</aspectj.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--<dependency>-->
            <!--<groupId>org.springframework</groupId>-->
            <!--<artifactId>spring-aop</artifactId>-->
            <!--<version>${spring.version}</version>-->
        <!--</dependency>-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-instrument</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>
</project>

应用context.xml中

package my.app;

import org.springframework.context.annotation.*;

@Configuration
@EnableLoadTimeWeaving(aspectjWeaving = EnableLoadTimeWeaving.AspectJWeaving.ENABLED)
public class AppConfig {
}

META-INF / aop.xml文件

<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
    <weaver>
        <include within="my.app.utils.*"/>
        <include within="my.app.aspect.*"/>
    </weaver>
    <aspects>
        <aspect name="my.app.aspect.LoggingAspect" />
    </aspects>
</aspectj>

1 个答案:

答案 0 :(得分:1)

对于execution,必须建议名为的代码。对于call,必须建议调用代码。 调用代码是否在my.app.utils.*my.app.aspect.*范围内?这些是您配置织工的唯一包。如果调用者来自org.springframework ...或您的应用程序的其他部分,则不会建议它。

转换为之前评论的回答。