[컴] Spring framework 의 @Pointcut의 동작

스프링 /

Spring framework 의 @Pointcut의 동작

ref.1 , ref.2 를 보자.

아래 예시는 모든 transfer 라는 이름의 method 가 실행될때라는 point(@Pointcut) 를 잡는 것이다. 함수이름 anyOldTransfer 는 그저 pointcut 의 이름을 정하는 역할을 할 뿐이다. 즉 anyOldTransfer라는 이름의 pointcut 이라고 보면 된다.

이 pointcut 을 기점으로 이 이후에, 또는 이 이전에 무엇인가를 하도록 정의할 수 있다.

@Pointcut("execution(* transfer(..))")// the pointcut expression
private void anyOldTransfer() {}// the pointcut signature
..
@AfterReturning(value = "anyOldTransfer()", returning = "id")
fun doSomething(joinPoint: JoinPoint, id: Long) {
...

조금더 자세한 이야기는 ref.1 에 있다.

dynamic proxy over reflection

정리가 필요...
answers from phind.com :

Java dynamic proxies provide a way to create proxy objects that implement a list of interfaces specified at runtime. These proxy objects can be used to add or modify the functionality of an existing class without modifying its original code. Dynamic proxies are particularly useful for framework writers and when concrete class implementations are not known until runtime Source 0.

The primary advantage of dynamic proxies over reflection is that dynamic proxies provide a type-safe way to perform method invocation on objects that present interface APIs Source 2. With reflection, you may need to manually check for method existence and handle exceptions related to incorrect method names or argument types.

Here are some pros and cons of using dynamic proxies versus reflection:

Dynamic Proxies:

Pros:

  • Type-safe method invocation, which reduces the chance of runtime errors due to incorrect method names or argument types Source 2.
  • Easier to implement handlers for multiple interfaces and methods, as all method invocations are routed to a single invoke() method Source 0.

Cons:

  • Limited to proxying interfaces, not concrete classes Source 3.
  • Requires the implementation of an InvocationHandler and the use of the Proxy class, which may be more complex than simply using reflection Source 3.

Reflection:

Pros:

  • Can be used to access and modify private members of a class, which is not possible with dynamic proxies Source 2.
  • Can be used to create instances of concrete classes, not just interfaces Source 3.

Cons:

  • Not type-safe, which can lead to runtime errors due to incorrect method names or argument types Source 2.
  • Handling multiple interfaces and methods may require additional complexity compared to dynamic proxies Source 0.

In summary, dynamic proxies are a useful tool for implementing proxy objects and adding or modifying functionality of existing classes without modifying their original code. They provide type-safety and can handle multiple interfaces and methods with ease. However, they are limited to proxying interfaces and require the implementation of an InvocationHandler. Reflection, on the other hand, can be used to access and modify private members and create instances of concrete classes, but lacks type-safety and may require additional complexity for handling multiple interfaces and methods.

Reference

  1. 11. Aspect Oriented Programming with Spring
  2. Spring AOP: What’s the difference between JoinPoint and PointCut? - Stack Overflow
  3. Creating Dynamic Proxies with Kotlin Coroutines - Technical Tutorial
  4. How to explicitly invoke default method from a dynamic proxy in Java? - StackTuts 

댓글 없음:

댓글 쓰기