Java代码中获取配置文件(config.properties)中内容的两种方法
方法千千万,本人暂时只总结了两种方法。
(1)config.properties中的内容如图
在applicationContext.xml中配置
<!-- 引入配置文件 -->
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:config.properties</value>
</list>
</property>
</bean>
在Controller层中使用springMVC的@Value注解获得配置文件的内容:
@Value("#{configProperties['local']}")
private String localAddress;
在代码中写localAddress即可得到配置文件中local的值。
(2)在applicationContext.xml中配置
<!-- 引入配置文件 -->
<bean id="configPropertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="2" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>classpath:config.properties</value>
</list>
</property>
</bean> <!--配置全局变量-->
<bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="singleton" value="true"/>
<property name="properties">
<props>
<prop key="csdn.url">${csdn.url}</prop>
</props>
</property>
</bean>
在需要用到的类中注入Properties,注意参数必须和配置全局变量的id相同
@Autowired
protected Properties properties;
然后在类中可以通过properties.getProperty("csdn.url")获取配置文件中csdn.url的内容
当然,两种获取参数的方法也可以同时使用,需要在applicationContext.xml中配置
<!-- 引入配置文件 -->
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:config.properties</value>
</list>
</property>
</bean> <bean id="configPropertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="order" value="2" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="properties" ref="configProperties"/>
</bean> <!--配置全局变量-->
<bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="singleton" value="true"/>
<property name="properties">
<props>
<prop key="csdn.url">${csdn.url}</prop>
</props>
</property>
</bean>
Java代码中获取配置文件(config.properties)中内容的两种方法的更多相关文章
- 【转】Linux 中清空或删除大文件内容的五种方法(truncate 命令清空文件)
原文: http://www.jb51.net/article/100462.htm truncate -s 0 access.log -------------------------------- ...
- javascript获取json对象的key名称的两种方法
javascript获取json对象的key名称的两种方法 数据处理中,你可能接收到一个不确定内容格式的json对象,然后要把key的值提取出来.今天试过两种可以提取json key的方法,均可以正常 ...
- Java学习笔记——可视化Swing中JTable控件绑定SQL数据源的两种方法
在 MyEclipse 的可视化 Swing 中,有 JTable 控件. JTable 用来显示和编辑常规二维单元表. 那么,如何将 数据库SQL中的数据绑定至JTable中呢? 在这里,提供两种方 ...
- PyQt(Python+Qt)学习随笔:QTableWidget中表格各列平均分配宽度的两种方法
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 在QTableWidget表格部件中,如果需要实现表格中各列要占满部件中的可用空间,同时实现各列平分 ...
- 怎么在CAD中测量图纸距离?来看看这两种方法
在CAD中设计图纸最重要的就是图纸的尺寸,俗话说也就是图纸间的距离.通过正确的数据设计出的图纸才能够准确,也能够避免施工时事不必要的误差.那怎么在CAD中测量图纸距离呢?具体要怎么来进行操作呢?下面我 ...
- 代码操作Word时,目录自动更新的两种方法
最近的项目中有一个功能点为:根据分析数据库并生成报告.不过不是大数据.数据挖掘之类,报告的内容.组织方式都是事先固定下来的.实现的方式为,在普通word文档中插入书签制成模板,然后程序使用OpenXM ...
- [译]在Linux中清空或删除大文件内容的5种方法
原文来源: https://www.tecmint.com/empty-delete-file-content-linux/ 有时,在处理Linux终端中的文件时,您可能希望清除文件的内容,而无需使用 ...
- Spring Boot中注入配置文件application.properties中的list 对象参数
例如要注入下列参数: dyn.spring.datasources[0].name=branchtadyn.spring.datasources[0].driverClassName=oracle.j ...
- Struts2 在Action中获取request、session、servletContext的三种方法
首页message.jsp: <body> ${requestScope.req }<br/> ${applicationScope.app }<br/> ${se ...
随机推荐
- 关于 IntelliJ IDEA 的Maven 版本修改
Project Structure->Project里Project sdk以及project language level Project Structure->Modules里Sour ...
- Android程序员不容错过的10款在线实用工具
Android十款在线工具,在做Android开发过程中,会遇到一些小的问题,虽然自己动手也能解决,但是有了一些小工具,解决这些问题就得心应手了.Android在线工具,包括在线测试工具,及其他较为重 ...
- Verilog频率计设计
这是以前的一个可编程逻辑课上机实验三 实验报告 数字频率计的基本设计思路是在给定一个time开始测量的时候产生的T的个数,也就是采用一个标准的基准时钟,在单位时间(1秒)里对被测信号的脉冲数进行计数. ...
- js 流程控制语句
1.复合语句 2.switch语句 3.do...while语句 4.while语句 5.for语句 6.for...in语句 7.break和continue语句 9.with语句 10.if语句 ...
- 电路设计软件 电路模拟软件 sPlan , LTspice 等
电路设计/PCB绘制 立创EDA https://lceda.cn/ sPlan http://www.electronic-software-shop.com/splan-70.html?langu ...
- BZOJ 2735: 世博会 主席树+切比雪夫距离转曼哈顿距离
2735: 世博会 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 124 Solved: 51[Submit][Status][Discuss] D ...
- java多线程安全
class Ticket implements Runnable { public int sum=10; public void run() { while(true) { if(sum>0) ...
- 使用Excel调用ABAP系统的函数
效果:在excel里创建一个按钮,开发一些VB script,可以连接指定的ABAP系统并执行系统里的ABAP function module. 在这里例子里执行ABAP系统的函数TH_USER_LI ...
- EF写distinct
在日常开发中常常是这么写的 var logErrorRequest = from l in _logErrorRepository.Table select new { WrongTime = l.W ...
- APP专项测试使用到的工具
最近在读<大话APP测试>,我也就是把需要使用的测试点做一个总结,目前是使用的工具进行的整理,后期慢慢把工具使用案例贴出来