本人环境,java17,spring6.2.1
在尝试将spring xml方式全部换为注解和java类的方式的时候发现@PropertySource+@Value方式不能够正常读取注入resources下的properties文件内容
后续研究后解决
往ioc中加入bean PropertySourcesPlaceholderConfigurer ,并配置location,最后在需要使用properties中属性的bean方法形参,比如我这里的druidDataSource里直接使用@Value("${}")
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
ClassPathResource classPathResource = new ClassPathResource("jdbc.properties");
configurer.setLocation(classPathResource);
return configurer;
}
@Bean("druidDataSource")
public DruidDataSource druidDataSource(@Value("${jdbc.driverName}")String driverClassName,
@Value("${jdbc.url}")String url,
@Value("${jdbc.username}")String username,
@Value("${jdbc.password}")String password,
@Value("${jdbc.minIdle}")int minIdle,
@Value("${jdbc.maxActive}")int maxActive){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(driverClassName);
druidDataSource.setUrl(url);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
druidDataSource.setMinIdle(Integer.valueOf(minIdle));
druidDataSource.setMaxActive(Integer.valueOf(maxActive));
return druidDataSource;
}
标签:PropertySource,jdbc,String,spring,Value,PropertySourcesPlaceholderConfigurer,dru From: https://www.cnblogs.com/idontcare/p/18673007