개념
Self-Invocation은 객체가 자기 자신의 메서드를 호출하는 것을 의미합니다.
즉, 클래스 내부에서 자기 자신의 다른 메서드를 호출하는 경우입니다.
하지만, Spring AOP에서 Self-Invocation은 AOP가 적용되지 않는 문제를 일으킬 수 있습니다.
1. Self-Invocation 예제
@Service
public class MyService {
public void methodA() {
System.out.println("Method A 실행");
methodB(); // Self-Invocation (자기 자신 호출)
}
public void methodB() {
System.out.println("Method B 실행");
}
}
위 코드에서 methodA() 는 methodB() 를 직접 호출합니다.
이런 경우 Spring AOP의 프록시 객체가 적용되지 않아서 methodB() 에는 Advice가 실행되지 않습니다.
2. Self-Invocation과 AOP 적용 문제
Spring AOP는 프록시 객체를 이용하여 Advice를 적용합니다.
즉, @Aspect가 적용된 메서드는 프록시 객체를 통해 호출될 때만 AOP가 적용됩니다.
하지만, self-invocation은 같은 객체의 내부에서 직접 호출하는 것이므로 프록시를 거치지 않고 실행됩니다.
이 때문에 AOP가 적용되지 않는 문제가 발생합니다.
3. Self-Invocation 문제 해결 방법
해결방법 1 : 별도의 Bean을 통해 호출
methodB()를 다른 Bean으로 분리하여 호출하면 프록시 객체를 통해 AOP가 정상 동작합니다.
@Service
public class MyService {
@Autowired
private AnotherService anotherService;
public void methodA() {
System.out.println("Method A 실행");
anotherService.methodB(); // 프록시 객체를 통해 호출 → AOP 적용됨
}
}
@Service
public class AnotherService {
public void methodB() {
System.out.println("Method B 실행");
}
}
해결방법 2. ApplicationContext 사용하여 프록시 객체 참조
ApplicationContext를 사용하면 자기 자신을 프록시 객체로 가져와서 호출할 수 있습니다.
@Service
public class MyService implements ApplicationContextAware {
private MyService selfProxy;
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.selfProxy = applicationContext.getBean(MyService.class);
}
public void methodA() {
System.out.println("Method A 실행");
selfProxy.methodB(); // 프록시 객체를 통해 호출 → AOP 적용됨
}
public void methodB() {
System.out.println("Method B 실행");
}
}
setApplicationContext() 를 통해 프록시 객체를 가져와서 selfProxy.methodB() 를 호출.
이렇게 하면 프록시를 거치므로 AOP 정상 동작
'Spring' 카테고리의 다른 글
Spring | ApplicationContext 란? (1) | 2025.03.12 |
---|---|
Spring | 프록시(Proxy)란? - AOP와 프록시 기반 동작 방식 (0) | 2025.03.12 |
Spring Boot | Intellij Packages 인텔리제이 패키지 합쳐져 보이는 현상 해결하는 방법 (0) | 2024.12.13 |
Spring Boot | 에러해결 - TypedResolveException: Could not resolve all dependencies for configuration ':compileClasspath' (0) | 2024.12.13 |
Spring Security | WebSecurity, HttpSecurity 두가지 주요 클래스에 대하여. (0) | 2024.03.13 |
개념
Self-Invocation은 객체가 자기 자신의 메서드를 호출하는 것을 의미합니다.
즉, 클래스 내부에서 자기 자신의 다른 메서드를 호출하는 경우입니다.
하지만, Spring AOP에서 Self-Invocation은 AOP가 적용되지 않는 문제를 일으킬 수 있습니다.
1. Self-Invocation 예제
@Service
public class MyService {
public void methodA() {
System.out.println("Method A 실행");
methodB(); // Self-Invocation (자기 자신 호출)
}
public void methodB() {
System.out.println("Method B 실행");
}
}
위 코드에서 methodA() 는 methodB() 를 직접 호출합니다.
이런 경우 Spring AOP의 프록시 객체가 적용되지 않아서 methodB() 에는 Advice가 실행되지 않습니다.
2. Self-Invocation과 AOP 적용 문제
Spring AOP는 프록시 객체를 이용하여 Advice를 적용합니다.
즉, @Aspect가 적용된 메서드는 프록시 객체를 통해 호출될 때만 AOP가 적용됩니다.
하지만, self-invocation은 같은 객체의 내부에서 직접 호출하는 것이므로 프록시를 거치지 않고 실행됩니다.
이 때문에 AOP가 적용되지 않는 문제가 발생합니다.
3. Self-Invocation 문제 해결 방법
해결방법 1 : 별도의 Bean을 통해 호출
methodB()를 다른 Bean으로 분리하여 호출하면 프록시 객체를 통해 AOP가 정상 동작합니다.
@Service
public class MyService {
@Autowired
private AnotherService anotherService;
public void methodA() {
System.out.println("Method A 실행");
anotherService.methodB(); // 프록시 객체를 통해 호출 → AOP 적용됨
}
}
@Service
public class AnotherService {
public void methodB() {
System.out.println("Method B 실행");
}
}
해결방법 2. ApplicationContext 사용하여 프록시 객체 참조
ApplicationContext를 사용하면 자기 자신을 프록시 객체로 가져와서 호출할 수 있습니다.
@Service
public class MyService implements ApplicationContextAware {
private MyService selfProxy;
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.selfProxy = applicationContext.getBean(MyService.class);
}
public void methodA() {
System.out.println("Method A 실행");
selfProxy.methodB(); // 프록시 객체를 통해 호출 → AOP 적용됨
}
public void methodB() {
System.out.println("Method B 실행");
}
}
setApplicationContext() 를 통해 프록시 객체를 가져와서 selfProxy.methodB() 를 호출.
이렇게 하면 프록시를 거치므로 AOP 정상 동작
'Spring' 카테고리의 다른 글
Spring | ApplicationContext 란? (1) | 2025.03.12 |
---|---|
Spring | 프록시(Proxy)란? - AOP와 프록시 기반 동작 방식 (0) | 2025.03.12 |
Spring Boot | Intellij Packages 인텔리제이 패키지 합쳐져 보이는 현상 해결하는 방법 (0) | 2024.12.13 |
Spring Boot | 에러해결 - TypedResolveException: Could not resolve all dependencies for configuration ':compileClasspath' (0) | 2024.12.13 |
Spring Security | WebSecurity, HttpSecurity 두가지 주요 클래스에 대하여. (0) | 2024.03.13 |