BAEL-6300: Invoking Spring Cache @Cacheable from Another Method of the Same Bean (#14689)

* BAEL-6300: Problem Reproduction

* BAEL-6300: Internal Invocation with External Reference

* BAEL-6300: compile-time weaving

* BAEL-6300: load-time weaving
This commit is contained in:
Forb Yuan
2023-09-02 22:23:29 +08:00
committed by GitHub
parent 48510fb972
commit 592f8f325a
9 changed files with 300 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
package com.baeldung.selfinvocation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;
import java.util.concurrent.atomic.AtomicInteger;
@Service
@CacheConfig(cacheNames = "square")
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
public class MathService {
@Autowired
private MathService self;
private final AtomicInteger counter = new AtomicInteger();
@CacheEvict(allEntries = true)
public AtomicInteger resetCounter() {
counter.set(0);
return counter;
}
@Cacheable(key = "#n")
public double square(double n) {
counter.incrementAndGet();
return n * n;
}
public double sumOfSquareOf2() {
return this.square(2) + this.square(2);
}
public double sumOfSquareOf3() {
return self.square(3) + self.square(3);
}
}