这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
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
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 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);
}

}
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 AddOneAndDoubleComponentUnitTest {

@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 SelfInjectionUnitTest {

@Resource
private SelfInjection selfInjection;

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

selfInjection.addOneAndDouble(0);

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