2015-01-31 44 views
0

我有一個maven Java EE 6項目,並且我在每個方法中都有一個記錄器信息,以便在控制檯中以參數和結尾開始顯示。如何在Maven項目中使用AspectJ for Loggin?

在一些方法中,我忘了make,所以我想用aspectJ來管理每個被調用的方法的開始和結束。

我使用Jboss EAP6作爲服務器,使用Jboss developper Studio作爲IDE,我在網上發現了一些tuturials,但總是談論spring或java aspactJ項目。 我在我的IDE上安裝了pluging aspectJ,我嘗試添加一個方面,告訴我我的maven項目不是aspectJ項目,那麼如何解決這個問題?

這是我的Maven的pom.xml

<dependencies> 
    <dependency> 
     <groupId>org.aspectj</groupId> 
     <artifactId>aspectjweaver</artifactId> 
     <version>1.7.3</version> 
    </dependency> 
    <dependency> 
     <groupId>org.aspectj</groupId> 
     <artifactId>aspectjrt</artifactId> 
     <version>1.7.3</version> 
    </dependency> 
</dependencies> 

<repositories> 
    <repository> 
     <id>snapshots</id> 
     <name>repo1-maven</name> 
     <url>http://central.maven.org/maven2</url> 
    </repository> 
    <repository> 
     <id>JBoss repository</id> 
     <url>http://repository.jboss.org/nexus/content/groups/public/</url> 
    </repository> 
</repositories> 

<build> 
    <pluginManagement> 
     <plugins> 
      <plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>aspectj-maven-plugin</artifactId> 
       <version>1.4</version> 
       <executions> 
        <execution> 
         <goals> 
          <goal>compile</goal> 
          <goal>test-compile</goal> 
         </goals> 
        </execution> 
       </executions> 
       <configuration> 
        <source>${maven.compiler.source}</source> 
        <target>${maven.compiler.target}</target> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>2.5.1</version> 
       <configuration> 
        <source>${maven.compiler.source}</source> 
        <target>${maven.compiler.target}</target> 
       </configuration> 
      </plugin> 
     </plugins> 
    </pluginManagement> 
</build> 

這是我的界面:

public interface IClient { 
    void addClient(ClientBean clt); 
} 

這是實現:

public class ClientImpl implements IClient { 
    private List<ClientBean> ListOfCustomers; 

    public ClientImpl() { 
     setListOfCustomers(new ArrayList<ClientBean>()); 
    } 

    public void addClient(ClientBean clt) { 
     ListOfCustomers.add(clt); 
    } 

    public List<ClientBean> getListOfCustomers() { 
     return ListOfCustomers; 
    } 

    public void setListOfCustomers(List<ClientBean> listOfCustomers) { 
     ListOfCustomers = listOfCustomers; 
    } 
} 

這是一類whitch我儘量讓我的aspectJ:

@Aspect 
public class ClientAspect { 
    @Pointcut("execution(* *.*(..))") 
    void anyCallMethod() {} 

    @Before(value = "anyCallMethod()") 
    public void befor(JoinPoint joinPoint) { 
     System.out.println("Before, class: " 
       + joinPoint.getSignature().getDeclaringType().getSimpleName() 
       + ", method: " + joinPoint.getSignature().getName()); 
    } 

    @After(value = "anyCallMethod()") 
    public void after(JoinPoint joinPoint) { 
     System.out.println("After, class: " 
       + joinPoint.getSignature().getDeclaringType().getSimpleName() 
       + ", method: " + joinPoint.getSignature().getName()); 
    } 
} 

我有個男人類來測試,當我跑我的項目它給我號登錄控制檯上

回答

1

有幾個問題與你的POM:

  • [大]AspectJ Maven Plugin將不適用,如果您只在pluginManagement部分配置它,但實際上並沒有在實際的plugins部分中使用它。
  • [中]您使用的過時版本1.4 AspectJ Maven Plugin,默認情況下使用AspectJ編譯器1.6.11。我建議您使用基於AspectJ 1.8.2的當前插件1.7版,但可以覆蓋插件的dependencies子版本中的當前版本1.8.4,我也建議您這樣做,因爲1.8.4包含幾個與1.8.2相比的修復程序。
  • [輕微]你的項目對AspectJ運行依賴應匹配AspectJ的Maven插件使用的版本。
  • [次要]您不需要項目對aspectjweaver的依賴關係,這只是加載時編織所必需的。對於編譯時編織你已經使用插件和運行時,那麼你只需要aspectjrt

更新:

下面是我自己的項目中的一個樣本配置(Java SE的,沒有應用程序服務器,但它應該是等價的):

<properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <java.source-target.version>1.7</java.source-target.version> 
    <aspectj.version>1.8.4</aspectj.version> 
</properties> 

<build> 
    <pluginManagement> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.2</version> 
       <configuration> 
        <source>${java.source-target.version}</source> 
        <target>${java.source-target.version}</target> 
        <!-- IMPORTANT --> 
        <useIncrementalCompilation>false</useIncrementalCompilation> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>aspectj-maven-plugin</artifactId> 
       <version>1.7</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>${project.build.sourceEncoding}</encoding> 
        <verbose>true</verbose> 
       </configuration> 
       <executions> 
        <execution> 
         <!-- IMPORTANT --> 
         <phase>process-sources</phase> 
         <goals> 
          <goal>compile</goal> 
          <goal>test-compile</goal> 
         </goals> 
        </execution> 
       </executions> 
       <dependencies> 
        <dependency> 
         <groupId>org.aspectj</groupId> 
         <artifactId>aspectjtools</artifactId> 
         <version>${aspectj.version}</version> 
        </dependency> 
       </dependencies> 
      </plugin> 
     </plugins> 
    </pluginManagement> 

    <plugins> 
     <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>aspectj-maven-plugin</artifactId> 
     </plugin> 
    </plugins> 
</build> 

<dependencyManagement> 
    <dependencies> 
     <dependency> 
      <groupId>org.aspectj</groupId> 
      <artifactId>aspectjrt</artifactId> 
      <version>${aspectj.version}</version> 
      <scope>runtime</scope> 
     </dependency> 
    </dependencies> 
</dependencyManagement> 

<dependencies> 
    <dependency> 
     <groupId>org.aspectj</groupId> 
     <artifactId>aspectjrt</artifactId> 
    </dependency> 
</dependencies> 
+0

什麼我可以做的解決主要問題,如果我想那個方面Maven插件將被應用? – TinyOS 2015-01-31 22:45:25

+0

你需要的附加部分' org.codehaus.mojo AspectJ的行家-插件'(無插件版本)指的是在' ...'部所配置的版本和其他設置。我將用我自己的一個項目的樣本更新我的答案。我的示例解決了從大到小的所有問題。我希望這對你很好。如果是這樣,請接受並提出我的答案。 – kriegaex 2015-01-31 23:28:18

+0

當我執行我的主要課程時,感謝很多我現在有日誌,我希望它能爲我的其他戰爭項目分開:接口項目,實現項目和戰爭項目 – TinyOS 2015-01-31 23:53:37