2017-09-03 3 views
0

AOPはかなり新しいです。 AspectJを使用してSpringを使用しないでmavenプロジェクトに注釈を作成しようとしています。しかし、私が@Aspectを使って呼び出す方法は呼び出されていません。Springを使用しないAspectJアノテーションの使用

これは以下のように私のポンポンが見えるものです:注釈は、次のようになります

<?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>test</groupId> 
<artifactId>tanvi</artifactId> 
<version>1.0-SNAPSHOT</version> 

<dependencies> 
    <!-- https://mvnrepository.com/artifact/aspectj/aspectjrt --> 
    <dependency> 
     <groupId>aspectj</groupId> 
     <artifactId>aspectjrt</artifactId> 
     <version>1.5.3</version> 
    </dependency> 
    <!-- https://mvnrepository.com/artifact/aspectj/aspectjweaver --> 
    <dependency> 
     <groupId>aspectj</groupId> 
     <artifactId>aspectjweaver</artifactId> 
     <version>1.5.3</version> 
    </dependency> 
    <dependency> 
     <groupId>aspectj</groupId> 
     <artifactId>aspectjtools</artifactId> 
     <version>1.5.3</version> 
    </dependency> 
</dependencies> 

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>aspectj-maven-plugin</artifactId> 
      <version>1.8</version> 
      <configuration> 
       <complianceLevel>1.8</complianceLevel> 
       <source>1.8</source> 
       <target>1.8</target> 
      </configuration> 
      <executions> 
       <execution> 
        <phase>process-sources</phase> 
        <goals> 
         <goal>compile</goal> 
         <goal>test-compile</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
     <plugin> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>3.6.2</version> 
      <configuration> 
       <source>1.8</source> 
       <target>1.8</target> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

@Target({ElementType.METHOD}) 
@Retention(RetentionPolicy.RUNTIME) 
public @interface HasAccess { 
    Access[] accesses(); 
    String message() default "You are not allowed to perform this operation"; 
} 

私は私の注釈のための注釈プロセッサを作成:

@Aspect 
public class HasAccessAdvice { 
    // @Before("execution(* *.*(..)) && @annotation(testAnnotation) ") 
    @Before("execution(* *.*(..)) && @annotation(hasAccess)") 
    public void myBeforeLogger(JoinPoint joinPoint, HasAccess hasAccess) { 

    System.out.println("Okay - we're in the before handler..."); 
    System.out.println("The test annotation value is: " + hasAccess.accesses().toString()); 

    Signature signature = joinPoint.getSignature(); 
    String methodName = signature.getName(); 
    String stuff = signature.toString(); 
    String arguments = Arrays.toString(joinPoint.getArgs()); 
    System.out.println("Write something in the log... We are just about to call method: " 
         + methodName + " with arguments " + arguments + "\nand the full toString: " 
         + stuff); 

} 

}

私はこの呼び出しでそれを呼び出しています:

public class TestMe { 

    @HasAccess(accesses = {Access.CREATE_PURCHASE}) 
    public void createPurchase(BigDecimal bigDecimal) { 
     System.out.println("create Purchase called"); 
    } 
} 

私はaop.xmlファイルを作成してのpom.xmlと同じフォルダに置きました。

<aspectj> 
    <aspects> 
     <aspect name="HasAccessAdvice"/> 
    </aspects> 
</aspectj> 

メソッドcreatePurchaseを呼び出すと、まず@Beforeメソッドが呼び出されることなく実行されます。私が逃しているもので私を助けてください。私が見つけたドキュメンテーション/回答のほとんどは、春に整列されていました。任意のチュートリアルへのポインタや、Springを使わない単純な注釈を作成する別の方法も、非常に感謝しています。

+0

:あなたはJUnitテストを作成している場合
、次のようなロード時間ウィーバーを含めるようにpom.xml<build><plugins> -sectionをsrc/test/resourcesMETA-INF/aop.xmlを入れて調整することができますか? – mateuszlo

答えて

1

まず、aop.xmlを使用しているので、私はロードタイム織りをしたいと思っています。 Load Time Weaving docsおよびdocs on different weaving typesを参照してください。

第二に、あなたのaop.xmlファイルには、使用する<aspect>定義しますが、あなたはまた、あなたが織るしたいクラスファイル/パッケージ定義する必要があります任意の上のあなたの側面を実行するために

<aspectj> 
    <aspects> 
     <aspect name="HasAccessAdvice"/> 
    </aspects> 
    <weaver options="-verbose"> 
     <!-- weave anything --> 
     <include within="*" /> 
     <!-- weave specific packages only --> 
     <include within="my.package..*" /> 
    </weaver>  
</aspectj> 

をどちらか"*"を使用my.packageTestMeのパッケージに置き換えてください。ダブルドット..にはサブパッケージも含まれています。
また、<aspect name="...">はパッケージで完全修飾Aspect-nameを要求します。 default-packageにHasAccessibleAdviceを作成しましたか?それ以外の場合はパッケージを追加してください。

第3に、クラスパス上のMETA-INF/aop.xmlでaop.xmlを読み取る必要があります。
CLIを使用して(java -javaagent...を使用して)テストを実行している場合は、クラスパス設定(-cp)を確認してください。どのようにあなたのコードを実行している

<properties> 
    <aspectj.version>1.8.9</aspectj.version> 
</properties> 
<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <version>2.9</version> 
      <configuration> 
       <argLine> 
        -javaagent:${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar 
       </argLine> 
      </configuration> 
      <dependencies> 
       <dependency> 
        <groupId>org.aspectj</groupId> 
        <artifactId>aspectjweaver</artifactId> 
        <version>${aspectj.version}</version> 
       </dependency> 
      </dependencies> 
     </plugin> 
    </plugins> 
</build> 
関連する問題