在spring 3.0中,可以通过使用@value,对一些*.properties文件中的文件,进行键值对的注入。
1首先在applicationContext.xml中加入命名空间:
<beans xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd"></beans>
2 在xml文件中生命标签:
<util:properties id="config" location="WEB-INF/config.properties"></util:properties>
id是可以替换的
3 创建config.properties文件:
例如:username=a
4 在类中应用:
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
public class TestController {
@Value("#{config['username']}")
private String username;
@RequestMapping(value = "/test")
public String test(Model model) {
model.addAttribute("username", username);
return "test";
}
}
这样username就被注入了,@Vlaue也可以生命到方法上给参数进行注入。