2017-07-01 4 views
0

注釈(春の起動アプリケーションで)注釈を書き込んで、call()メソッドCallableに適用しようとしましたが、それでもまだ動作しません。通常の方法(下記のコードを参照してください)、それは動作します、これは私を悩ませ続ける、あなたは私にいくつかの手がかりをくれますか?どうもありがとうございました。ここでJavaの注釈がCallableで機能しない

が@sbjavateamに触発さ

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

@Aspect 
@Component 
public class LogExecutionTimeAspect { 

    private static final Logger logger = LoggerFactory.getLogger(LogExecutionTimeAspect.class); 

    @Around("@annotation(LogExecutionTime)") 
    public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable { 
     final long start = System.currentTimeMillis(); 
     final Object proceed = joinPoint.proceed(); 
     final long executionTime = System.currentTimeMillis() - start; 
     logger.info(joinPoint.getSignature() + " executed in " + executionTime + "ms"); 
     return proceed; 
    } 
} 


public class DummyCallable implements Callable<Integer> { 

    private Integer start, count; 

    DummyCallable() {} 

    DummyCallable(Integer start, Integer count) { 
     this.start = start; 
     this.count = count; 
    } 

    @LogExecutionTime // not working... 
    @Override 
    public Integer call() throws Exception { 
     Thread.sleep(start * 1000); 
     Integer sum = 0; 
     for (Integer i = start; i <= start + count; i++) { 
      sum += i; 
     } 
     return sum; 
    } 
} 



@LogExecutionTime // This will work... 
public List<Integer> getAllUserScores() { 
    Callable c1 = new DummyCallable(1, 100000); 
    Callable c2 = new DummyCallable(2, 100000); 
    Callable c3 = new DummyCallable(3, 100000); 
    // .... run them ... 
    return result; 
} 
+1

春はプロキシを使用し、オブジェクトへのメソッド呼び出しだけがインターセプトされます。内部的にメソッドを呼び出しています。 –

答えて

0

、私のコードです、私は三つのこと、

  1. spring aop work only with object that are managed by spring container. To apply aspect for your class it should be a bean or component and instantiated by spring context.(すべての権利は、これはsbjavateamの回答@からコピーされます。)ことを実現
  2. 前のステートメントに基づいて、Callable c1 = new DummyCallable(1, 100000);は本質的に間違っています。なぜなら、スプリングコンテキストからDummyCallableを作成しなければならないからです(そうすれば、Beanはそれが依存関係に正しく挿入されるようになります)。newは使用できません。
  3. DummyCallableクラスは、シングルトンではないようにプロトタイプのスコープを持つ必要があります。シングルトンスコープはSpring Beanのデフォルトスコープです。その結果、このクラスにはこの注釈(@Scope("prototype"))が必要です。以下は

私の修正があり、他に

@Component 
@Scope("prototype") 
public class DummyCallable implements Callable<Integer> {} 


private DummyCallable createDummyCallable(Integer start, Integer end) { 
     return context.getBean(DummyCallable.class, start, end); 
    } 

、あなたにもこの設定をお勧めします、

spring.aop.proxy-target-class=true 

なく、少なくとも最後には、非常に多くの@sbjavateamをお願いします。

+0

いくつかの発言、ポイント2について:あなたはspring aopなしでこの呼び出しにアスペクトを適用することができますが、アスペクトをコンテナによってマネージドBeanに適用することはできますが、speialエージェントを使用してプロジェクトをコンパイルする必要があります。 AspectJは、コンパイル時またはクラスローディング時に使用できます。 Spring Aopはaspectjをサポートしていますので、エージェントspringを使ってプロジェクトをコンパイルすると、aopはこの呼び出しをサポートします/。 https://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html#aop-aj-ltw – xyz

関連する問題