-2

javaでアノテーションベースのスプリングアスペクトを設定するにはどうすればよいですか?

スプリング サービスを傍受したいとしましょう。通常は AOP ポイントカット式で行います。この例では、式の代わりに注釈を使用してそれを行う方法を詳しく説明しています。注釈を使用するため、これは移植性が高くなります。

多くの例がありますが、適切な内容を持っているものはほとんどありません。したがって、ここに置く...

これは解決済みの質問です。私は自分の答えを投稿しているので、自分自身を含む他の人に役立ちます..

4

1 に答える 1

0

春のアノテーションベースのAOPアドバイス

1.注釈を定義する

 public @interface ApplyAuthorisationAdvice {
 }

<aop:aspectj-autoproxy /> 前に行われていない場合は、春の構成ファイルに設定します

2.

import org.aopalliance.aop.Advice;
import org.aspectj.lang.annotation.Aspect;

@Aspect
@Component
public class AuthorisationAspect implements Advice {

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

    @Autowired
    MyService myService;

    @SuppressWarnings("unchecked")
    @AfterReturning(pointcut = "@annotation(com.example.ApplyAuthorisationAdvice)", returning = "result")
    public void afterReturning(JoinPoint joinPoint, Object result) {

        if (result != null && (result instanceof List)) {
            // filter stuff
        }

    }
}

3.傍受する必要があるサービスにアノテーションを適用する

@Component("myService")
public class MyServiceImpl implements MyService {
  @ApplyAuthorisationAdvice 
   public List<MySummary> search(MyContext searchContext) {
   }
}
于 2012-11-29T11:41:09.723 に答える