Spring AOP,从依赖项导入的方面未执行

时间:2018-03-27 21:49:16

标签: java spring-boot aop aspectj spring-aop

我正在尝试在库模块中定义方面,并在另一个模块中的SpringBoot服务上“使用”它们。以下是项目结构。 enter image description here 我已经实现了两个单独的方面,一个在库模块内,另一个在服务模块中

authors

以下是库POM

package it.cattanisimone.java.mvn.aspectj.controller;

public aspect LocalAspect {

    public pointcut traced(): execution(* *.*(..));

    before() : traced() {
        System.out.println("local");
    }

}


package it.cattanisimone.java.mvn.aspectjlibrary;

public aspect ImportedAspect {

    public pointcut traced(): execution(* *.*(..));

    before() : traced() {
        System.out.println("imported");
    }

}

服务一个

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>java-mvn</artifactId>
        <groupId>it.cattanisimone</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>it.cattanisimone</groupId>
    <artifactId>java-mvn-aspectj-library</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.13</version>
        </dependency>
    </dependencies>

</project>

运行服务并调用控制器提供的简单API,只应用LocalAspect

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>java-mvn</artifactId>
        <groupId>it.cattanisimone</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>it.cattanisimone</groupId>
    <artifactId>java-mvn-aspectj</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>

    <dependencies>

        <dependency>
            <groupId>it.cattanisimone</groupId>
            <artifactId>java-mvn-aspectj-library</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.4.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
            <version>1.4.0.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.4.0.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <addResources>true</addResources>
                    <mainClass>it.cattanisimone.java.mvn.aspectj.AspectServer</mainClass>
                </configuration>

                <dependencies>
                    <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <version>1.4.0.RELEASE</version>
                    </dependency>

                </dependencies>
            </plugin>

            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>it.cattanisimone.java.mvn.aspectj.AspectServer</mainClass>
                        </manifest>
                    </archive>
                </configuration>

                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-jar-plugin</artifactId>
                        <version>2.4</version>
                    </dependency>

                    <dependency>
                        <groupId>org.codehaus.plexus</groupId>
                        <artifactId>plexus-io</artifactId>
                        <version>2.0.2</version>
                    </dependency>

                </dependencies>
            </plugin>

        </plugins>

    </build>

</project>

我也尝试过基于注释的语法。相同的结果

0 个答案:

没有答案