Programatically Add a AnnotationSessionFactoryBean
- by user146714
Hi,
I have a class that implements BeanDefinitionRegistryPostProcessor.
I am trying to add an AnnotationSessionFactoryBean to my Spring Context in either postProcessBeanFactory or postProcessBeanDefinitionRegistry. I need to do this programatically so that I can configure the object at runtime.
I am trying to do:
@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);
thanks for your help
--updated with solution:
had to do a:
GenericBeanDefinition bd = new GenericBeanDefinition();
bd.setBeanClass(AnnotationSessionFactoryBean.class);
bd.getPropertyValues().add("dataSource", dataSource);
bdr.registerBeanDefinition("sessionFactory", bd);