解决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