spring中注入属性文件中的值

来源:互联网 发布:免费交朋友软件 编辑:程序博客网 时间:2024/05/22 13:20

方法1:
1、属性文件配置

<!-- 获取properties中的值 -->   <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">     <property name="locations">       <list>       <value>classpath:conf.properties</value>       </list>     </property>   </bean>   <!-- Spring的动态变量,能在bean中直接调用 -->    <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">       <property name="properties" ref="configProperties" />   </bean> <!-- 配置扫描包 --> <context:component-scan base-package="com.xxx.hyweb" />

2、使用
properties中SERVICE_URL=http://sxxxx.com

@Componentpublic class ConfUitl { @Value("#{configProperties['SERVICE_URL']}") private String url; public String getUrl(){  return url; } public void setUrl(String url){  this.url = url; }}

方法2:
1、配置

<util:properties id="propertyConfigurer" location="classpath:global.properties"/><context:property-placeholder properties-ref="propertyConfigurer" ignore-unresolvable="true"/>

2、使用
properties中test.key = 542421ae23d4f;

@Servicepublic class Service{    @Value("${test.key}")    private String key;}
原创粉丝点击