简体   繁体   English

多模块Maven项目中的AOP方面

[英]AOP aspects in a multi-module Maven project

I have a multi-module Maven project and configured Spring AOP in one of my module. 我有一个多模块Maven项目,并在我的一个模块中配置了Spring AOP。 Unfortunately AOP only works for the project where it is. 不幸的是,AOP只适用于它所在的项目。 Here is my Maven config: 这是我的Maven配置:

The parent pom.xml : 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): 第一个模块(我的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>

The second module (where the AOP does not work): 第二个模块(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>

And finally my Spring AOP configuration from the first module: 最后我从第一个模块的Spring AOP配置:

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

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. 父依赖项未正确继承,请使用dependencyManagement。

it is more clearly explained in documentation here 这里的文档中更清楚地解释了它

parent - pom.xml 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 第一个模块 - 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 第二个模块 - 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: 从春天aop文档:

If you only need to advise the execution of operations on Spring beans, then Spring AOP is the right choice. 如果您只需要建议在Spring bean上执行操作,那么Spring AOP是正确的选择。 If you need to advise objects not managed by the Spring container (such as domain objects typically), then you will need to use AspectJ. 如果您需要建议不由Spring容器管理的对象(通常是域对象),那么您将需要使用AspectJ。

As my second module is not using the spring at all I should use the AspetJ instead of spring aspects. 由于我的第二个模块根本没有使用弹簧,我应该使用AspetJ而不是弹簧方面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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