2

컨테이너로 관리되지 않는 객체에 대해 @Configurable 스프링 주석을 사용하기 위해 컴파일 타임 위빙을 통해 AspectJ에서 Spring을 사용하고 있습니다. 컨텍스트가 생성 된 후에 I는 TestConfigurable을 만들면@Configurable은 @PostConstruct 메소드로 초기화 된 객체에 대해 작동하지 않습니다.

@Component 
public class TestComponent {} 

가 TestComponent가 미세 주입 :이 개체에 주입있어

@Configurable(autowire = Autowire.BY_TYPE) 
public class TestConfigurable { 

    private TestComponent component; 

    public TestComponent getComponent() { 
     return component; 
    } 

    @Autowired 
    public void setComponent(TestComponent component) { 
     this.component = component; 
    } 
} 

성분 : 여기

는 구성 - 주석 객체 @ 예는 하지만 @ PostConstruct-annotated 메서드에서 그렇게 할 때 autowiring이 발생하지 않습니다. @PostConstruct와

구성 요소 : 여기

public class TestApplication { 

    public static void main(String[] args) { 
     ConfigurableApplicationContext applicationContext = new ClassPathXmlApplicationContext("/application-context.xml"); 
     applicationContext.registerShutdownHook(); 

     TestConfigurable configurable = new TestConfigurable(); 
     System.out.println("After context is loaded: " + configurable.getComponent()); 
    } 
} 

이 응용 프로그램이 생성하는 출력 : 내가 실행하고있어

@Component 
public class TestPostConstruct { 

    @PostConstruct 
    public void initialize() { 
     TestConfigurable configurable = new TestConfigurable(); 
     System.out.println("In post construct: " + configurable.getComponent()); 
    } 
} 

응용 프로그램

In post construct: null 
After context is loaded: [email protected] 

그래서 거기있는 @Configurable 객체에 종속성을 주입하는 해결 방법 @PostConstruct 메서드 안에 ated? 모든 @Component-annotated Bean은 이미 컨텍스트에 있고 @PostConstruct가 호출 될 때 자동으로 이미 완료되었습니다. Spring이 여기에서 autowiring을하지 않는 이유는 무엇입니까? ..

문제를 해결하는 데 도움이되는 다른 구성 파일 (context 및 pom.xml)을 게시 할 수 있습니다.

업데이트 1 : 문제의 원인을 찾은 것 같습니다. @Configurable로 주석 된 객체는 BeanFactoryAware를 구현하는 AnnotationBeanConfigurerAspect에 의해 초기화됩니다. 이 부분은 BeanFactory를 사용하여 Bean을 초기화한다. BeanFactory가 AnnotationBeanConfigurerAspect로 설정되기 전에 TestPostConstruct 객체의 @PostConstruct 메소드가 실행됩니다. 로거가 디버그되도록 설정하면 다음 메시지가 콘솔에 출력됩니다 : "BeanFactory가 설정되지 않았습니다 ... :이 컨피규러가 Spring 컨테이너에서 실행되는지 확인하십시오 .. 유형이 [...] 인 bean을 구성 할 수 없습니다. . "

문제가 어떤 해결 방법은 여전히 ​​나를 위해 열려있는 경우 ...

+0

문제의 해결 방법은 아니지만'@ Configurable'을 사용하는 대신 단순히 구성 요소에 팩토리를 삽입 할 수 있습니다. –

답변

3

나는이 하나의 해결 방법을 발견했다.

@DependsOn("org.springframework.context.config.internalBeanConfigurerAspect") 

희망이 정보가 누군가를 위해 도움이 될 것입니다 : @PostConstruct 당신은 @PostConstruct 방법으로 클래스에 다음과 같은 주석을 추가 할 수 있습니다 실행되기 전에 AnnotationBeanConfigurerAspect 측면을 초기화합니다.

+0

이것이 정답이라고 생각하면 질문을 끝내기 위해 자신의 대답을 수락 할 수 있습니다. – kriegaex