解决Sping 框架 Controller@Value获取不到值

原因:要获取 int.properties 中的数据 但是 一直拿不到值 如下代码

使用这种方式注入 *.properties文件

<!-- 引入配置文件 -->
    <context:property-placeholder location="classpath:*.properties"/>
@RestController
public class HomeController {

    @Value("${datasource.username}")
    private String userName;

    @RequestMapping(value = "/")
    public HashMap<String, String> home() {
        HashMap<String, String> hashMap = Maps.newHashMap();
        hashMap.put("db_username",userName);
        return hashMap;
    }
}

解决方法:

@RestController
@PropertySource("classpath:ini.properties")
public class HomeController {

    @Value("${datasource.username}")
    private String userName;

    @RequestMapping(value = "/")
    public HashMap<String, String> home() {
        HashMap<String, String> hashMap = Maps.newHashMap();
        hashMap.put("db_username",userName);
        return hashMap;
    }
}

指定读取字段的文件

原文地址:https://www.cnblogs.com/rufus-hua/p/7206042.html