spring 通过@Value 获取properties文件中设置了属性 ,与@Value # 和$的区别

 

spring 获取 properties的值方法

在spring.xml中配置

很奇怪的是,在context-param 加载的spring.xml 不能使用 ${xxx}

必须交给DispatcherServlet 管理的 springMVC.xml才能用?

要交给springMVC的DispatcherServlet去扫描,而不是spring的监听器ContextLoaderListener去扫描,就可以比较方便的使用“${xxx}”去注入。

1、使用 $ 获取属性

@Value("${user.name}")

private String userName;

<!--方法1-->
 <context:property-placeholder location="classpath*:info/info.properties" />
<!--方法2-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="order" value="1" />
    <property name="locations">
        <list>
            <value>classpath:info/info.properties</value>
        </list>
    </property>
</bean>

 

2、使用 #获取属性

@Value("#{user.name}")

private String userName;

<bean id="prop" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 
        <property name="locations"> 
            <array> 
                <value>classpath:configure.properties</value> 
            </array> 
        </property> 
</bean>

 

 

转载于:https://www.cnblogs.com/lemon-flm/p/8391239.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值