Skip to content

@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์ด ๋˜๋„๋ก ์ˆ˜์ •ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ๊ทธ์™ธ์— ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•๋“ค๋„ ์žˆ์ง€๋งŒ ์„ ํ˜ธ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

java
@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๋ฅผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์—†๋Š”์ง€๋ฅผ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Released under the MIT License.