【发布时间】:2018-01-29 09:25:15
【问题描述】:
我想允许我的一个库的用户指定他们的 JPA 实体和 Spring Data JPA 存储库的位置,例如:
database1:
datasource:
repository-package: com.sample.database1.repositories1,com.sample.database1.repositories2
entity-packages: com.sample.database1.entities1,com.sample.database1.entities2
我的库定义:
@Configuration
@EnableJpaRepositories(basePackages = "${database1.datasource.repository-package}",
entityManagerFactoryRef = "database1EntityManagerFactory",
transactionManagerRef = "database1TransactionManager")
static class Database1DataSourceAutoConfiguration {
@Value("${database1.datasource.entity-packages}")
private String[] entityPackages;
entityPackages 被正确注入,包含一个带有两个实体包的数组。
但是,basePackages = "${database1.datasource.repository-package}" 显然不起作用,因为它引用的是字符串 "com.sample.database1.repositories1,com.sample.database1.repositories2",而不是包含两个字符串的数组。
有没有办法可以将 YML 文件中的 String[] 注入到该注释属性中?如果没有,有什么解决方法吗?
@EnableJpaRepositories(basePackages = { ??? }
【问题讨论】:
-
You can try this!我用这个实现多数据源。
标签: spring spring-boot spring-data spring-data-jpa