Spring Boot:如何从应用程序属性配置数据源

6
我希望以下代码值:DriverClassNameUrlUsernamePasswordapplication.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;
    }
   ....
   ....
   ....

你的堆栈跟踪显示启动Tomcat时出现问题。 你在pom.xml文件中包含了Tomcat依赖吗? - Nicholas K
您可以使用@EnableConfigurationProperties(JpaProperties.class)注释来检查并将属性绑定到变量@Value("${spring.datasource.url}"),并在资源文件夹中的application.yml中进行设置。 - Amit
2个回答

9

一旦您在@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;
    }

0

看起来你忘记在pom.xml或build.gradle中添加依赖项,或者你的构建中没有该依赖项(如果你已经添加了,请运行mvn clean install

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

请添加后重试。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接