Java spring boot:如何从应用程序属性配置数据源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52801624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:37:06  来源:igfitidea点击:

spring boot: how to configure datasource from application properties

javaspringhibernatespring-boot

提问by Dan

I want the below code values: DriverClassName, Url, Username, Passwordto be read from application.propertiesfile, how to do that? I am using Spring Boot, Mysql, Hibernate and Spring Rest.

我想下面的代码值:DriverClassNameUrlUsernamePassword从读取application.properties文件,该怎么做?我正在使用 Spring Boot、Mysql、Hibernate 和 Spring Rest。

DatasourceConfig.java

数据源配置文件

    //This is working fine with inline value specified as below (DriverClassName, Url,Username,Password
    @Configuration
    @EnableTransactionManagement
    @EnableJpaRepositories(basePackages = "com.nouhoun.springboot.jwt.integration.repository")
    public class DatasourceConfig {

        @Bean
        public DataSource datasource() throws PropertyVetoException {
               final DriverManagerDataSource dataSource = new DriverManagerDataSource();
               dataSource.setDriverClassName("com.mysql.jdbc.Driver");
               dataSource.setUrl("jdbc:mysql://localhost:3306/fdb?createDatabaseIfNotExist=true");
               dataSource.setUsername("root");
               dataSource.setPassword("");
               return dataSource;
    }
   ....
   ....
   ....

采纳答案by kj007

Once you have defined data source properties in application.propertiesin @SpringBootApplicationit will auto configure your datasource, so you can remove DataSource configuration. But still if you want to customize your data source configuration then below should work as Environmentshould give you access of properties:

一旦你定义数据源属性application.properties@SpringBootApplication它会自动配置datasource,这样你就可以删除DataSource configuration。但是如果你想自定义你的数据源配置,那么下面应该Environment可以让你访问属性:

@Configuration
@PropertySource(value= {"classpath:application.properties"})
public class DatasourceConfig {

    @Autowired
    Environment environment;

    @Bean
    public DataSource datasource() throws PropertyVetoException {
        final DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(environment.getProperty("spring.datasource.driver-class-name"));
        dataSource.setUrl(environment.getProperty("spring.datasource.url"));
        dataSource.setUsername(environment.getProperty("spring.datasource.username"));
        dataSource.setPassword(environment.getProperty("spring.datasource.password"));
        return dataSource;
    }
}

Or if you don't want to access properties via Environment, you can access by @Value

或者,如果您不想通过 访问属性Environment,则可以通过访问@Value

  @Value("${spring.datasource.driver-class-name}")
    private String driverName;

    @Value("${spring.datasource.url}")
    private String url;

    @Value("${spring.datasource.username}")
    private String userName;

    @Value("${spring.datasource.password}")
    private String password;

    @Bean
    public DataSource datasource() throws PropertyVetoException {
        final DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(driverName);
        dataSource.setUrl(url);
        dataSource.setUsername(userName);
        dataSource.setPassword(password);
        return dataSource;
    }

回答by Deepak Jain

Seems you forget to add dependency in you pom.xml or build.gradle or your build dont have that dependency if you already added (run mvn clean install)

似乎您忘记在 pom.xml 或 build.gradle 中添加依赖项,或者如果您已经添加,则您的构建没有该依赖项(运行mvn clean install

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>6.0.6</version>
</dependency>

Please add and try again

请添加并重试