【发布时间】:2019-08-09 16:23:07
【问题描述】:
如果我有一个以编程方式管理的 LocalContainerEntityManagerFactoryBean,例如带有 @Bean 注释的方法:
@Configuration
@EnableJpaRepositories(
entityManagerFactoryRef = "myEntityManager",
transactionManagerRef = "myTransactionManager",
basePackages = {"com.mycompany.my.repository"}
)
public class MyDbConfig {
@Bean
public LocalContainerEntityManagerFactoryBean mcEntityManager() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
//some production config here
return em;
}
}
如何添加这个仅用于集成测试的小 JPA 属性,最好是通过下面的监听器(在测试类路径中)
public class AfterAllBeansCreatedListener {
@Autowired
private LocalContainerEntityManagerFactoryBean em
@PostConstruct
private void reconfigure() {
HashMap<String, Object> properties = em.getJpaPropertyMap()
properties.put("hibernate.hbm2ddl.auto", "create");
em.setJpaPropertyMap(properties);
}
}
目前,我在嵌套的@TestConfiguration 中使用另一种带有@Bean 注释的方法生成LocalContainerEntityManagerFactoryBean,但这意味着我需要复制该bean 的所有其他创建逻辑,这当然是纯粹的邪恶。
【问题讨论】:
标签: spring spring-boot spring-boot-test