2011-04-10 7 views
2
내가 설정 빈 속성 후 일부 초기화 작업을 수행 할 필요가

,AutowireCapableBeanFactory.autowireBean (bean) InitlizingBean.afterPropertiesSet()을 호출하지 않습니까?

ApplicationContext context = new ...; 
AutowireCapableBeanFactory factory = context.getAutowireCapableBeanFactory(); 

// autowireBean only populate the fields, but never invoke afterPropertiesSet(). 
factory.autowireBean(bean); 

// Should I set it manually? 
// if (bean instanceof InitializingBean) { 
// ((InitializingBean) bean).afterPropertiesSet(); 
// } 
// if (bean instanceof ApplicationContextAware) { 
// ((ApplicationContextAware) bean).setApplicationContext(context); 
// } 
// if ... 

답변

8

이 시도 : 그것은 (내가 추천하는) @PostConstruct와 함께 작동

factory.autowireBean(bean); 
bean = (YourBean) factory.initializeBean(bean, "anyName"); 

, 그것뿐만 아니라 afterPropertiesSet()을 실행해야하므로. anyName은 빈 이름이며, 아마도 BeanNameAware 인터페이스가 포함될 때 사용됩니다.

1

예. 문서에서는이 메소드가 빈을 자동 완성한다고 말합니다.

공장 초기화에는 초기화를 처리하는 다른 방법이 있지만 bean 정의가 필요합니다.

관련 문제