戻り値の型がありません:
@Pointcut("execution(* backend.repository.QuestionsRepository.AnswerQuestion (..))")
次のように、引数名をバインドする必要があります。
@Pointcut("execution(* backend.repository.QuestionsRepository.AnswerQuestion (..))
&& args(question, answer)") // wrapped for readability only
ソリューション例
サービス インターフェイス:
package foo.bar.service;
public interface Service{
void whack(String thing, Integer thang);
}
実装クラス:
package foo.bar.service;
public class DefaultService implements Service{
@Override
public void whack(final String thing, final Integer thang){
System.out.println(
"Inside thing: " + thing + ", inside thang: " + thang
);
}
}
春の AOP の側面:
@Aspect
public class ServiceAspect{
@Pointcut("execution(* foo.bar.service.*.*(..))")
public void serviceMethodExecution(){
}
@Around(value = "serviceMethodExecution() && args(param1, param2)")
public void aroundServiceMethodExecution(final ProceedingJoinPoint pjp,
final String param1,
final Integer param2) throws Throwable{
System.out.println("Before thing: " + param1 + ", before thang: "
+ param2);
pjp.proceed();
System.out.println("After thing: " + param1 + ", after thang: "
+ param2);
}
}
スプリング コンテキスト XML:
<aop:aspectj-autoproxy proxy-target-class="false" />
<bean class="foo.bar.service.DefaultService" />
<bean class="foo.bar.aspects.ServiceAspect" />
テスト用のメイン クラス:
次に、プロセス全体をテストするための主な方法を示します。サービス Bean とアスペクトを定義する上記の XML を使用して、 XML 構成なしで Spring ApplicationContext を開始します (AspectJ ウィービングをオンにしたため、XML を使用しないソリューションのみが機能することがわかりました。どの Bean を含める必要があるかわかりません)。アスペクト j-autoproxy を有効にするのでClassPathXmlApplicationContext
、この最小限の XML を使用します):
public static void main(final String[] args){
final ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("classpath:/aspectContext.xml");
final Service service = applicationContext.getBean(Service.class);
service.whack("abc", 123);
}
出力:
Before thing: abc, before thang: 123
Inside thing: abc, inside thang: 123
After thing: abc, after thang: 123
これで始められるはずです。基本的に:JDKプロキシ(春のデフォルト)を使用する場合、インターセプトしているメソッドがサービスインターフェースによってサポートされていることを確認する必要があります。Spring AOP プロキシ メカニズムについては、こちらを参照してください。
ノート:
ご覧のとおり、メソッドの引数をポイントカットではなくアスペクトにバインドしているため、異なる引数シグネチャを持つメソッドでポイントカットを再利用できるようになっています。ただし、次のように、ポイントカットでそれらをバインドすることもできます。
@Pointcut(value = "execution(* foo.bar.service.*.*(..)) && args(a,b)",
argNames = "a,b")
public void serviceMethodExecution(final String a, final Integer b){
}
@Around(value = "serviceMethodExecution(param1, param2)",
argNames = "param1,param2")
public void aroundServiceMethodExecution(final String param1,
final Integer param2,
final ProceedingJoinPoint pjp) throws Throwable{
System.out.println("Before thing: " + param1 + ", before thang: "
+ param2);
pjp.proceed();
System.out.println("After thing: " + param1 + ", after thang: "
+ param2);
}