之前一直用的是Java代码获取配置文件中的属性值,也是可以实现的,代码如下:
public Integer getProperty(String propertyName) throws IOException{
Properties props = new Properties();
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("others.properties");
props.load(inputStream);
String phoneLimit = props.getProperty(propertyName);
Integer res=Integer.parseInt(phoneLimit);
return res;
}
这种方法也是完全可以用哦~
最近研究了下Springmvc的@Value用法,可以直接通过@Value获取的属性值,简单方便哦,具体步骤如下:
1、在application-context.xml 如下配置:
2、在dispatcher-servlet.xml中添加如下配置(dispatcher-servlet.xml中主要是对MVC的配置,如果想在controller中使用@Value则必须配置,不然你获取到的只是${..}的字符串)
3、配置基本完成,去controller使用吧
package com.loan.fore.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/test")
public class test {
@Value("${isProduct}")
private String isProduct;
@RequestMapping("")
public String indexShow(Model model){
System.out.println(isProduct);
return "";
}
}
版权声明:本文为dreamer_8399原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。