2015-07-04 65 views
0

面向方面開發的新手段。方面註釋鏈接

兩部分問題即將到來。

  1. 你有沒有包含教程和代碼的好網站? 到目前爲止,我已經看過很多教程,但代碼分散,沒有什麼我可以拼湊在一起,以便它可以在本地工作。

  2. 我試圖創建一個方面和一個aspectj類的框架,應該攔截所有方面的註釋方法調用。 它在我的本地項目中效果很好,但是當我嘗試在另一個項目中使用該方面時,它似乎沒有工作。

代碼示例:看點攔截

@Aspect 
public class InterceptCallAspect { 
    @Around("execution(* *(@InterceptCall (*)));") 
    public void record(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { 
     //Before 
     System.out.println("Before"); 
     proceedingJoinPoint.proceed(); 
     System.out.println("After"); 
     //After 
    } 
} 

的看點

@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.METHOD) 
public @interface InterceptAspectAnnotation { 
} 

所以,當我詮釋我的測試用例的項目中,我得到適當的地方系統輸出。 但是,當我創建我的jar並將其捆綁到另一個項目中時,它不會執行任何操作。

我的POM文件:

<?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> 
<artifactId>InterceptCall</artifactId> 
<groupId>testing</groupId> 
<packaging>jar</packaging> 
<version>0.0.2-SNAPSHOT</version>  
<dependencies> 
    <dependency> 
     <groupId>org.aspectj</groupId> 
     <artifactId>aspectjrt</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.aspectj</groupId> 
     <artifactId>aspectjweaver</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-aop</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
    </dependency> 
</dependencies> 
<build> 
    <plugins> 
     <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>aspectj-maven-plugin</artifactId> 
      <version>1.7</version> 
      <configuration> 
       <complianceLevel>1.8</complianceLevel> 
       <source>1.8</source> 
       <target>1.8</target> 
      </configuration> 
      <executions> 
       <execution> 
        <goals> 
         <goal>compile</goal> 
         <goal>test-compile</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 

回答

2

我只能滿足你的第二個問題,所以我會離開第一個爲別人着想。

如果您通過外部庫(如構建的jar)提供該方面,則需要告知aspectj-maven-plugin從哪裏可以找到用於編織的方面。該配置 - 標籤需要包含一個名爲aspectLibrariesaspectLibrary標籤都有效標記每個LIB使用:

 <aspectLibraries> 
      <aspectLibrary> 
       <groupId>com.your.example.util</groupId> 
       <artifactId>tracing-aspect</artifactId> 
      </aspectLibrary> 
     </aspectLibraries> 
+0

嗯,我想創建一個方面的通用庫,它應該可用於任何項目。所以如果你是對的(需要檢查它),這意味着任何想要使用我的框架的項目都需要在他們的Maven項目中有aspectLibraries標記?如果他們使用groovy或螞蟻類似的構建腳本。 – Nosfert

+0

是的,無論構建腳本如何,AspectJ都需要知道哪些方面用於編織。它可以以任何方式獲取項目代碼中的所有內容,但如果它以圖書館的形式出現,情況並非如此。因此,aspectLibraries標籤。 – sheltem

+0

http://www.github.com/abrovinc/methodmock 這就是我走的路。 – Nosfert