简体   繁体   中英

AOP aspects in a multi-module Maven project

I have a multi-module Maven project and configured Spring AOP in one of my module. Unfortunately AOP only works for the project where it is. Here is my Maven config:

The parent pom.xml :

<parent>
    <artifactId>spring-boot-starter-parent</artifactId>
    <groupId>org.springframework.boot</groupId>
    <version>1.3.6.RELEASE</version>
</parent>

<modules>
    <module>rabbitmq</module>
    <module>rss_parser</module>
</modules>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
</dependencies>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

The first module (where my AOP config is):

 <groupId>com.rss.rabbitmq</groupId>
<artifactId>rabbitmq</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>rabbitmq</name>

<parent>
    <groupId>gpw.radar.rss</groupId>
    <artifactId>parent-module</artifactId>
    <version>1.0</version>
</parent>

<properties>
    <java.version>1.8</java.version>
    <rss.parser.version>1.0-SNAPSHOT</rss.parser.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.rss.parser</groupId>
        <artifactId>rss-parser</artifactId>
        <version>${rss.parser.version}</version>
    </dependency>
</dependencies>

The second module (where the AOP does not work):

<groupId>com.rss.parser</groupId>
<artifactId>rss-parser</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>rss_parser</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <junit.version>4.12</junit.version>
    <rometools.version>1.7.0</rometools.version>
    <jackson-datatype-jsr310.version>2.6.1</jackson-datatype-jsr310.version>
    <jackson-databind.version>2.6.6</jackson-databind.version>
</properties>

<parent>
    <groupId>gpw.radar.rss</groupId>
    <artifactId>parent-module</artifactId>
    <version>1.0</version>
</parent>

<dependencies>
    <dependency>
        <groupId>com.rometools</groupId>
        <artifactId>rome-fetcher</artifactId>
        <version>${rometools.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
        <version>${jackson-datatype-jsr310.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${jackson-databind.version}</version>
    </dependency>
</dependencies>

And finally my Spring AOP configuration from the first module:

@Pointcut("within(com.rss.rabbitmq.cron..*) || within(com.rss.rabbitmq.sender..*) || within(com.rss.parser..*)")
public void loggingPointcut() {
}

And I am using the pointcut in this aspect:

@Around("loggingPointcut()")
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
    // <<implementation>>
}

After some digging, I understand that it should work fine if I have myproject /jar in classpath, but in my case it does not work. The AOP for the first module, so for the "within(com.rss.rabbitmq.cron..*) || within(com.rss.rabbitmq.sender..*)" , works correctly but for the second project it does not.

Also I was trying to change the package name as the first parts are the same and I was thinking that it could cause the problem but it doesn't.

parent dependencies are not inherited properly, use dependencyManagement.

it is more clearly explained in documentation here

parent - pom.xml

<parent>
  <artifactId>spring-boot-starter-parent</artifactId>
  <groupId>org.springframework.boot</groupId>
  <version>1.3.6.RELEASE</version>
</parent>

<modules>
  <module>rabbitmq</module>
  <module>rss_parser</module>
</modules>

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
  </dependencies>
</dependencyManagement>

<build>
  <pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </pluginManagement>
</build>

first module - pom.xml

<groupId>com.rss.rabbitmq</groupId>
<artifactId>rabbitmq</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>rabbitmq</name>

<parent>
  <groupId>gpw.radar.rss</groupId>
  <artifactId>parent-module</artifactId>
  <version>1.0</version>
</parent>

<properties>
  <java.version>1.8</java.version>
  <rss.parser.version>1.0-SNAPSHOT</rss.parser.version>
</properties>

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
  </dependency>
  <dependency>
    <groupId>com.rss.parser</groupId>
    <artifactId>rss-parser</artifactId>
    <version>${rss.parser.version}</version>
  </dependency>
</dependencies>

second module - pom.xml

<groupId>com.rss.parser</groupId>
<artifactId>rss-parser</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>rss_parser</name>
<url>http://maven.apache.org</url>

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <maven.compiler.source>1.8</maven.compiler.source>
  <maven.compiler.target>1.8</maven.compiler.target>
  <junit.version>4.12</junit.version>
  <rometools.version>1.7.0</rometools.version>
  <jackson-datatype-jsr310.version>2.6.1</jackson-datatype-jsr310.version>
  <jackson-databind.version>2.6.6</jackson-databind.version>
</properties>

<parent>
  <groupId>gpw.radar.rss</groupId>
  <artifactId>parent-module</artifactId>
  <version>1.0</version>
</parent>

<dependencies>
  <dependency>
    <groupId>com.rometools</groupId>
    <artifactId>rome-fetcher</artifactId>
    <version>${rometools.version}</version>
  </dependency>
  <dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>${jackson-datatype-jsr310.version}</version>
  </dependency>
  <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>${jackson-databind.version}</version>
  </dependency>
</dependencies>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

Finally I found where was the problem. From the spring aop documentation:

If you only need to advise the execution of operations on Spring beans, then Spring AOP is the right choice. If you need to advise objects not managed by the Spring container (such as domain objects typically), then you will need to use AspectJ.

As my second module is not using the spring at all I should use the AspetJ instead of spring aspects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM