spring 获取配置文件properties中参数

spring 获取配置文件properties中参数

 
<iframe id="cproIframe_u728416_1" style="border-width: 0px; margin: 0px; padding: 0px;" src="http://pos.baidu.com/acom?adn=3&amp;at=134&amp;aurl=&amp;cad=1&amp;ccd=24&amp;cec=UTF-8&amp;cfv=16&amp;ch=0&amp;col=zh-CN&amp;conOP=0&amp;cpa=1&amp;dai=1&amp;dis=0&amp;layout_filter=rank%2Ctabcloud&amp;ltr=http%3A%2F%2Fwww.baidu.com%2Fs%3Fie%3Dutf-8%26f%3D8%26rsv_bp%3D1%26tn%3Dbaidu%26wd%3Dspring%2520mvc%2520PropertyPlaceholderConfigurer%26rsv_pq%3D857a1d3400010732%26rsv_t%3D3e3dkpSNLQpYm%252Bzw8TbjldufYNimprz532c5sHfxK7e9mMRZaeb7FK0wiYA%26bs%3Dspring%2520mvc%2520propertisutil&amp;ltu=http%3A%2F%2Fblog.94gleaner.com%2F445.html&amp;lunum=6&amp;n=74015068_cpr&amp;pcs=1366x665&amp;pis=10000x10000&amp;ps=169x878&amp;psr=1366x768&amp;pss=1366x170&amp;qn=2af92e2cbaa81964&amp;rad=&amp;rsi0=336&amp;rsi1=280&amp;rsi5=4&amp;rss0=%23FFFFFF&amp;rss1=%23FFFFFF&amp;rss2=%230000FF&amp;rss3=%23444444&amp;rss4=%23008000&amp;rss5=&amp;rss6=%23e10900&amp;rss7=&amp;scale=&amp;skin=&amp;td_id=728416&amp;tn=text_default_336_280&amp;tpr=1423746810626&amp;ts=1&amp;version=2.0&amp;xuanting=0&amp;dtm=BAIDU_DUP2_SETJSONADSLOT&amp;dc=2&amp;di=u728416&amp;tt=1423746809723.906.2326.2340" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" align="center,center" width="336" height="280"></iframe>

Spring中config属性文件的读取与使用 PropertyPlaceholderConfigurer 注解@Component

1.配置文件:/WEB-INF/configInfo.properties
配置文件内容:
email.host = www.94gleaner.com
email.port = xxx
email.username = gleaner
email.password = xxx
email.sendFrom = [email protected]

2.Spring容器启动时,使用内置bean对属性文件信息进行加载,在bean.xml中添加如下:
<!– spring的属性加载器,加载properties文件中的属性 –>
<bean id=”propertyConfigurer” class=”org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”>

  1. <property name=”location”>
    1. <value>/WEB-INF/configInfo.properties</value>
  2. </property>
  3. <property name=”fileEncoding” value=”utf-8″ />

</bean>

3.其它bean中引用,例:
<property name=”host”>

  1. <value>${email.host}</value>

</property>
<property name=”port”>

  1. <value>${email.port}</value>

</property>

4.JAVA代码中获取方法:创建ConfigInfo.java
@Component(“configInfo”)
public class ConfigInfo {

  1. @Value(“${email.host}”)
  2. private String host;
  3. @Value(“${email.port}”)
  4. private String port;
  5. public String getHost() {
    1. return host;
  6. }
  7. public String getPort() {
    1. return port;
  8. }

}

5.代码中调用处
@Autowired
private ConfigInfo configInfo;
public byte[] Test(HttpServletResponse response) {

  1. string host = configInfo.getHost();

}

猜你喜欢

转载自goahead2010.iteye.com/blog/2185514
今日推荐