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

时间:2018-01-31 13:02:30   收藏:0   阅读:614

 

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

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!