0

我有一个 Maven Java EE 6 项目,我在每个方法中都有一个 Logger 信息,以在控制台中显示参数的开头和结尾。

在某些方法中,我忘记了 make,所以我想使用 aspectJ 来管理每个调用方法的开头和结尾。

我使用 Jboss EAP6 作为服务器,使用 Jbossdevelopper Studio 作为 IDE,我在网上找到了一些教程,但总是谈论 spring 或 java aspactJ 项目。我在我的 IDE 上安装了插件 aspectJ,我尝试添加一个方面,它告诉我我的 maven 项目不是 aspectJ 项目,那么如何解决这个问题?

这是我的 Maven pom.xml

 <dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.7.3</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.7.3</version>
    </dependency>
</dependencies>

<repositories>
    <repository>
        <id>snapshots</id>
        <name>repo1-maven</name>
        <url>http://central.maven.org/maven2</url>
    </repository>
    <repository>
        <id>JBoss repository</id>
        <url>http://repository.jboss.org/nexus/content/groups/public/</url>
    </repository>
</repositories>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.4</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

这是我的界面:

public interface IClient {
    void addClient(ClientBean clt);
}

这是实现:

public class ClientImpl implements IClient {
    private List<ClientBean> ListOfCustomers;

    public ClientImpl() {
        setListOfCustomers(new ArrayList<ClientBean>());
    }

    public void addClient(ClientBean clt) {
        ListOfCustomers.add(clt);
    }

    public List<ClientBean> getListOfCustomers() {
        return ListOfCustomers;
    }

    public void setListOfCustomers(List<ClientBean> listOfCustomers) {
        ListOfCustomers = listOfCustomers;
    }
}

这是我试图做我的aspectJ的一个类witch:

@Aspect
public class ClientAspect {
    @Pointcut("execution(* *.*(..))")
    void anyCallMethod() {}

    @Before(value = "anyCallMethod()")
    public void befor(JoinPoint joinPoint) {
        System.out.println("Before, class: "
                + joinPoint.getSignature().getDeclaringType().getSimpleName()
                + ", method: " + joinPoint.getSignature().getName());
    }

    @After(value = "anyCallMethod()")
    public void after(JoinPoint joinPoint) {
        System.out.println("After, class: "
                + joinPoint.getSignature().getDeclaringType().getSimpleName()
                + ", method: " + joinPoint.getSignature().getName());
    }
}

我有一个要测试的人班,当我运行我的项目时,它在控制台上给了我没有登录

4

1 回答 1

1

您的 POM 存在几个问题:

  • [major] AspectJ Maven Plugin只在pluginManagementsection中配置,但在实际section中没有实际使用,是不会应用的plugins
  • [中]您使用了过时的 1.4 版AspectJ Maven 插件,默认情况下使用 AspectJ 编译器 1.6.11。我建议您使用基于 AspectJ 1.8.2 的当前插件版本 1.7,但可以在插件的dependencies子部分中覆盖以使用当前版本 1.8.4,我也建议您这样做,因为 1.8.4 包含与 1.8.2 相比有几个修复。
  • [次要]你的项目对AspectJ Runtime的依赖应该与AspectJ Maven Plugin使用的版本相匹配。
  • [次要]您不需要对aspectjweaver的项目依赖,这只是加载时编织所必需的。对于编译时编织,您已经使用了该插件,并且在运行时您只需要aspectjrt

更新:

这是我自己的一个项目的示例配置(Java SE,没有应用程序服务器,但它应该是等效的):

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.source-target.version>1.7</java.source-target.version>
    <aspectj.version>1.8.4</aspectj.version>
</properties>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>${java.source-target.version}</source>
                    <target>${java.source-target.version}</target>
                    <!-- IMPORTANT -->
                    <useIncrementalCompilation>false</useIncrementalCompilation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.7</version>
                <configuration>
                    <showWeaveInfo>true</showWeaveInfo>
                    <source>${java.source-target.version}</source>
                    <target>${java.source-target.version}</target>
                    <Xlint>ignore</Xlint>
                    <complianceLevel>${java.source-target.version}</complianceLevel>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <verbose>true</verbose>
                </configuration>
                <executions>
                    <execution>
                        <!-- IMPORTANT -->
                        <phase>process-sources</phase>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjtools</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </pluginManagement>

    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
    </dependency>
</dependencies>
于 2015-01-31T22:37:07.047 回答