복붙노트

[SPRING] 기본 AOP 프로그램이 BeanCurrentlyInCreationException을 발생시킵니다.

SPRING

기본 AOP 프로그램이 BeanCurrentlyInCreationException을 발생시킵니다.

나는 간단한 AOP 프로그램을 만들고 그것으로 BeanCurrentlyInCreationException 예외를 받기 시작했다.

여기 내 코드가 있습니다 :

MyAspect.java

package aspect;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
//@Component
public class MyAspect {

    @Pointcut("execution(public * *(..))")
    private void anyPublicOperation() {
    }

    @Before("anyPublicOperation()")
    private void beforePointCut(){
        System.out.println("Inside before pointcut of MyAspect");
    }
}

Calculator.java

package program;

import org.springframework.stereotype.Component;

@Component
public class Calculator {

    public int add(int i, int j) {
        return i + j;
    }
}

Config.java

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

import aspect.MyAspect;


@Configuration
//Enable AspectJ auto proxying
@EnableAspectJAutoProxy
@ComponentScan(basePackages={"program"})
public class Config {

    //Declare a bean
    @Bean
    public MyAspect aspect() {
        return new MyAspect();
    }
}

App.java - 메인 프로그램이 들어 있습니다 :

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import program.Calculator;

public class App {
    public static void main(String[] args) throws Exception {

        ApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);

        System.out.println("=======Calling methods========");

        Calculator cal = ctx.getBean(Calculator.class);
        int result = cal.add(10,20);
        System.out.println(result);
    }
}

프로그램을 실행하면이 예외가 발생합니다.

Aug 13, 2015 2:44:10 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@17ab5d6d: startup date [Thu Aug 13 14:44:10 IST 2015]; root of context hierarchy
Aug 13, 2015 2:44:10 PM org.springframework.context.annotation.AnnotationConfigApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'aspect' defined in Config: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [aspect.MyAspect]: Factory method 'aspect' threw exception; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'aspect': Requested bean is currently in creation: Is there an unresolvable circular reference?
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1111)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1006)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)
    at App.main(App.java:10)

내 프로그램마다 순환 의존성이 없으므로이 예외가 발생합니다.

또한 코드에 작은 변경을 가하면 잘 작동합니다. 작동하도록 변경 한 내용은 다음과 같습니다.

1) Config.java 코드의 빈 선언에 주석 처리 :

@Configuration
//Enable AspectJ auto proxying
@EnableAspectJAutoProxy
@ComponentScan(basePackages={"aspects","program"})
public class Config {

    // bean declaration is removed here and updated basePackages for @ComponentScan

}

내 aspect 클래스에서 @Component 주석을 다음과 같이 사용할 수 있습니다 :

@Aspect
@Component
public class MyAspect {

    // same as earlier code.
}

해결법

  1. ==============================

    1.나는 포스트에서 언급 된 동일한 코드 조각을 가지고 시도해 보았고 Config.java에서 Bean을 정의했고 그것은 나를 위해 일했다. 이 프로그램을 사용하여 얻은 유일한 오류는 MyAspect 클래스에 있습니다. 그만큼

    나는 포스트에서 언급 된 동일한 코드 조각을 가지고 시도해 보았고 Config.java에서 Bean을 정의했고 그것은 나를 위해 일했다. 이 프로그램을 사용하여 얻은 유일한 오류는 MyAspect 클래스에 있습니다. 그만큼

    @Before("anyPublicOperation()")
    public void beforePointCut(){
        System.out.println("Inside before pointcut of MyAspect");
    }
    

    잘 지내라. 또한 사용하기 전에 컨텍스트를 새로 고침으로써 이전 코드를 시도 할 수 있습니다.

    ApplicationContext context = new AnnotationConfigApplicationContext();
    ((AnnotationConfigApplicationContext) context).register(Config.class);
    ((AbstractApplicationContext) context).refresh(); 
    
  2. from https://stackoverflow.com/questions/31984258/basic-aop-program-throws-beancurrentlyincreationexception by cc-by-sa and MIT license