About binding of Spring AOP Annotation

Overview

Add your own annotation to the class or method, and use the characters specified in annotation.

--Environment - Spring AOP 4.3.5 - cglib 2.1_3

The acquisition method is different between the annotation attached to the class and the annotation attached to the method, and I stumbled there. ..

Class version

Annotation attached to the class


@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ClassAnnotation{
    String value() default "defalut";

Aspect target class


@ClassAnnotation("Class_01")
public class SampleController {

Realization part of Aspect (PointCat is carried out around)


@Around("@within(ann)")
public void classIntercepter(ProceedingJoinPoint pjp, ClassAnnotation ann) throws Throwable{
    System.out.println(ann.value());
    pjp.proceed();

Method version

Annotation attached to the method


@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodAnnotation {
    String value() default "defalut";

Aspect target method


@MethodAnnotation("Method_01")
public void sampleMethod(

Realization part of Aspect (


@Around("@annotation(ann)")
public void MethodIntercepter(ProceedingJoinPoint pjp, MethodAnnotation ann) throws Throwable{
    System.out.println(ann.value());
    pjp.proceed();

If you do not use a value, set the full path of annotation class.

Realization of Aspect(class)


@Around("@within(com.example.annotation.ClassAnnotation)")
public void classIntercepter(ProceedingJoinPoint pjp) throws Throwable{
    pjp.proceed();

Realization of Aspect(Method)


@Around("@annotation(com.example.annotation.MethodAnnotation)")
public void classIntercepter(ProceedingJoinPoint pjp) throws Throwable{
    pjp.proceed();

reference:

Slightly special Java annotation How to write Spring AOP pointcut specifier Let's utilize Annotation with AspectJ Part 1

Recommended Posts

About binding of Spring AOP Annotation
About Spring AOP
About spring AOP
About DI of Spring ①
About Spring AOP Pointcut
About DI of Spring ②
Overview of Spring AOP
The story of encountering Spring custom annotation
About the initial display of Spring Framework
About the official start guide of Spring Framework
About disconnect () of HttpURLConnection
About selection of OpenJDK
About Java method binding
History of Java annotation
About form. ○○ of form_with
About @Accessors of Lombok
About the function of Spring Boot due to different versions
A story about BeanNotOfRequiredTypeException occurring after applying AOP in Spring
About the handling of Null
[Personal memo] About Spring framework
About an instance of java
About Spring Framework context error
[Java] Creation of original annotation
About the description of Docker-compose.yml
About size comparison of compareTo
About types of code coverage
Introduction to Spring Boot ② ~ AOP ~
[Java] Spring AOP execution order
Memorandum of understanding about LOD.
Spring Boot for annotation learning
About Spring DI related annotations
About partial match of selector