多个 Spring 数据源:考虑定义 ExternalJarRepository 类型的 bean
Multiple Spring Datasource : Consider defining bean of type ExternalJarRepository
@SpringBootApplication
@EnableJpaRepositories(basePackages=..,entityManagerRef=..)
@ComponentScan("com.main", "com.external.jar")
public class Application{
...
//methods defining primary datasource, transaction manager, entityManagerFactoryBean
}
外部罐子
@Configuration
@EnableJpaRepositories(basePackages="com.external.model", entityManagerRef=.., transactionManager=..)
public class ExternalDataConfig{
//methods defining datasource, transaction manager, entityManagerFactoryBean
}
public DataRepository extends CrudRepository<T,ID>{}
@Service
public class ExternalService{
@Autowire
private DataRepository externalRepository;
}
错误
Field externalRepository in com.external.jar.ExternalService required a bean of type DataRepository'
但是日志显示默认和外部存储库都已成功扫描,不确定为什么依赖注入机制无法找到 bean
.s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
.s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
.s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 55ms. Found 3 JPA repository interfaces.
.s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
.s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
.s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 75ms. Found 21 JPA repository interfaces.
事实证明,默认应用程序也有一个名为 DataRepository
的 bean,并且应用程序设置允许覆盖 bean
spring:
main:
allow-bean-definition-overriding: true
重命名外部依赖中的 DataRepository
bean 成功了
@SpringBootApplication
@EnableJpaRepositories(basePackages=..,entityManagerRef=..)
@ComponentScan("com.main", "com.external.jar")
public class Application{
...
//methods defining primary datasource, transaction manager, entityManagerFactoryBean
}
外部罐子
@Configuration
@EnableJpaRepositories(basePackages="com.external.model", entityManagerRef=.., transactionManager=..)
public class ExternalDataConfig{
//methods defining datasource, transaction manager, entityManagerFactoryBean
}
public DataRepository extends CrudRepository<T,ID>{}
@Service
public class ExternalService{
@Autowire
private DataRepository externalRepository;
}
错误
Field externalRepository in com.external.jar.ExternalService required a bean of type DataRepository'
但是日志显示默认和外部存储库都已成功扫描,不确定为什么依赖注入机制无法找到 bean
.s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
.s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
.s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 55ms. Found 3 JPA repository interfaces.
.s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
.s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
.s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 75ms. Found 21 JPA repository interfaces.
事实证明,默认应用程序也有一个名为 DataRepository
的 bean,并且应用程序设置允许覆盖 bean
spring:
main:
allow-bean-definition-overriding: true
重命名外部依赖中的 DataRepository
bean 成功了