天天看点

java配置 spring 的@PropertySource("classpath:jdbc.properties") 出现 @Value("${jdbc.driver}") 取值为null

使用Java配置项目,其中有一个配置数据源时,使用@PropertySource("classpath:jdbc.properties")  读取配置文件,但是在注入属性时,发现 @Value("${jdbc.driver}") 等不能给属性注入值,为null;如下图:

java配置 spring 的@PropertySource("classpath:jdbc.properties") 出现 @Value("${jdbc.driver}") 取值为null

 网上找了很多东西,最后发现Java配置中的一个地方写错:

java配置 spring 的@PropertySource("classpath:jdbc.properties") 出现 @Value("${jdbc.driver}") 取值为null

最后改成如下图的内容就好使:

java配置 spring 的@PropertySource("classpath:jdbc.properties") 出现 @Value("${jdbc.driver}") 取值为null

最后结果如下图:

java配置 spring 的@PropertySource("classpath:jdbc.properties") 出现 @Value("${jdbc.driver}") 取值为null