2010-12-28 7 views
0

BeanDefinitionRegistryPostProcessor를 구현하는 클래스가 있습니다.프로그래밍 방식으로 AnnotationSessionFactoryBean 추가

postProcessBeanFactory 또는 postProcessBeanDefinitionRegistry에서 Spring 컨텍스트에 AnnotationSessionFactoryBean을 추가하려고합니다. 런타임에 객체를 구성 할 수 있도록 프로그래밍 방식으로이 작업을 수행해야합니다.

내가 할 노력하고 있어요 : 당신의 도움에 대한

@Override 
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry bdr) throws BeansException { 

RootBeanDefinition bd = new RootBeanDefinition(
AnnotationSessionFactoryBean.class); 

      // fails here.. can not cast 
AnnotationSessionFactoryBean asfb = (AnnotationSessionFactoryBean)bd; 

bdr.registerBeanDefinition("sessionFactory", asfb); 

감사

이 솔루션 --updated :

GenericBeanDefinition bd = new GenericBeanDefinition(); 
    bd.setBeanClass(AnnotationSessionFactoryBean.class); 
    bd.getPropertyValues().add("dataSource", dataSource); 
      bdr.registerBeanDefinition("sessionFactory", bd); 

답변

1

bean 정의 :

는해야했다 이 아니고 실제 빈인이므로 캐스팅 할 수 없습니다.

RootBeanDefinition 대신 GenericBeanDefinition을 사용하십시오. 그런 다음 Bean 정의의 을 사용하여 AnnotationSessionFactoryBean에 필요한 Bean을 설정할 수 있습니다. 당신 원본, 또는 다른 속성이 아직 정의되지 않은 문제가 발생하는 경우, 당신은 RuntimeBeanReference를 사용할 수

bd.getPropertyValues().add("dataSource", dataSource); 
bd.getPropertyValues().add("annotatedClasses", listOfClasses); 
etc... 

참고 :

이 방법, 당신은 호출 registerBeanDefinition()하기 전에 다음과 같은 일을 할 수 있습니다 대신 bd.getPropertyValues().add("dataSource", new RuntimeBeanReference("dataSource")과 같은 작업을 수행하여 자리 표시 자로

관련 문제