作者:gleaner 发布:2014-02-28 17:38 分类:
工作学习 阅读:12,883 views
评论关闭
<iframe id="cproIframe_u728416_1" style="border-width: 0px; margin: 0px; padding: 0px;" src="http://pos.baidu.com/acom?adn=3&at=134&aurl=&cad=1&ccd=24&cec=UTF-8&cfv=16&ch=0&col=zh-CN&conOP=0&cpa=1&dai=1&dis=0&layout_filter=rank%2Ctabcloud&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&ltu=http%3A%2F%2Fblog.94gleaner.com%2F445.html&lunum=6&n=74015068_cpr&pcs=1366x665&pis=10000x10000&ps=169x878&psr=1366x768&pss=1366x170&qn=2af92e2cbaa81964&rad=&rsi0=336&rsi1=280&rsi5=4&rss0=%23FFFFFF&rss1=%23FFFFFF&rss2=%230000FF&rss3=%23444444&rss4=%23008000&rss5=&rss6=%23e10900&rss7=&scale=&skin=&td_id=728416&tn=text_default_336_280&tpr=1423746810626&ts=1&version=2.0&xuanting=0&dtm=BAIDU_DUP2_SETJSONADSLOT&dc=2&di=u728416&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 = 94gleaner@94gleaner.com
2.Spring容器启动时,使用内置bean对属性文件信息进行加载,在bean.xml中添加如下:
<!– spring的属性加载器,加载properties文件中的属性 –>
<bean id=”propertyConfigurer” class=”org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”>
- <property name=”location”>
- <value>/WEB-INF/configInfo.properties</value>
- </property>
- <property name=”fileEncoding” value=”utf-8″ />
</bean>
3.其它bean中引用,例:
<property name=”host”>
- <value>${email.host}</value>
</property>
<property name=”port”>
- <value>${email.port}</value>
</property>
4.JAVA代码中获取方法:创建ConfigInfo.java
@Component(“configInfo”)
public class ConfigInfo {
- @Value(“${email.host}”)
- private String host;
- @Value(“${email.port}”)
- private String port;
-
- public String getHost() {
- return host;
- }
- public String getPort() {
- return port;
- }
}
5.代码中调用处
@Autowired
private ConfigInfo configInfo;
public byte[] Test(HttpServletResponse response) {
- string host = configInfo.getHost();
}