本稿はSpringFrameworkの@Beforeについて説明します。

@Beforeとは

@Beforeはメソッド単位で付与するアノテーションです。

@Beforeは@Acpectが付与されたクラス内で使用されます。

@Beforeが付与されたクラスは、execution内の条件に当てはまる場合、対象の処理の実行前に@Beforeメソッドが実行されます。

※AOPに関してはこちらを参照。

以下、@Beforeを使用したサンプルです。

@Aspect
@Component
public class TestAOP {

    @Before("execution(* com.example.springmvc2.*.*(..))")
    public void before(JoinPoint joinPoint) {
        System.out.println("Method Start:"  + joinPoint.getSignature());
    }

    @After("execution(* com.example.springmvc2.*.*(..))")
    public void after(JoinPoint joinPoint) {
        System.out.println("Method End:"  + joinPoint.getSignature());
    }
}