3

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:

4
  • You should configure your datasource from "application.properties" Commented May 10, 2016 at 17:14
  • did not get you. Yes my datasource properties are there in application.properties. But I am not able to port them in getDatSorce method() above. Commented May 10, 2016 at 18:46
  • Try extending your MyDataSource class from WebMvcConfigurerAdapter. Plus add EnableWebMvc annotation and add the method @Override public void addResourceHandlers(final ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); registry.setOrder(Ordered.HIGHEST_PRECEDENCE); } Commented May 10, 2016 at 20:25
  • You are using Spring Boot then why are you even trying to do those things yourself? Spring Boot already does that, you are basically trying very hard NOT to use Spring Boot. Commented May 11, 2016 at 10:00

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

3 Comments

This isn't true for Spring Boot where a PropertySourcesPlaceholderConfigurer is auto-configured so ${…} placeholders are resolved by default.
If you mean the PropertySourcesPlaceholderConfigurer is not required, yes, I am getting the values in ${...}. But I am not able to use them inside the bean getDataSource()?
The point I've been making is that you might switch to env.getProperty instead of ${..}.
-1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.