简体   繁体   中英

Spring Boot not reading from property file

I have tried every option on web but not able to set the values in following method:

@Configuration
@PropertySource("classpath:application.properties")
public class MyDataSource {
    @Value("${db.driver}") 
    private String DB_DRIVER;

    @Value("${db.url}")
    private String DB_URL;

    @Value("${db.username}")
    private String DB_USERNAME;

    @Value("${db.password}")
    private String DB_PASSWORD;


    @Bean
    public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    public DataSource getDataSource() {
         DriverManagerDataSource dataSource = new DriverManagerDataSource();
         dataSource.setDriverClassName(DB_DRIVER);
         dataSource.setUrl(DB_URL);
         dataSource.setUsername(DB_USERNAME);
         dataSource.setPassword(DB_PASSWORD);

         return dataSource;
     }
}

My application.properties is in main/resources folder and values can be seen in variables in debug mode. But on running app, it shows Property ' ' must not be empty.

EDIT: I am not sure what can be the issue in first case? So changed the application.property file as suggested and code as below :

@Autowired
protected JdbcTemplate jdbcTemp;

public List<> getData(String id) {

    return jdbcTemp.query("SELECT ........,new RowMapper());
}

But getting java.lang.NullPointerException:

If you're using Spring Boot, you can leverage application.properties file by declaring some entries:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass

In this way there is no need to implement a @Configuration class to setup database connection in Spring Boot.

You can deepen more here: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html

By the way, take a look at spring.io

For the java configuration, using Environment instance to obtain the properties seems to be the preferred way, as by default ${..} placeholders are not resolved.

You may use something like this:

@Autowired
private Environment env;

@Bean
public DataSource getDataSource() {
     DriverManagerDataSource dataSource = new DriverManagerDataSource();
     dataSource.setDriverClassName(env.getProperty("db.driver");

     .....

     return dataSource;
 }

Reasons from the Spring Jira :

  1. it's inconsistent. @PropertySource is the declarative counterpart to ConfigurableEnvironment#addPropertySource. We do not add a PropertySourcesPlaceholderConfigurer in the latter case, and it would be inconsistent to do so in the former. it will not be what the user intended in every (or even most) cases.
  2. It is entirely possible, and even recommended that @Configuration class users forego $ {...} property replacement entirely, in favor of Environment#getProperty lookups within @Bean methods. For users following this recommendation, the automatic registration of a PropertySorucesPlaceholderConfigurer would be confusing when noticed, and generally undesirable as it's one more moving part. Yes, it's presence is benign, but not cost-free. a PSPC must visit every bean definition in the container to interrogate PropertyValues, only to do nothing in cases where users are going with the Environment#getProperty approach.
  3. it is solvable (and already solved) by documentation. Proper use of @PropertySource, PropertySourcesPlaceholderConfigurer and other components is pretty comprehensively documented in the Javadoc for @Configuration already, and reference documentation is soon to follow.

Me too was getting the error when tried to switch from MySQL to MSSQL. The actual issue was I forgot to put the MSSQL dependency in the service. I used mssql-jdbc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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