I'm facing an issue trying to define a context hierarchy using AnnotationConfigApplicationContext
.
The problem is when defining a module context inside beanRefContext.xml
and setting the 'parent' property with another context (XML/Annotated based).
Example:
beanRefContext.xml in module A
<bean id="moduleA_ApplicationContext" class="org.springframework.context.support.ClassPathXmlApplicationContext"> <property name="configLocations"> <list> <value>classpath:db-context.xml</value> </list> </property> </bean>
db-context.xml
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="org.h2.Driver" p:url="jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;MODE=MySQL;TRACE_LEVEL_SYSTEM_OUT=2"/> <!-- Hibernate Session Factory --> <bean name="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="useTransactionAwareDataSource" value="true"/> <property name="packagesToScan"> <list> <value>com.example.model</value> </list> </property> <property name="hibernateProperties"> <!-- hibernate props --> </property> </bean>
beanRefContext.xml in module B
<bean id="moduleB_ApplicationContext" class="org.springframework.context.annotation.AnnotationConfigApplicationContext" > <property name="parent" ref="moduleA_ApplicationContext"/> <constructor-arg> <list> <value>com.example.dao</value> </list> </constructor-arg> </bean>
FooHibernateDao
class FooHibernateDao implements FooDao { @Autowired @Qualifier("sessionFactory") private SessionFactory sessionsFactory; // CRUD methods }
Module B application context fails to find bean defined in module A application context.
From looking at the code of AnnotationConfigApplicationContext
it seems that the scanning process doesn't use the parent as a reference to resolve beans.
Is there something I'm doing wrong or my attempt to create a hierarchy is impossible with annotation configuration?