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 ...
随机推荐
- php的yii框架开发总结6
MVC中的Controller部分,所有的controller类都是继承自Controller基类,基类里面包含actionAdmin-管理员,actionIndex-一般默认显示,actionVie ...
- JavaScript 闭包的详细分享(三种创建方式)(附小实例)
JavaScript闭包的详细理解 一.原理:闭包函数--指有权访问私有函数里面的变量和对象还有方法等:通俗的讲就是突破私有函数的作用域,让函数外面能够使用函数里面的变量及方法. 1.第一种创建方式 ...
- 使用g++ 编译C++程序
在命令行下,编译C++程序 g++ main.cpp -o main.exe
- April 29 2017 Week 17 Saturday
Every man is a poet when he is in love. 每个恋爱中的人都是诗人. It is said this saying was from Plato, the famo ...
- Fiddler-3 Fiddler抓包-手机端配置
电脑端可以通过Fiddler监听手机端的http请求.需要两个步骤:首先配置Fiddler,再配置手机端. 1 配置 Fiddler 允许远程设备连接: 菜单Tools - Telerik Fiddl ...
- sublime打开txt文件乱码的问题
我们使用Sublime打开TXT文件的时候,会经常因为编码的问题造成乱码. 这是因为TXT记事本的默认保存编码格式是GBK,而Sublime text不支持GB2312和GBK编码. 我们可以通过安装 ...
- 二叉搜索树实现MAP
二叉搜索树的基本实现. /* Date: 2014-04-29 purpose: An implementation of MAP using binary search tree. */ #ifnd ...
- POJ-2503 Babelfish---map或者hash
题目链接: https://vjudge.net/problem/POJ-2503 题目大意: 就像查找一本字典,根据输入的条目和要查询的单词,给出查询结果(每个单词长度不超过10) 解题思路: ma ...
- DP找最优配置,(POJ1018)
题目链接:http://poj.org/problem?id=1018 这个DP,我的头都快晕了. dp[i][j]表示取到第i个设备,宽带为j时的最小价格. 状态转移方程: dp[i][k]=min ...
- JS学习笔记--变量类型
1.js数据类型分为基本数据类型和引用数据类型 基本数据类型:string.number.boolean.null.undefined.symbol(ES6中新增) 引用数据类型:object.arr ...