Search code examples
javamavenaspectjweblogic12caspectj-maven-plugin

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


I can't configure Weblogic 12c to work with AspectJ. Reading some posts I have done something to try to configure it, but I can't reach a result. My project works with maven and aspectj maven plugin. My configuration it's the following:

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>

My Aspect

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());
}
}

So, When I run mvn clean install this error is shown:

Errors shown by AspectJ

I know Spring has compatibility with AspectJ but I can't use it, I just need the configuration shown above. If someone want to help me I have all the code of the example in this repo in github:

https://github.com/afdecastro879/aspectJPrueba

Finally, I'm developing my project Using IntelliJ Idea IDE.

Thanks for all


Solution

  • The aspect as such looks a bit long-winded, but okay (except for the package name typo co.example instead of com.example in your pointcut). What I would recommend you to do, though, is to use a standard Maven directory layout instead of customising paths in your AspectJ Maven plugin, especially because IntelliJ IDEA plays so nicely with Maven projects, being able to auto-update IDEA project settings whenever you change the POM etc.

    You should remove those two parameters from your AspectJ Maven configuration:

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

    I 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 in order to enable you to easily integrate my changes into your repo. Last, but not least, I added a sample stand-alone application with a main method so as to be able to quickly test the whole thing.

    Now Maven says:

    [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)
    

    And the sample application yields this output:

    #### 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