먼저 이 질문은 두 가지로 나눠야 한다.
- Spring AOP는 왜 Bean으로 등록된 객체에 적용되는가?
- Spring AOP 동작 방식에는 어떤 것이 있는가?
1. 왜 Bean으로 동록되어야 하는가?
https://stackoverflow.com/questions/23394407/is-it-possible-to-use-spring-aop-without-creating-beans
Is it possible to use Spring AOP without creating beans?
Spring AOP depends on proxy mechanism - J2SE dynamic proxies or using CGLIB(according to the spring documentation). Is it possible to use the AOP mechanism defined by Spring without creating/decla...
stackoverflow.com
- 요약하자면 Spring AOP는 기본적으로 Spring IoC와 협업한다.
- Spring AOP가 정상적으로 동작하기 위해서는 Spring Context 즉, Spring 관리 아래에 있어야 하지만 Bean으로 등록되지 않은 객체는 보통 new SomeClass() 와 같이 동적 할당하여 사용하기 때문에 Spring 관리 아래에 놓이지 못한다.
2. AOP 동작 방식
Spring AOP는 기본적으로 RTW(Runtime weaving)으로 두 가지 방식이 존재한다.
ProxyFactoryBean은 interface의 유무에 따라 두 가지 방식 중 한 가지를 선택한다.
1. JDK Proxy
- 인터페이스가 존재하는 경우 사용된다.
- 자바 리플렉션을 사용하여 실제 실행되는 메서드를 invoke() 하기 때문에 CGLIB 방식보다 성능 상 불리하다.
2. CGLIB
- 바이트 코드를 조작하기때문에 인터페이스가 없어도 실행 가능하다.
- 다만 final 클래스나 메서드인 경우에는 사용할 수 없다.
Bean이 아닌 POJO 객체, 그리고 final 클래스나 메서드에 사용하고 싶다면?
- AspectJ를 참고
추가) @Transactional은 기본적으로 CGLib Proxy 방식을 사용
참고
https://do-study.tistory.com/83
Spring AOP 동작 방식, 원리
스프링은 기본적으로 프록시 기반 AOP 를 제공한다. 스프링에서는 Java Dynamic Proxy를 사용하거나 Cglib을 사용하여 프록시 기반 AOP를 구현했다. 이 글에서는 어떻게 Spring 에서 AOP를 사용할 수 있는
do-study.tistory.com
https://stackoverflow.com/questions/38043089/spring-aop-for-non-spring-component
Spring AOP for non spring component
I am writing Spring 4 application with java config. I can use AOP in this project for all spring component. But i can't use it for a normal POJO class. what is the library I need to add and what ...
stackoverflow.com
'자바 개발자되기' 카테고리의 다른 글
Java Ratelimiter - 초 당 처리량 조절하기 (1) | 2021.03.01 |
---|---|
자바 원시(Primitive) 타입과 참조(Reference) 타입, 그리고 Reflection (1) | 2021.01.01 |
Junit5 @BeforeEach private method가 동작하는 이유 (0) | 2020.12.30 |
Junit5 - @BeforeEach @BeforeAll (0) | 2020.12.23 |
[우아콘2020] - Zero Payload 방식 리뷰 (0) | 2020.12.19 |