spring core aop : proxy - internal call solution
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package com.example.aop.internalcall;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 프록시 내부 호출 해결 1. 자기 자신 주입
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class CallServiceV1 {
|
||||
|
||||
// 스프링 컨테이너에서 주입 받으면 프록시 주입
|
||||
private CallServiceV1 callServiceV1;
|
||||
|
||||
// 자기 자신을 주입하므로 생성자 주입 시 순환 참조가 일어 나므로 세터로 주입
|
||||
@Autowired
|
||||
public void setCallServiceV1(CallServiceV1 callServiceV1) {
|
||||
this.callServiceV1 = callServiceV1;
|
||||
}
|
||||
|
||||
public void external() {
|
||||
log.info("call external");
|
||||
callServiceV1.internal(); // 외부 메소드 호출로 변경
|
||||
}
|
||||
|
||||
public void internal() {
|
||||
log.info("call internal");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.example.aop.internalcall;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 프록시 내부 호출 해결 2. 지연 조회 - ApplicationContext, ObjectProvider 이용
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class CallServiceV2 {
|
||||
|
||||
// private final ApplicationContext applicationContext;
|
||||
private final ObjectProvider<CallServiceV2> callServiceProvider;
|
||||
|
||||
public CallServiceV2(ObjectProvider<CallServiceV2> callServiceProvider) {
|
||||
this.callServiceProvider = callServiceProvider;
|
||||
}
|
||||
|
||||
public void external() {
|
||||
log.info("call external");
|
||||
// CallServiceV2 callServiceV2 = applicationContext.getBean(CallServiceV2.class);
|
||||
CallServiceV2 callServiceV2 = callServiceProvider.getObject();
|
||||
callServiceV2.internal(); // 외부 메소드 호출로 변경
|
||||
}
|
||||
|
||||
public void internal() {
|
||||
log.info("call internal");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.example.aop.internalcall;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 프록시 내부 호출 해결 3. 구조 변경(분리)
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class CallServiceV3 {
|
||||
|
||||
private final InternalService internalService;
|
||||
|
||||
public void external() {
|
||||
log.info("call external");
|
||||
internalService.internal(); // 외부 메소드 호출
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.example.aop.internalcall;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class InternalService {
|
||||
|
||||
public void internal() {
|
||||
log.info("call internal");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
spring.main.allow-circular-references=true
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.example.aop.internalcall;
|
||||
|
||||
import com.example.aop.internalcall.aop.CallLogAspect;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
@Slf4j
|
||||
@Import(CallLogAspect.class)
|
||||
@SpringBootTest
|
||||
class CallServiceV1Test {
|
||||
|
||||
@Autowired
|
||||
CallServiceV1 callServiceV1;
|
||||
|
||||
@Test
|
||||
void external() {
|
||||
callServiceV1.external();
|
||||
}
|
||||
|
||||
@Test
|
||||
void internal() {
|
||||
callServiceV1.internal();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.example.aop.internalcall;
|
||||
|
||||
import com.example.aop.internalcall.aop.CallLogAspect;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
@Slf4j
|
||||
@Import(CallLogAspect.class)
|
||||
@SpringBootTest
|
||||
class CallServiceV2Test {
|
||||
|
||||
@Autowired
|
||||
CallServiceV2 callServiceV2;
|
||||
|
||||
@Test
|
||||
void external() {
|
||||
callServiceV2.external();
|
||||
}
|
||||
|
||||
@Test
|
||||
void internal() {
|
||||
callServiceV2.internal();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.example.aop.internalcall;
|
||||
|
||||
import com.example.aop.internalcall.aop.CallLogAspect;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
@Slf4j
|
||||
@Import(CallLogAspect.class)
|
||||
@SpringBootTest
|
||||
class CallServiceV3Test {
|
||||
|
||||
@Autowired
|
||||
CallServiceV3 callServiceV3;
|
||||
|
||||
@Test
|
||||
void external() {
|
||||
callServiceV3.external();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user