首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AspectJ和多模块设置

AspectJ和多模块设置
EN

Stack Overflow用户
提问于 2018-01-31 08:08:41
回答 1查看 695关注 0票数 0

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

我的项目结构如下所示。

代码语言:javascript
复制
-- root   
---- DesktopGUI  
---- DataStore  
---- localsettings  
---- crosscuttingconcerns  

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

我的根pom.xml

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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>

我已经将我的方面定义如下

代码语言:javascript
复制
@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;
        }
    }
}

并像这样注释了我的方法

代码语言:javascript
复制
@Override
@LogMethodCall
public void startMvvmfx(Stage stage) throws Exception {
    stage.setTitle(internalizationManager.getStringValue("titles", "main_app"));
    stage.show();
    stage.centerOnScreen();
}

此方法在DesktopGUI模块中定义

这样定义的注解

代码语言:javascript
复制
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogMethodCall {
}

它编译时没有错误,但有一个警告。

代码语言:javascript
复制
Warning:(21, 0) ajc: advice defined in com.example.solutions.example.crosscuttingconcerns.aspect.MethodLogger has not been applied [Xlint:adviceDidNotMatch]

我已经尝试将ApsectJ facet添加到我的模块中,但这仍然没有帮助。

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

请帮我解决这个问题,我不知道出了什么问题。

我还将Java Compiler设置为Ajc

谢谢

EN

回答 1

Stack Overflow用户

发布于 2018-01-31 10:13:56

我在你的POMs中看不到AspectJ Maven plugin的踪迹,即没有使用AspectJ编译器,因此你永远不能在你的项目中使用面向方面编程。假设您想要使用编译时织入,那么在两个模块中都需要这个插件。在方面库模块中,您需要它来正确地编译方面,而在其他模块中,您需要它来将方面编织到您的核心代码referring to the aspect library中。

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48532488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档