繁体   English   中英

Spring Boot的DataSource外部化配置不起作用

[英]Spring Boot externalised configuration for DataSource not working

我有一个应用程序-使用Spring 4.3.6和Spring Boot 1.4.4-将被导出为JAR。 我想连接到远程Oracle数据库,但是在不破坏应用程序的情况下无法外部化配置。

这是我当前的解决方法:

import org.apache.tomcat.jdbc.pool.DataSource;

@Bean
public DataSource dataSource() {
  DataSource dataSource = new DataSource();

  dataSource.setUrl("jdbc:oracle:thin:@ip-address:port:orcl");
  dataSource.setUsername("user");
  dataSource.setPassword("password");
  dataSource.setDriverClassName("oracle.jdbc.OracleDriver");

  return dataSource;
}

通过以上操作,我的应用程序能够连接到数据库并成功执行查询。 但是,当我尝试将配置外部化时,如下所示:

@Bean
@ConfigurationProperties(prefix="app.datasource")
public DataSource dataSource() {
  return new DataSource();
}

// application.properties
app.datasource.url=jdbc:oracle:thin:@ip-address:port:orcl
app.datasource.username=user
app.datasource.password=password
app.datasource.driver-class-name=oracle.jdbc.OracleDriver

当尝试在我的Spring Boot Controller中执行jdbcTemplate.update(query)时,我将得到以下错误(请注意,没有外部化上述工作):

org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: The url cannot be null

我试图删除@ConfigurationProperties并将app.datasource更改为spring.datasource 我也尝试过使用DataSourceBuilder.create().build()返回javax.sql.DataSource但是在两种情况下都会引发相同的错误。

我做错了。 成功外部化配置的正确方法是什么?

假设您有两个不同的Oracle数据库的两个数据源。 然后,您具有以下属性文件:

/path/to/config/application.properties

oracle1.username=YourUser1
oracle1.password=YourPassword1
oracle1.url=jdbc:oracle:thin:@localhost:1521:XE

oracle2.username=YourUser2
oracle2.password=YourPassword2
oracle2.url=jdbc:oracle:thin:@192.168.0.3:1521:XE

然后在配置文件中:

import oracle.jdbc.pool.OracleDataSource;

@Configuration
public class DatasourcesConfig {

@Autowired
private Environment env;

@Primary
@Bean(name = "dataSource1")
DataSource oracle1DataSource() throws SQLException {

    OracleDataSource dataSource = new OracleDataSource();
    dataSource.setUser(env.getProperty("oracle1.username"));
    dataSource.setPassword(env.getProperty("oracle1.password"));
    dataSource.setURL(env.getProperty("oracle1.url"));
    return dataSource;
}

@Bean(name = "dataSource2")
DataSource oracle2DataSource() throws SQLException {

    OracleDataSource dataSource = new OracleDataSource();
    dataSource.setUser(env.getProperty("oracle2.username"));
    dataSource.setPassword(env.getProperty("oracle2.password"));
    dataSource.setURL(env.getProperty("oracle2.url"));
    return dataSource;
  }
}

如果要在运行jar时指定application.properties文件的外部位置,然后将spring.config.location设置为系统属性,则可以尝试:

java -jar target/your-application-0.0.1.jar -Dspring.config.location=/path/to/config/

确保在构建jar时排除了application.properties文件

无需自己创建DataSource

确保您有

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
</dependency>

在您的类路径和oracle驱动程序中进行依赖,并在application.properties文件中放置以下属性:

spring.datasource.url=jdbc:oracle:thin:@ip-address:port:orcl
spring.datasource.username=user
spring.datasource.password=password
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver

之后,您应该可以@Autowired您的DataSource

有关更多信息,请参见: https : //docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html#boot-features-connect-to-production-database

您不能覆盖Spring Boot提供的预定义属性。

只需在application.properties文件中使用以下属性。

spring.datasource.url=jdbc:oracle:thin:@ip-address:port:orcl
spring.datasource.data-username=user
spring.datasource.data-password=password
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver

另请参阅: https : //docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

除此之外,在类级别使用@ConfigurationProperties进行澄清,并且前缀"app"而不是"app.datasource"

@ConfigurationProperties(prefix = "app")

现在,您有一个名为DbProperties的类,并且该类的属性与application.properties键的最后一部分相同

public class DBProperties {
    private String url;
    private String username;
    private String password;
    // setters and getters
}

现在,实际的config / component类应该如下所示。

@Component
@ConfigurationProperties(prefix = "app")
public class MyComponent {
    DBProperties datasource = new DBProperties();

    public DBProperties getDatasource() {
        return datasource;
    }

    public void setDatasource(DBProperties datasource) {
        this.datasource = datasource;
    }    
}

请注意

  1. 实例变量名称是datasource ,它与键的第二部分相同
  2. datasource是一个类级别的实例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM