Reputation: 67
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:
Upvotes: 3
Views: 14572
Reputation: 115
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
Upvotes: -1
Reputation: 993
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
Upvotes: 5
Reputation: 1906
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:
- 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.
- 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.
- 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.
Upvotes: 1