`

spring 3中新增的@value注解

阅读更多
在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 {  
 
    private String imageDir;  
           @Value("#{settings['test.abc']}")  
    public void setImageDir(String val) {  
        this.imageDir = val;  
    }  
 


这样就将test.abc的值注入了imageDir中了
11
10
分享到:
评论
10 楼 leo_soul 2015-11-24  
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="fileEncoding" value="utf-8"/>
        <property name="locations">
            <list>
                <value>/WEB-INF/config/*.properties</value>
            </list>
        </property>
    </bean>

这样可以在类里用@Value(“${abc}”)
xml里用<property name="jdbcUrl" value="\${jdbc.url}"/>
9 楼 alwtgicf 2015-04-30  
配置文件应该是test.abc=123
8 楼 刘小瘦 2014-09-11  
你好 麻烦给一下代码文档可以么 试了下 取不出来值
7 楼 zy116494718 2013-12-12  
5楼是好人,楼主差评,自己要先测试好了再放上去啊!!!!
6 楼 wnn 2013-10-10  
这样用注解的话,properties文件中有几个值就要定义几个成员变量,请问怎样可以一次把properties文件中的值全部取出来??
5 楼 wenwushuangquandexia 2013-08-16  
不是该这样吗   @Value("#{settings['abc']}")  
4 楼 青春的、脚步 2013-04-10  
@Value("#{settings['test.abc']}")    带#什么意思
3 楼 hellostory 2011-12-31  
能否排版下,我觉得排版是一个基本技能!!!!
2 楼 masuweng 2011-12-31  
不好意思  看错了 annotation.Value;
1 楼 masuweng 2011-12-31  
jar包是那个 @value

相关推荐

Global site tag (gtag.js) - Google Analytics