我有以下結構:多模塊項目春季3.0 AnnotationSessionFactoryBean packagesToScan屬性值設置模塊明智
共模塊
- 包含相關的通用模塊,服務和持久性API
- 包含一個常見的持久性上下文test-common-persistence-context.xml。
搜索模塊(取決於通用模塊)
- 包含搜索模塊相關的模型bean(標有JPA實體的註解),服務和持久性API
- 包含搜索模塊相關的春天上下文文件
預約模塊(取決於公共模塊) - 包含預訂模塊相關的模型bean(標有JPA實體的註解),服務和持久性API - 包含訂票模塊相關的Spring上下文文件
在通用模塊我有測試共持久化上下文。 XML。對於SessionFactory的有型豆 org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean我需要屬性「packagesToScan」值設置爲在JPA Entity註解標註模型bean的包present.Without它,我得到例外未知實體:MY_ENTITY_NAME
通用模塊:測試共持續-context.xml的
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- Property Place Holder -->
<context:property-placeholder location="classpath:test-common-persistence-bundle.properties" />
<!-- Data Source -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${test.db.driverClassName}"/>
<property name="url" value="${test.db.jdbc.url}"/>
<property name="username" value="${test.db.username}"/>
<property name="password" value="${test.db.password}"/>
</bean>
<!--
Hibernate Configuration
-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="${test.packages.to.scan.jpa.annotations}"/>
<property name="hibernateProperties">
<value>
<!-- SQL dialect -->
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
hibernate.hbm2ddl.auto=update
</value>
</property>
</bean>
<!-- Transaction Manager -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
在我的共同模塊沒有JPA實體,所以我沒有任何包掃描,所以我保持「packagesToScan」屬性值爲空
不過,我需要以使各模塊中的JPA實體被檢測到使用相同的持久化上下文在我的搜索模塊和預約模塊測試(SearchPersistenceTestBase.java)。
搜索模塊:SearchPersistenceTestBase.java
@Ignore
@ContextConfiguration(locations = {
"classpath:test-common-persistence-context.xml",
"classpath:test-search-spring-context.xml"})
@TransactionConfiguration(transactionManager="txManager", defaultRollback=true)
public class SearchPersistenceTestBase extends AbstractTransactionalJUnit4SpringContextTests {
}
任何人都可以請指導我如何實現與建立我上面顯示這個期望的行爲?
**我試過的方法**
我想過使用額外的java.lang。字符串型豆,其值從屬性文件中設置
<bean id="entityPackagesToScan" class="java.lang.String">
<constructor-arg value="${test.packages.to.scan.jpa.annotations}" />
</bean>
其中 test.packages.to.scan.jpa.annotations在test-common-persistence-bundle.properties定義爲空
然後我覆蓋測試搜索彈簧的context.xml bean定義
測試搜索彈簧的context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- Property Place Holder -->
<context:property-placeholder location="classpath:test-search-bundle.properties" />
.. context-component scan elements here
<bean id="entityPackagesToScan" class="java.lang.String">
<constructor-arg value="${test.packages.to.scan.jpa.annotations}" />
</bean>
</beans>
其中 test.packages.to.scan.jpa.annotations在test-search-bundle.properties
被定義爲 「com.search.model」 但這並沒有工作我得到異常未知實體:MY_ENTITY_NAME
感謝,
Jignesh