diff --git a/spring-aop-2/src/main/java/com/baeldung/internalaop/AddComponent.java b/spring-aop-2/src/main/java/com/baeldung/internalaop/AddComponent.java new file mode 100644 index 000000000000..014968e7a199 --- /dev/null +++ b/spring-aop-2/src/main/java/com/baeldung/internalaop/AddComponent.java @@ -0,0 +1,32 @@ +package com.baeldung.internalaop; + +import org.springframework.cache.annotation.CacheConfig; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Component; + +@Component +@CacheConfig(cacheNames = "addOne") +public class AddComponent { + + private int counter = 0; + + @Cacheable + public int addOne(int n) { + counter++; + return n + 1; + } + + public int addOneAndDouble(int n) { + return addOne(n) + addOne(n); + } + + public int getCounter() { + return counter; + } + + @CacheEvict(allEntries = true) + public void resetCache() { + counter = 0; + } +} diff --git a/spring-aop-2/src/main/java/com/baeldung/internalaop/AddOneAndDoubleComponent.java b/spring-aop-2/src/main/java/com/baeldung/internalaop/AddOneAndDoubleComponent.java new file mode 100644 index 000000000000..3e5a0e8b19ef --- /dev/null +++ b/spring-aop-2/src/main/java/com/baeldung/internalaop/AddOneAndDoubleComponent.java @@ -0,0 +1,16 @@ +package com.baeldung.internalaop; + +import jakarta.annotation.Resource; + +import org.springframework.stereotype.Component; + +@Component +public class AddOneAndDoubleComponent { + + @Resource + private AddComponent addComponent; + + public int addOneAndDouble(int n) { + return addComponent.addOne(n) + addComponent.addOne(n); + } +} diff --git a/spring-aop-2/src/main/java/com/baeldung/internalaop/SelfInjection.java b/spring-aop-2/src/main/java/com/baeldung/internalaop/SelfInjection.java new file mode 100644 index 000000000000..9ecd47838861 --- /dev/null +++ b/spring-aop-2/src/main/java/com/baeldung/internalaop/SelfInjection.java @@ -0,0 +1,39 @@ +package com.baeldung.internalaop; + +import jakarta.annotation.Resource; + +import org.springframework.cache.annotation.CacheConfig; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.context.annotation.Lazy; +import org.springframework.stereotype.Component; + +@Component +@CacheConfig(cacheNames = "selfInjectionAddOne") +public class SelfInjection { + + @Lazy + @Resource + private SelfInjection selfInjection; + + private int counter = 0; + + @Cacheable + public int addOne(int n) { + counter++; + return n + 1; + } + + public int addOneAndDouble(int n) { + return selfInjection.addOne(n) + selfInjection.addOne(n); + } + + public int getCounter() { + return counter; + } + + @CacheEvict(allEntries = true) + public void resetCache() { + counter = 0; + } +} diff --git a/spring-aop-2/src/test/java/com/baeldung/internalaop/AddComponentUnitTest.java b/spring-aop-2/src/test/java/com/baeldung/internalaop/AddComponentUnitTest.java new file mode 100644 index 000000000000..9dda851a1dbf --- /dev/null +++ b/spring-aop-2/src/test/java/com/baeldung/internalaop/AddComponentUnitTest.java @@ -0,0 +1,37 @@ +package com.baeldung.internalaop; + +import static org.assertj.core.api.Assertions.assertThat; + +import jakarta.annotation.Resource; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +import com.baeldung.Application; + +@SpringBootTest(classes = Application.class) +class AddComponentUnitTest { + + @Resource + private AddComponent addComponent; + + @Test + void whenInternalCall_thenCacheNotHit() { + addComponent.resetCache(); + + addComponent.addOneAndDouble(0); + + assertThat(addComponent.getCounter()).isEqualTo(2); + } + + @Test + void whenExternalCall_thenCacheHit() { + addComponent.resetCache(); + + addComponent.addOne(0); + addComponent.addOne(0); + + assertThat(addComponent.getCounter()).isEqualTo(1); + } + +} diff --git a/spring-aop-2/src/test/java/com/baeldung/internalaop/AddOneAndDoubleComponentUnitTest.java b/spring-aop-2/src/test/java/com/baeldung/internalaop/AddOneAndDoubleComponentUnitTest.java new file mode 100644 index 000000000000..0dd6256ae4ba --- /dev/null +++ b/spring-aop-2/src/test/java/com/baeldung/internalaop/AddOneAndDoubleComponentUnitTest.java @@ -0,0 +1,29 @@ +package com.baeldung.internalaop; + +import static org.assertj.core.api.Assertions.assertThat; + +import jakarta.annotation.Resource; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +import com.baeldung.Application; + +@SpringBootTest(classes = Application.class) +class AddOneAndDoubleComponentUnitTest { + + @Resource + private AddOneAndDoubleComponent addOneAndDoubleComponent; + + @Resource + private AddComponent addComponent; + + @Test + void whenCallingFromExternalClass_thenAopProxyIsUsed() { + addComponent.resetCache(); + + addOneAndDoubleComponent.addOneAndDouble(0); + + assertThat(addComponent.getCounter()).isEqualTo(1); + } +} diff --git a/spring-aop-2/src/test/java/com/baeldung/internalaop/SelfInjectionUnitTest.java b/spring-aop-2/src/test/java/com/baeldung/internalaop/SelfInjectionUnitTest.java new file mode 100644 index 000000000000..aba9195f8285 --- /dev/null +++ b/spring-aop-2/src/test/java/com/baeldung/internalaop/SelfInjectionUnitTest.java @@ -0,0 +1,26 @@ +package com.baeldung.internalaop; + +import static org.assertj.core.api.Assertions.assertThat; + +import jakarta.annotation.Resource; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +import com.baeldung.Application; + +@SpringBootTest(classes = Application.class) +class SelfInjectionUnitTest { + + @Resource + private SelfInjection selfInjection; + + @Test + void whenCallingFromExternalClass_thenAopProxyIsUsed() { + selfInjection.resetCache(); + + selfInjection.addOneAndDouble(0); + + assertThat(selfInjection.getCounter()).isEqualTo(1); + } +}