Spring Boot:多个数据源-服务器在启动之前终止

时间:2019-02-11 20:47:27

标签: java spring spring-boot spring-data

不确定我缺少什么...请帮助...

我已经有一个使用一个数据库的应用程序。现在,我要添加另一个数据库。以下是错误消息(服务器在启动时终止),当我添加额外的代码以获取新的数据源时,我会收到错误消息。

application.properties

# === Existing DATA SOURCE (SQL SERVER) ===
spring.datasource.driverClassName=com.ibm.db2.jcc.DB2Driver
spring.datasource.url=jdbc:sqlserver://mysqlserver/mydb
spring.datasource.username=user1
spring.datasource.password=passwrd

# === New DATA SOURCE (SQL SERVER) ===  ADDING THIS DTASOURCE CODE
spring.db2Datasource.driverClassName=com.ibm.db2.jcc.DB2Driver
spring.db2Datasource.url=jdbc:db2://mydb2server/mydb
spring.db2Datasource.username=user1
spring.db2Datasource.password=passwrd

创建了这个新类: DatasourceConfig.java

@Configuration
public class DatasourceConfig {
    @Bean
    @Primary
    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix="spring.db2Datasource")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }
}

I have not doe any changes in my main class:
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class })
@ComponentScan(basePackages="com.my.company")
public class SpringBootAFSApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootStudentApplication.class, args);
    }
}

错误:

2019-02-11 14:43:33.323 ERROR 680 --- [ost-startStop-1] o.s.b.web.embedded.tomcat.TomcatStarter  : Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCreationException. Message: Error creating bean with name 'servletEndpointRegistrar' defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/ServletEndpointManagementContextConfiguration$WebMvcServletEndpointManagementContextConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar]: Factory method 'servletEndpointRegistrar' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'healthEndpoint' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.health.HealthEndpoint]: Factory method 'healthEndpoint' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthIndicatorAutoConfiguration': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthIndicatorAutoConfiguration$$EnhancerBySpringCGLIB$$75412098]: Constructor threw exception; nested exception is org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'secondaryDataSource': Could not bind properties to 'HikariDataSource' : prefix=spring.db2Datasource, ignoreInvalidFields=false, ignoreUnknownFields=true; nested exception is org.springframework.boot.context.properties.source.InvalidConfigurationPropertyNameException: Configuration property name 'spring.db2Datasource' is not valid

JdbcStudentRepository.java

@Repository("Student")
public class JdbcStudentRepository implements StudentRepository {
    private GeneratedKeyHolder keyHolder = new GeneratedKeyHolder();
    private NamedParameterJdbcTemplate jdbcTemplate;


    @Autowired
    public JdbcStudentRepository(NamedParameterJdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public int count(){
        return this.jdbcTemplate.queryForObject("select count(1) from STUDENT", Collections.emptyMap(), Integer.class);
    }

2 个答案:

答案 0 :(得分:1)

似乎您正在使用Hikari数据源:

  

无法将属性绑定到“ HikariDataSource”

根据文档,您需要具有稍微不同的配置:

  

此外,如果您碰巧在类路径上有Hikari,则此基本设置   不起作用,因为Hikari没有url属性(但确实有一个   jdbcUrl属性)。在这种情况下,您必须重写配置   如下:

app.datasource.jdbc-url=jdbc:mysql://localhost/test
app.datasource.username=dbuser app.datasource.password=dbpass
app.datasource.maximum-pool-size=30

完整信息和其他实施选项可以在here

中找到

答案 1 :(得分:0)

根据documentation,您的前缀错误 应该是这样的:

db.datasource.driverClassName=com.ibm.db2.jcc.DB2Driver
db.datasource.url=jdbc:db2://mydb2server/mydb
db.datasource.username=user1
db.datasource.password=passwrd

并更新以下行:

@Bean
@ConfigurationProperties(prefix="db.datasource")
public DataSource secondaryDataSource() {
    return DataSourceBuilder.create().build();
}