AspectJ和多模块设置Intellij

时间:2018-01-31 00:08:41

标签: java maven intellij-idea aspectj aspect

我一直处理这个问题一天。到目前为止,我有一个非常简单的项目,但仍然无法配置AspectJ

我的项目结构如下所示。

-- root   
---- DesktopGUI  
---- DataStore  
---- localsettings  
---- crosscuttingconcerns  

我已在AspectJ模块中定义了所有cross-cutting-concerns内容。

我的根pom.xml

<?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">
    <properties>
        <example.desktop.version>1.0-SNAPSHOT</example.desktop.version>
        <example.desktop.settings.version>1.0-SNAPSHOT</example.desktop.settings.version>
        <example.desktop.gui.version>1.0-SNAPSHOT</example.desktop.gui.version>
        <aspectj.version>1.8.13</aspectj.version>
    </properties>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example.solutions</groupId>
    <artifactId>example</artifactId>
    <packaging>pom</packaging>
    <version>${example.desktop.version}</version>

    <modules>
        <module>DesktopGUI</module>
        <module>DataStore</module>
        <module>localsettings</module>
        <module>crosscuttingconcerns</module>
    </modules>

</project>

DesktopGUI pom.xml

<?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>example</artifactId>
        <groupId>com.example.solutions</groupId>
        <version>${example.desktop.version}</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>desktop-gui</artifactId>
    <build>
        <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>
    </build>
    <properties>
        <mvvmfx.version>1.7.0</mvvmfx.version>
        <jfoenix.version>8.0.1</jfoenix.version>
        <jfxtrascontrols.version>8.0-r5</jfxtrascontrols.version>
        <fontawesomefx.version>8.9</fontawesomefx.version>
        <guice.version>4.1.0</guice.version>
    </properties>
    <!--Dependencies-->
    <dependencies>
        <!--DI, IoC Frameworks Dependencies-->
        <dependency>
            <groupId>com.google.inject.extensions</groupId>
            <artifactId>guice-grapher</artifactId>
            <version>${guice.version}</version>
        </dependency>
        <dependency>
            <groupId>com.google.inject</groupId>
            <artifactId>guice</artifactId>
            <version>${guice.version}</version>
        </dependency>
        <dependency>
            <groupId>de.saxsys</groupId>
            <artifactId>mvvmfx</artifactId>
            <version>${mvvmfx.version}</version>
        </dependency>
        <!--UI Dependencies-->
        <dependency>
            <groupId>com.jfoenix</groupId>
            <artifactId>jfoenix</artifactId>
            <version>${jfoenix.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jfxtras</groupId>
            <artifactId>jfxtras-controls</artifactId>
            <version>${jfxtrascontrols.version}</version>
        </dependency>
        <dependency>
            <groupId>de.jensd</groupId>
            <artifactId>fontawesomefx</artifactId>
            <version>${fontawesomefx.version}</version>
        </dependency>
        <dependency>
            <groupId>de.saxsys</groupId>
            <artifactId>mvvmfx-guice</artifactId>
            <version>${mvvmfx.version}</version>
        </dependency>

        <!--Local project dependencies-->
        <dependency>
            <groupId>com.example.solutions</groupId>
            <artifactId>local-settings</artifactId>
            <version>${example.desktop.settings.version}</version>
        </dependency>
        <dependency>
            <groupId>com.example.solutions</groupId>
            <artifactId>cross-cutting-concerns</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>


</project> 

交叉关注问题pom.xml

<?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>sto</artifactId>
        <groupId>com.example.solutions</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <properties>
        <aspectj.version>1.8.13</aspectj.version>
        <slf4f.version>1.8.0-beta0</slf4f.version>
    </properties>
    <artifactId>cross-cutting-concerns</artifactId>
    <dependencies>
        <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>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4f.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4f.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.21</version>
        </dependency>

    </dependencies>
</project>

我已将我的方面定义如下

@Aspect
public class MethodLogger {
    private static Logger logger = LoggerFactory.getLogger(MethodLogger.class);

    @Pointcut("execution(* *(..)) && @annotation(com.example.solutions.example.crosscuttingconcerns.annotation.LogMethodCall)")
    public void logPointCut() {
    }

    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) {
        try {
            long start = System.currentTimeMillis();
            Object result = point.proceed();
            System.out.println("HELLOO");
            logger.info("#%s(%s): %s in %[msec]s",
                    MethodSignature.class.cast(point.getSignature()).getMethod().getName(),
                    point.getArgs(),
                    result,
                    System.currentTimeMillis() - start
            );
            return result;
        } catch (Throwable ex) {
            return null;
        }
    }
}

并注释了我的方法

@Override
@LogMethodCall
public void startMvvmfx(Stage stage) throws Exception {
    stage.setTitle(internalizationManager.getStringValue("titles", "main_app"));
    stage.show();
    stage.centerOnScreen();
}

此方法在DesktopGUI模块

中定义

注释定义如此

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogMethodCall {
}

它编译没有错误,但有警告

Warning:(21, 0) ajc: advice defined in com.example.solutions.example.crosscuttingconcerns.aspect.MethodLogger has not been applied [Xlint:adviceDidNotMatch]

我试图将ApsectJ方面添加到我的模块中,但这仍然没有帮助。

我认为问题可能是由多模块项目结构引起的。

请帮助解决这个问题,我没有想法有什么不妥。

我已将Java Compiler设置为Ajc

enter image description here

由于

1 个答案:

答案 0 :(得分:0)

我在POM中看不到AspectJ Maven plugin的痕迹,即没有使用AspectJ编译器,因此您永远无法在项目中使用AOP。假设您要使用编译时编织,则需要两个模块中的插件。在方面库模块中,您需要它来正确编译方面,并在您需要的其他模块中编译方面,以便将方面编织到您的核心代码中referring to the aspect library

对于更复杂的多模块设置,请参阅here,但IMO对于您的简单项目不需要它。

有关完整的示例项目,请参阅my other answer。安装了AspectJ扩展的IDEA和安装了AJDT的Eclipse导入Maven设置完美无缺,并且能够从Maven和IDE构建和运行项目。