use @ Configuration to load the configuration file and cannot get the value defined in properties
@Configuration
@PropertySource(value = "classpath:jdbc.properties", encoding = "UTF-8")
@ConfigurationProperties(prefix = "jdbc")
public class DataSourceConfig {
private String url;
private String driverClassName;
private String username;
private String password;
// getter setter
@Bean
public DataSource dataSource() {
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName(driverClassName);
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
return ds;
}
}
< hr >
try to take the value directly using the injection Environment, or null;
is written directly in the global application.properties (yml), and then fetched, or null
is currently solved by directly setting the custom properties property to the returned object using @ ConfigurationProperties (prefix= "mysql") on the method. The
test can be injected correctly when used in this way.
that is, the file name and path are correct.