@Cacheable self-invocation (in effect, a method within the target object calling another method of the target object). The cache annotation will be ignored at runtime
์คํ๋ง ํ๋ ์์ํฌ์์ ๊ฐ๋ฐ์๊ฐ ๊ฒฝํํ๊ธฐ ์ฌ์ด ์ค์๋ ๋์ผํ ํด๋์ค ๋ด์์ ๋น๋๊ธฐ ๋๋ ์บ์ ์ด๋ ธํ ์ด์ ์ด ์ ์ธ๋ ํจ์๋ฅผ ๋ด๋ถ์ ์ผ๋ก ํธ์ถํ๋๋ก ๋น์ฆ๋์ค ๋ก์ง์ ์์ฑํ๋ ๊ฒ ์ ๋๋ค. ์ด๋ฌํ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ ๊ฒฝ์ฐ์๋ Self Invocation์ด ๋์ง ์๋๋ก ๋ณ๋์ ํด๋์ค๋ก ๋ถ๋ฆฌ๋๋๋ก ๋ฆฌํฉํ ๋ง์ ์งํํ๊ฑฐ๋ Self-Injection์ด ๋๋๋ก ์์ ํด์ผํฉ๋๋ค. ๊ทธ์ธ์ ์๋์ ๊ฐ์ ๋ฐฉ๋ฒ๋ค๋ ์์ง๋ง ์ ํธ๋์ง ์์ต๋๋ค.
@Service
public class AService extends AbstractService {
@Cacheable(cacheNames = "a", key = "#root.methodName", unless = "#result == null")
public String getA() {
return "A";
}
public String getAA() {
return ((AService) AopContext.currentProxy()).getA();
}
}
- AdviceMode.ASPECTJ (AspectJ Weaving)
- AopContext.currentProxy()
- ApplicationContext.getBean()
์คํ๋ง ํ๋ ์์ํฌ ๊ธฐ๋ฐ์ ์ ํ๋ฆฌ์ผ์ด์ ์๋ฒ๋ฅผ ์์ฑํ๋ ๊ฐ๋ฐ์๋ผ๋ฉด ํ๋ก์ ๋งค์ปค๋์ฆ์ ๋ํด์ ์ดํดํ๊ณ ์์ด์ผ ํฉ๋๋ค. ๊ธฐ๋ณธ์ ์ธ ํ๋ก์(CGLib) ๋๋ JDK ํ๋ก์๊ฐ ๋ฌด์์ธ์ง ์๊ณ ์์ด์ผ ์ public ์ ๊ทผ์ ์ด์๊ฐ ์๋๊ฑฐ๋ ๋ด๋ถ ํธ์ถ์ธ ๊ฒฝ์ฐ AOP๋ฅผ ์ํํ ์ ์๋์ง๋ฅผ ์ดํดํ ์ ์์ต๋๋ค.