我想配置Spring Boot以使用2个JNDI数据源。我尝试了这个配置:
应用性质
spring.production-datasource.jndi-name=java:/global/production_gateway
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.warehouse-datasource.jndi-name=java:/global/production_warehouse
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
主数据库
@Configuration
@EnableJpaRepositories(
basePackages = "org.datalis.plugin.production.entity",
entityManagerFactoryRef = "productionEntityManager",
transactionManagerRef = "productionTransactionManager"
)
@EnableTransactionManagement
public class ContextProductionDatasource {
@Autowired
private Environment env;
@Primary
@Bean
@ConfigurationProperties(prefix="spring.production-datasource")
public DataSource productionDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean
public EntityManager productionEntityManager(EntityManagerFactory emf) {
return emf.createEntityManager();
}
@Primary
@Bean
public PlatformTransactionManager productionTransactionManager(final EntityManagerFactory emf) {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
@Primary
@Bean
public PersistenceExceptionTranslationPostProcessor productionExceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
}
第二个数据源:
@Configuration
@EnableJpaRepositories(
basePackages = "org.datalis.plugin.warehouse.entity",
entityManagerFactoryRef = "warehouseEntityManager",
transactionManagerRef = "warehouseTransactionManager"
)
@EnableTransactionManagement
public class ContextWarehouseDatasource {
@Autowired
private Environment env;
@Primary
@Bean
@ConfigurationProperties(prefix="spring.warehouse-datasource")
public DataSource warehouseDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public EntityManager warehouseEntityManager(EntityManagerFactory emf) {
return emf.createEntityManager();
}
@Bean
public PlatformTransactionManager warehouseTransactionManager(final EntityManagerFactory emf) {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor warehouseExceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
}
部署代码时,会出现异常:
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: more than one 'primary' bean found among candidates: [productio nDataSource, warehouseDataSource]"}}
完整错误堆栈:
https://pastebin.com/EsNp2Fp9
你知道我怎样才能解决这个问题吗?
只能有一个主数据源。其他混乱看起来不错。
假设ContextWarehouse数据源
为辅助连接
像这样从warehouseDataSource()
中删除@Primary
,然后重试。
@Bean
@ConfigurationProperties(prefix="spring.warehouse-datasource")
public DataSource warehouseDataSource() {
return DataSourceBuilder.create().build();
}
我通过使用Spring和Hibernate创建实体、服务和服务的JUnit测试开始了我的项目。所有这些都很管用。然后,我添加了spring-mvc来使用许多不同的分步教程来制作这个web应用程序,但是当我试图使用注释制作Controller时,我在部署期间从Glassfish得到错误。我想由于某种原因Spring没有看到我的服务,但经过多次尝试,我仍然无法处理它。 /webapp/web-inf/
UnsatisfiedDependencyException:在文件[/home/x/workspace/botshop-backend-java/target/classes/far/botshop/backend/storage/filesystemStorageService.class]中创建名为“file SystemStorageService”的bean时出错:通过构造函数参数0表示
在运行该项目时,我出现了以下错误
我正在运行一个JUnit测试,嗯,我的Spring启动项目,我是这样写的: 找到依赖项[com.br.suppcomm.ocp.dao.logindao]得[com.br.suppcomm.ocp.dao.logindao]:需要至少1个具有此依赖项自动候选资格得bean.依赖项注释:{@org.SpringFramework.Beans.Factory.Annotation.AutoWired(