我在使用
在模块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"/>
在模块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
}
模块B应用程序上下文无法找到在模块A应用程序上下文中定义的bean。从查看
AnnotationConfigApplicationContext
定义上下文层次结构时遇到了问题。问题出现在beanRefContext.xml
中定义模块上下文并将“parent”属性设置为另一个上下文(基于XML/注释)。例如:在模块A中的
beanRefContext.xml
:<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"/>
在模块B中的
beanRefContext.xml
:<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
}
模块B应用程序上下文无法找到在模块A应用程序上下文中定义的bean。从查看
AnnotationConfigApplicationContext
的代码来看,扫描过程似乎不使用父级作为引用来解析bean。是否有什么我做错了或者我的尝试使用注释配置创建层次结构是不可能的?