2016-09-22 45 views
0

我無法配置Weblogic 12c以使用AspectJ。閱讀一些帖子,我已經做了一些嘗試來配置它,但我無法達到結果。我的項目使用maven和aspectj maven插件。我的配置是如下:如何配置AspectJ在Weblogic 12c中的war包中工作

的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全新安裝這個錯誤如圖所示:

Errors shown by AspectJ

我知道春天有AspectJ的兼容性,但我不能用它,我只需要上面顯示的配置。如果有人想幫我,我有這個回購的例子的所有代碼在github上:

https://github.com/afdecastro879/aspectJPrueba

最後,我發展我的項目中使用IntelliJ IDEA的IDE。

感謝所有

回答

0

的方面本身看起來有點囉嗦,但沒關係(除包名錯字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 
[email protected] 

Process finished with exit code 0 
+0

非常感謝你。它完美的作品。 –