Files
spring-soap/spring-aop-2/src/main/java/com/baeldung/selfinvocation/MathService.java
Forb Yuan 592f8f325a 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
2023-09-02 16:23:29 +02:00

44 lines
1.1 KiB
Java

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);
}
}