Не уверен, что мне не хватает ... Нужна помощь, пожалуйста ...
У меня уже есть приложение, которое использует одну базу данных.Сейчас я добавляю другую базу данных.Ниже приведена ошибка (сервер завершает работу при запуске), которую я получаю, когда добавляю дополнительный код для получения нового источника данных.
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);
}