不确定我缺少什么...需要帮助...
我已经有一个使用一个数据库的应用程序。现在我正在添加另一个数据库。以下是当我添加额外代码以获取新数据源时出现的错误(服务器在启动时终止)。
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);
}
最佳答案
您似乎正在使用 Hikari 数据源:
Could not bind properties to 'HikariDataSource'
根据文档,您需要有稍微不同的配置:
Also, if you happen to have Hikari on the classpath, this basic setup does not work, because Hikari has no url property (but does have a jdbcUrl property). In that case, you must rewrite your configuration as follows:
app.datasource.jdbc-url=jdbc:mysql://localhost/test app.datasource.username=dbuser app.datasource.password=dbpass app.datasource.maximum-pool-size=30
可以找到完整信息和其他实现选项 here
关于java - Spring 启动: Multiple datasource - Server Terminates before starting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54638887/