如何在war包中配置AspectJ在Weblogic 12c中使用

时间:2016-09-22 15:08:17

标签: java maven aspectj weblogic12c aspectj-maven-plugin

我无法配置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的这个回购中拥有该示例的所有代码:

https://github.com/afdecastro879/aspectJPrueba

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

全部谢谢

1 个答案:

答案 0 :(得分:1)

这样的方面看起来有点啰嗦,但没关系(除了你的切入点中的包名称拼写错误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并修复了POM和其他一些东西(包括切入点中的拼写错误,提交的二进制文件等)。我还创建了一个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