zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    学习笔记16:残差网络
    学习笔记15:第二种加载数据的方法
    学习笔记14:模型保存
    学习笔记13:微调模型
    学习笔记12:图像数据增强及学习速率衰减
    蚯蚓
    [JSOI] 重要的城市
    正则表达式
    加分二叉树
    选择客栈
  • 原文地址:https://www.cnblogs.com/lemon-flm/p/8391239.html
Copyright © 2011-2022 走看看