如何在 war 包中配置与 Weblogic 12c 一起使用的 AspectJ

How to configure AspectJ working with in Weblogic 12c in a war package

我无法配置 Weblogic 12c 以使用 AspectJ。阅读一些帖子我做了一些尝试配置它,但我无法达到结果。我的项目使用 maven 和 aspectj maven 插件。我的配置如下:

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">
<modelVersion>4.0.0</modelVersion>
<groupId>co.example</groupId>
<artifactId>PruebaAspectJ</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>basicWebapp</name>
<parent>
    <groupId>com.oracle.weblogic.archetype</groupId>
    <artifactId>wls-common</artifactId>
    <version>12.1.3-0-0</version>
</parent>
<dependencies>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.7</version>
    </dependency>
</dependencies>
<build>
    <finalName>basicWebapp</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
                <target>1.8</target>
                <source>1.8</source>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.8</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>UTF-8</encoding>
                <verbose>true</verbose>
                <aspectDirectory>src/java/aspectos</aspectDirectory>
                <!--<sources>
                    <source>
                        <basedir>src/main/java</basedir>
                        <includes>
                            <include>**/*.aj</include>
                            <include>**/*.java</include>
                        </includes>
                    </source>
                </sources>-->
                <outputDirectory>${project.reporting.outputDirectory}/aspectj-report</outputDirectory>

            </configuration>
            <executions>
                <execution>
                    <!-- IMPORTANT -->
                    <phase>process-sources</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId>
                    <version>${aspectj.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjrt</artifactId>
                    <version>1.8.7</version>
                </dependency>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjweaver</artifactId>
                    <version>1.8.7</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.source-target.version>1.8</java.source-target.version>
    <aspectj.version>1.8.7</aspectj.version>
</properties>

我的看点

package aspectos;

public aspect Logger {
pointcut logger() : call(* co.example..*(..));

before() : logger() {
    System.out.println("#### Signatura: "+thisJoinPointStaticPart.getSignature());
    boolean entro = false;
    for (int i = 0; i < thisJoinPoint.getArgs().length; i++) {
        if(!entro){
            System.out.println("#### Argumentos: ");
            entro=true;
        }
        System.out.println("\t"+thisJoinPoint.getArgs()[i].getClass().toString());
    }
    System.out.println("#### Target: "+thisJoinPoint.getTarget().getClass().toString());
}

after() returning(Object r): logger(){
    if(r!=null){
        System.out.println("#### Objeto retornado: "+r.getClass().getSimpleName());
    }
}

after() throwing(Throwable e): logger(){
    System.out.println("#### Excepcion: "+e.getMessage());
}
}

因此,当我 运行 mvn clean install 时显示此错误:

Errors shown by AspectJ

我知道 Spring 与 AspectJ 兼容,但我不能使用它,我只需要上面显示的配置。如果有人想帮助我,我在 github:

中有这个 repo 中示例的所有代码

https://github.com/afdecastro879/aspectJPrueba

最后,我正在使用 IntelliJ Idea 开发我的项目 IDE。

感谢大家

这样的方面看起来有点冗长,但还可以(除了包名拼写错误 co.example 而不是切入点中的 com.example )。不过,我建议您做的是使用标准的 Maven 目录布局,而不是在 AspectJ Maven 插件中自定义路径,尤其是因为 IntelliJ IDEA 与 Maven 项目配合得非常好,无论何时您都能够自动更新 IDEA 项目设置更改 POM 等

您应该从 AspectJ Maven 配置中删除这两个参数:

<aspectDirectory>src/java/aspectos</aspectDirectory>
<!-- ... -->
<outputDirectory>${project.reporting.outputDirectory}/aspectj-report</outputDirectory>

cloned your repo and fixed the POM and a few other things (typos in package names in pointcuts, committed binaries etc.) for you. I also created a pull request是为了让您能够轻松地将我的更改集成到您的存储库中。最后但同样重要的是,我添加了一个带有 main 方法的示例独立应用程序,以便能够快速测试整个过程。

现在 Maven 说:

[INFO] --- aspectj-maven-plugin:1.8:compile (default) @ PruebaAspectJ ---
[INFO] Showing AJC message detail for messages of types: [error, warning, fail]
[INFO] Join point 'method-execution(java.lang.String com.example.AccountBean.getName())' in Type 'com.example.AccountBean' (AccountBean.java:29) advised by around advice from 'com.example.AroundExample' (AroundExample.java:18)
[INFO] Join point 'method-execution(void com.example.AccountBean.setName(java.lang.String))' in Type 'com.example.AccountBean' (AccountBean.java:33) advised by around advice from 'com.example.AroundExample' (AroundExample.java:18)
[INFO] Join point 'method-execution(float com.example.AccountBean.getAmount())' in Type 'com.example.AccountBean' (AccountBean.java:37) advised by around advice from 'com.example.AroundExample' (AroundExample.java:18)
[INFO] Join point 'method-execution(void com.example.AccountBean.setAmount(float))' in Type 'com.example.AccountBean' (AccountBean.java:41) advised by around advice from 'com.example.AroundExample' (AroundExample.java:18)
[INFO] Join point 'method-execution(java.lang.String com.example.AccountBean.getMsg())' in Type 'com.example.AccountBean' (AccountBean.java:45) advised by around advice from 'com.example.AroundExample' (AroundExample.java:18)
[INFO] Join point 'method-execution(void com.example.AccountBean.deposit())' in Type 'com.example.AccountBean' (AccountBean.java:50) advised by around advice from 'com.example.AroundExample' (AroundExample.java:18)
[INFO] Join point 'method-call(void com.example.AccountBean.setName(java.lang.String))' in Type 'de.scrum_master.app.Application' (Application.java:8) advised by before advice from 'aspectos.Logger' (Logger.aj:9)
[INFO] Join point 'method-call(void com.example.AccountBean.setName(java.lang.String))' in Type 'de.scrum_master.app.Application' (Application.java:8) advised by afterReturning advice from 'aspectos.Logger' (Logger.aj:22)
[INFO] Join point 'method-call(void com.example.AccountBean.setName(java.lang.String))' in Type 'de.scrum_master.app.Application' (Application.java:8) advised by afterThrowing advice from 'aspectos.Logger' (Logger.aj:28)
[INFO] Join point 'method-call(void com.example.AccountBean.setAmount(float))' in Type 'de.scrum_master.app.Application' (Application.java:9) advised by before advice from 'aspectos.Logger' (Logger.aj:9)
[INFO] Join point 'method-call(void com.example.AccountBean.setAmount(float))' in Type 'de.scrum_master.app.Application' (Application.java:9) advised by afterReturning advice from 'aspectos.Logger' (Logger.aj:22)
[INFO] Join point 'method-call(void com.example.AccountBean.setAmount(float))' in Type 'de.scrum_master.app.Application' (Application.java:9) advised by afterThrowing advice from 'aspectos.Logger' (Logger.aj:28)

示例应用程序产生以下输出:

#### Signatura: void com.example.AccountBean.setName(String)
#### Argumentos: 
    class java.lang.String
#### Target: class com.example.AccountBean
Executing aspectj
#### Signatura: void com.example.AccountBean.setAmount(float)
#### Argumentos: 
    class java.lang.Float
#### Target: class com.example.AccountBean
Executing aspectj
com.example.AccountBean@1be6f5c3

Process finished with exit code 0