6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Spring AOP Annotationのbindingについて

Last updated at Posted at 2017-11-23

概要

クラスやメソッドに自作annotationをつけ、annotationにて指定した文字を使用する。

  • 環境
    • Spring AOP 4.3.5
    • cglib 2.1_3

クラスにつけたannotationと、メソッドにつけたannotationでは取得方法が異なっており、そこで躓きました。。

###Class版

クラスにつけるannotation
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ClassAnnotation{
    String value() default "defalut";
Aspect対象のクラス
@ClassAnnotation("Class_01")
public class SampleController {
Aspectの実現部(PointCatはAroundで実施)
@Around("@within(ann)")
public void classIntercepter(ProceedingJoinPoint pjp, ClassAnnotation ann) throws Throwable{
    System.out.println(ann.value());
    pjp.proceed();

###Method版

メソッドにつけるannotation
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodAnnotation {
    String value() default "defalut";
Aspect対象のメソッド
@MethodAnnotation("Method_01")
public void sampleMethod(
Aspectの実現部(
@Around("@annotation(ann)")
public void MethodIntercepter(ProceedingJoinPoint pjp, MethodAnnotation ann) throws Throwable{
    System.out.println(ann.value());
    pjp.proceed();

###値を使用しない場合はannotationクラスのフルパス設定をする

Aspectの実現部(クラス)
@Around("@within(com.example.annotation.ClassAnnotation)")
public void classIntercepter(ProceedingJoinPoint pjp) throws Throwable{
    pjp.proceed();
Aspectの実現部(メソッド)
@Around("@annotation(com.example.annotation.MethodAnnotation)")
public void classIntercepter(ProceedingJoinPoint pjp) throws Throwable{
    pjp.proceed();

参照:

ちょっと特殊なJavaのアノテーション
Spring AOP ポイントカット指定子の書き方について
AspectJを使ってAnnotationを活用しよう 前編

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?