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>

 

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-17
猜你喜欢
  • 2021-09-21
  • 2022-03-04
  • 2022-12-23
  • 2021-08-25
  • 2022-12-23
  • 2021-12-28
  • 2021-07-03
相关资源
相似解决方案