解決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;
    }
}

指定讀取字段的文件


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM