SpringMVC通过@Value注解读取.properties配置
在bean(可以是controller、service、dao等)中,使用@Value注解:
public class TestService{
@Value("${sytem.username}")
String loginUserName;
}
在spring中定义资源文件:
<!-- 导入属性配置文件 -->
<context:property-placeholder location="classpath:properties/*.properties" ignore-unresolvable="true" />
config.properties文件:
sytem.username=admin
这样config.properties中的sytem.username就注入到loginUserName属性中了。
原文链接:http://www.itdadao.com/articles/c15a352661p0.html