Java/SpringBoot

[SpringBoot] 종속성 순환 에러 트러블슈팅

JoJun's 2021. 12. 30. 16:42
728x90
반응형

AOP (Aspect Oriented Programming)를 공부하다가 "종속성 순환 에러"가 발생하였다.

해당 이슈에 대해 슈팅해보겠다.


● 트러블 이슈

각 메서드 ( Controller, Service, Repository )에서 처리 시간을 처리하는 AOP를 만드는 도중 아래 에러 메시지가 발생하였다.

The dependencies of some of the beans in the application context form a cycle:

   memberController defined in file [C:\Users\pc\Desktop\hello-spring\hello-spring\out\production\classes\hello\hellospring\controller\MemberController.class]
      ↓
   memberService defined in class path resource [hello/hellospring/SpringConfig.class]
┌─────┐
|  timeTraceAop defined in class path resource [hello/hellospring/SpringConfig.class]
└─────┘

애플리케이션 콘텍스트에서 일부 Bean의 종속성은 순환을 형성합니다.라는 에러 메시지였고,
timeTraceAop 객체가 계속 순환을 형성하고 있다.


● 분석

TImeTraceAop 객체를 살펴보자

package hello.hellospring.aop;

import org.aspectj.lang.ProceedingJoinPoint;  
import org.aspectj.lang.annotation.Around;  
import org.aspectj.lang.annotation.Aspect;

@Aspect  
public class TimeTraceAop {  
	@Around("execution(\* hello.hellospring..\*(..))")  
	public Object execute(ProceedingJoinPoint joinPoint) throws Throwable {  
		long start = System.currentTimeMillis();
	
		System.out.println("START: " + joinPoint.toString());

		try{
			return joinPoint.proceed();
		}finally {
			long finish = System.currentTimeMillis();
			long timeMs = finish - start;
			
			System.out.println("END: " + joinPoint.toString() + " "+ timeMs + "ms");
		}
	}
}

 

TimeTraceAop객체의 패키지 경로를 보면 package hello.hellospring.aop;이며
Around execution 전략에 포함되는 패키지 경로를 가지고 있다.



● 원인

TimeTraceAop의 execution 함수가 자신의 execution메소드에도 실행이 되어 계속 순환이 됨.



● 해결


public class TimeTraceAop {
    @Around("execution(* hello.hellospring..*(..)) && !target(hello.hellospring.SpringConfig)")
    public Object execute(ProceedingJoinPoint joinPoint) throws Throwable{

SpringConfig에서 TimeTraceAop를 Bean등록을 해주고 있다. execution에 hello.hellospring.SpringConfig 경로를 제외시켜줌으로써 해결했다.

728x90
반응형