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>

 

 

posted @ 2018-01-31 11:09  低调人生  阅读(859)  评论(0编辑  收藏  举报