Skip to content

Spring AOP for a method call from within the same Class #18515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions spring-aop-2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ This module contains articles about Spring aspect oriented programming (AOP)
- [How to Test a Spring AOP Aspect](https://www.baeldung.com/spring-aop-test-aspect)
- [Advise Methods on Annotated Classes With AspectJ](https://www.baeldung.com/aspectj-advise-methods)
- [Joinpoint vs. ProceedingJoinPoint in AspectJ](https://www.baeldung.com/aspectj-joinpoint-proceedingjoinpoint)
- [Spring AOP for a Method Call within the Same Class](TODO)

- More articles: [[<-- prev]](/spring-aop)
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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 AddComponentIntegrationTest {

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

}
Original file line number Diff line number Diff line change
@@ -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 AddOneAndDoubleComponentIntegrationTest {

@Resource
private AddOneAndDoubleComponent addOneAndDoubleComponent;

@Resource
private AddComponent addComponent;

@Test
void whenCallingFromExternalClass_thenAopProxyIsUsed() {
addComponent.resetCache();

addOneAndDoubleComponent.addOneAndDouble(0);

assertThat(addComponent.getCounter()).isEqualTo(1);
}
}
Original file line number Diff line number Diff line change
@@ -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 SelfInjectionIntegrationTest {

@Resource
private SelfInjection selfInjection;

@Test
void whenCallingFromExternalClass_thenAopProxyIsUsed() {
selfInjection.resetCache();

selfInjection.addOneAndDouble(0);

assertThat(selfInjection.getCounter()).isEqualTo(1);
}
}