【问题标题】:spring boot: how to configure datasource from application propertiesspring boot:如何从应用程序属性配置数据源
【发布时间】:2019-03-18 23:44:50
【问题描述】:

我希望从 application.properties 文件中读取以下代码值:DriverClassNameUrlUsernamePassword,该怎么做?我正在使用 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;
    }
   ....
   ....
   ....

【问题讨论】:

  • 您的堆栈跟踪表明启动 tomcat 存在问题。你是否在 pom.xml 中包含了 tomcat 依赖项?
  • 您可以检查@EnableConfigurationProperties(JpaProperties.class) 注释并将属性绑定到变量@Value("${spring.datasource.url}") private String url;在资源文件夹中使用 application.yml。

标签: java spring hibernate spring-boot


【解决方案1】:

一旦您在@SpringBootApplication 中的application.properties 中定义了数据源属性,它将自动配置您的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;
    }
}

或者如果你不想通过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;
    }

【讨论】:

    【解决方案2】:

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

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

    请添加并重试

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-26
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      • 1970-01-01
      • 2018-09-11
      • 2022-09-25
      • 2015-11-21
      相关资源
      最近更新 更多