我正在使用以下配置进行数据库集成测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring/spring-master.xml"})
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
@Transactional()
public class EmployeeAccessPropertyTest {
spring-master.xml 按以下顺序加载spring配置文件:
<import resource="classpath:spring/db/dev-datasource-config.xml"/>
<import resource="classpath:spring/db/jpa-config.xml"/>
<import resource="classpath:spring/db/transaction-config.xml"/>
<import resource="classpath:spring/db/component-config.xml"/>
dev-datasource-config.xml 定义dataSource并通过liquibase初始化db方案:
<context:property-placeholder location="classpath:db/config/dev-data-access.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="liquibase" class="liquibase.integration.spring.SpringLiquibase">
<property name="dataSource" ref="dataSource" />
<property name="changeLog" value="classpath:db/changelog/db.changelog-master.xml" />
<property name="dropFirst" value="true" />
</bean>
现在,当我运行测试时,我收到以下错误:
javax.persistence.PersistenceException: [PersistenceUnit: studentPersistenceUnit] Unable to build EntityManagerFactory
...
Caused by: org.hibernate.HibernateException: Missing table: employees
看来,当初始化entityManagerFactory时(在 jpa-config.xml 文件中),不会创建表。好的,我首先通过liquibase maven插件生成方案:
mvn initialize liquibase:update
通过Spring上下文注释以下数据库方案生成:
<!--<bean id="liquibase" class="liquibase.integration.spring.SpringLiquibase">-->
<!--<property name="dataSource" ref="dataSource" />-->
<!--<property name="changeLog" value="classpath:db/changelog/db.changelog-master.xml" />-->
<!--<property name="dropFirst" value="true" />-->
<!--</bean>-->
问题已经消失。我该怎么做才能确保Spring首先加载dev-datasource-config.xml,初始化liquibase bean,然后只加载jpa-config.xml,如下所示:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource"/>
答案 0 :(得分:1)
使用'依赖'属性,如春季手册中所述:
E.g。
<bean id="beanOne" class="ExampleBean" depends-on="manager"/>
<bean id="manager" class="ManagerBean" />