This is a story about AOP of Java Spring Framework.
For example, there are 100 classes that want to use a certain Service class, and it can be realized by adding @Autowired
each time and DI, but it is not very smart.
Therefore, define the process you want to perform in various places (class with @ Aspect
annotation), and add annotation to the method of the class you want to use it to improve the visibility of the source code. That is the purpose.
Join Point
A point that executes a cross-cutting concern (at the time of method execution or exception throw).The code that runs at a particular JoinPoint, the implementation of cross-cutting concerns.
@Around` is highly versatile, but if you want to limit the timing such as processing that you want to perform only when the processing of the target method is successful, you should use Advice that suits your purpose.@Before Processed before processing the target method
@AfterReturning Processed when the target method is successfully processed
@AfterThrowing Processed when an exception is thrown in the processing of the target method
@After: Processed when the process is completed regardless of the success or failure of the process of the target method
@Around Executed before and after processing the target method
pointcut
An expression that selects the JoinPoint to be executed. You can narrow down the execution timing in detail by specifying the conditions. I will not touch it this time. The following will be helpful.
https://qiita.com/rubytomato@github/items/de1019aeaaab51c8784dMyController.java
@Controller
@RequestMapping("/hogehoge")
public class MyController {
@GetMapping
@MyAnnotation(hoge = "test", piyo = false)
public String doGet(HttpServletRequest req, HttpServletResponse res) {
//~ Processing ~
return "hogehoge";
}
}
This area was helpful. https://itsakura.com/java-annotation-make Primitive type, String, Class, enumeration type, annotation, and only one-dimensional array of them can be specified as arguments. https://www.ne.jp/asahi/hishidama/home/tech/java/annotation.html
MyAnnotation.java
//Annotation scope.
@Retention(RetentionPolicy.RUNTIME)
//Target to which you want to give Annotation.
@Target(ElementType.METHOD)
public @interface MyAnnotation {
/**String*/
String hoge();
/** boolean */
boolean piyo();
}
MyAspect.java
@Aspect
@Component
public class MyAspect {
@AfterReturning("@annotation(myAnnotation)")
public void after(JoinPoint jp, MyAnnotation myAnnotation) throws Throwable {
//Try to receive the caller's arguments
//Here, you can get the caller's HttpServletRequest req and HttpServletResponse res.
Object[] o = jp.getArgs();
//Can accept Annotation arguments
String hoge = myAnnotation.hoge();
System.out.println(hoge); // test
boolean piyo = myAnnotation.piyo();
System.out.println(piyo); // false
}
}
--Can receive the argument of the calling method, but cannot process the variable name as the key because it is the "value" of the argument
protected HttpServletRequest getRequest() {
return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
.getRequest()
.getParameter("hoge");
}
--Variables cannot be passed as arguments of Annotation (arguments)
Primitive type, String, Class, enumeration type, annotation, and only one-dimensional array of them can be specified. )
--The writing method is slightly different depending on whether the Annotation argument is used or not.
--When using, follow the sample code above
--When not used @AfterReturning ("@ annotation (jp.ne.example.MyAnnotation)")
(describe the full path of the annotation class)
Recommended Posts