Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring boot: how to configure datasource from application properties

I want the below code values: DriverClassName, Url, Username, Password to be read from application.properties file, how to do that? I am using Spring Boot, Mysql, Hibernate and 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;
    }
   ....
   ....
   ....
like image 896
Dan Avatar asked Oct 14 '18 10:10

Dan


People also ask

How do you customize and configure a DataSource in a spring boot application?

To configure your own DataSource , define a @Bean of that type in your configuration. Spring Boot reuses your DataSource anywhere one is required, including database initialization. If you need to externalize some settings, you can bind your DataSource to the environment (see “Section 25.8.

How read data from application properties file in spring?

Another way to read application properties in the Spring Boot application is to use the @ConfigurationProperties annotation. To do that, we will need to create a Plain Old Java Object where each class field matches the name of the key in a property file.


1 Answers

Once you have defined data source properties in application.properties in @SpringBootApplication it 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 Environment should give you access of properties:

@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

  @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;
    }
like image 122
kj007 Avatar answered Nov 14 '22 23:11

kj007