【问题标题】:AspectJ and multi module setup IntellijAspectJ 和多模块设置 Intellij
【发布时间】:2018-01-31 00:08:40
【问题描述】:

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

我的项目结构如下。

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

我已经在cross-cutting-concerns 模块中定义了所有AspectJ 的东西。

我的根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>

桌面GUI 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

谢谢

【问题讨论】:

    标签: java maven intellij-idea aspectj aspectj-maven-plugin


    【解决方案1】:

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

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

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

    【讨论】:

    • 感谢您的回答,我已将java编译器设置为ajc,我是否也应该使用AspectJ maven插件?
    • 不是,而是相反。所以请不要更改 Maven 编译器插件设置,而是使用 AspectJ Maven 插件。这就是它的目的。 Ajc 有这么多特殊的命令行设置,通过 Maven 编译器(如果可能的话)配置它们会非常乏味且不舒服。
    猜你喜欢
    • 1970-01-01
    • 2017-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-17
    • 2013-04-26
    相关资源
    最近更新 更多