2016-09-22 47 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

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