解決Sping 框架 Controller@Value獲取不到值

緣由:要獲取 int.properties 中的數據 可是 一直拿不到值 以下代碼app

使用這種方式注入 *.properties文件spa

<!-- 引入配置文件 -->
    <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;
    }
}

解決方法:code

@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;
    }
}

指定讀取字段的文件blog

相關文章
相關標籤/搜索