多模块Maven项目中的AOP方面

时间:2016-08-20 15:07:23

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

我有一个多模块Maven项目,并在我的一个模块中配置了Spring AOP。不幸的是,AOP只适用于它所在的项目。这是我的Maven配置:

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>

第一个模块(我的AOP配置所在的位置):

 <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>

第二个模块(AOP不起作用):

<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>

最后我从第一个模块开始进行Spring AOP配置:

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

我正在使用这方面的切入点:

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

经过一番挖掘,我明白如果我在classpath中有myproject / jar它应该可以正常工作,但在我的情况下它不起作用。第一个模块的AOP,对于"within(com.rss.rabbitmq.cron..*) || within(com.rss.rabbitmq.sender..*)",可以正常工作,但对于第二个项目则没有。

此外,我试图更改包名称,因为第一部分是相同的,我认为它可能会导致问题,但它不会。

2 个答案:

答案 0 :(得分:0)

父依赖项未正确继承,请使用dependencyManagement。

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>

第一个模块--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>

第二个模块 - 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>

答案 1 :(得分:0)

最后我发现了问题所在。从春天aop文档:

  

如果您只需要建议在Spring上执行操作   豆子,那么Spring AOP是正确的选择。如果你需要建议   不受Spring容器管理的对象(例如域对象)   通常),然后你需要使用AspectJ。

由于我的第二个模块根本没有使用弹簧,我应该使用AspetJ而不是弹簧方面。