在spring 3.0中,可以通过使用@value,对一些如xxx.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
<util:properties id="settings" location="WEB-INF/classes/META-INF/spring/test.properties" />
3 创建test.properties
abc=123
4
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping("/admin/images")
@Controller
public class ImageAdminController {
@Value("#{settings['abc']}")
public void setImageDir(String val) {
this.imageDir = val;
}
}
本文详细介绍了在Spring 3.0中如何通过使用@value注解来注入来自配置文件(如test.properties)的属性值,并提供了具体的代码示例。包括在applicationContext.xml中配置命名空间、使用<util:properties>元素加载属性文件以及在Controller类中通过@Value注解获取并使用这些属性。
6481

被折叠的 条评论
为什么被折叠?



