JAVA中自定义properties文件介绍
Gradle中的使用
1. 使用gradle.properties
buid.gradle 和 gradle.properties可以项目使用,在同一个项目中,build.gradle可以直接获取其同级或者其父级(父级也要有build.gradle)的properties文件。下面是示例(假设它们是同级):
gradle.properties:
csdn = "www.csdn.com"
build.gradle:
println csdn
2.使用其他的 .properties文件
当properties文件名不为 gradle.properties(例如test.properties) 时或者 不在同级或者父级的目录下时,默认是不会自动引入的,这时候可以使用Java的方式进行引入,网上有很多方式,也可以参考上面官网的API。
可参考:https://blog.csdn.net/Senton/article/details/4083127
这里举两个简单的示例:文件默认在同级目录下面,其他目录的话,把文件名改成路径。
第一种
Properties properties = new Properties()
properties.load(new FileInputStream("test.properties"))
println properties.getProperty("csdn")
第二种
def config = new ConfigSlurper().parse(new File("test.properties").toURL())
println config.csdn
第二种方式除了加载 properties文件外,还可以加载 groovy 文件 或者 gradle 文件。
Java读取Properties文件的六种方法
1。使用java.util.Properties类的load()方法
示例:
InputStream in = lnew BufferedInputStream(new FileInputStream(name));
Properties p = new Properties();
p.load(in);
2。使用java.util.ResourceBundle类的getBundle()方法
示例:
ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
3。使用java.util.PropertyResourceBundle类的构造函数
示例:
InputStream in = new BufferedInputStream(new FileInputStream(name));
ResourceBundle rb = new PropertyResourceBundle(in);
4。使用class变量的getResourceAsStream()方法
示例:
InputStream in = JProperties.class.getResourceAsStream(name);
Properties p = new Properties();
p.load(in);
5。使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
示例:
InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);
Properties p = new Properties();
p.load(in);
6。使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
示例:
InputStream in = ClassLoader.getSystemResourceAsStream(name);
Properties p = new Properties();
p.load(in);
补充
Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
示例:
InputStream in = context.getResourceAsStream(path);
Properties p = new Properties();
p.load(in);
spring中使用自定义properties文件
Spring简化了加载资源文件的配置,可以通过去加载,这个元素的写法如下:
<context:property-placeholder location="classpath:jdbc.properties"/>
如果想要配置多个properties文件
<context:property-placeholder location="classpath:jdbc.properties"/> <context:property-placeholder location="classpath:jdbc.properties"/>
这种方式是不被允许的,一定会出"Could not resolve placeholder"。
解决方案:
(1) 在Spring 3.0中,可以写:
<context:property-placeholder location="xxx.properties" ignore-unresolvable="true"/> <context:property-placeholder location="xxx.properties" ignore-unresolvable="true"/>
(2) 但是在Spring 2.5中,没有ignore-unresolvable属性,所以就不能使用上面的那种方法去配置,
可以改如下的格式:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/jdbc.properties</value>
</list>
</property>
</bean>
JAVA中自定义properties文件介绍的更多相关文章
- 五种方式让你在java中读取properties文件内容不再是难题
一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题.就借此机会把Spring+SpringMVC ...
- SpringBoot中自定义properties文件配置参数并带有输入提示
1. 创建配置类 在项目中创建一个参数映射类如下 @ConfigurationProperties(prefix = "user.info") public class MyPro ...
- java加载properties文件的六中基本方式实现
java加载properties文件的方式主要分为两大类:一种是通过import java.util.Properties类中的load(InputStream in)方法加载: 另一种是通过impo ...
- Java项目中读取properties文件,以及六种获取路径的方法
下面1-4的内容是网上收集的相关知识,总结来说,就是如下几个知识点: 最常用读取properties文件的方法 InputStream in = getClass().getResourceAsStr ...
- Java中创建操作文件和文件夹的工具类
Java中创建操作文件和文件夹的工具类 FileUtils.java import java.io.BufferedInputStream; import java.io.BufferedOutput ...
- java加载properties文件的六种方法总结
java加载properties文件的六种方法总结 java加载properties文件的六中基本方式实现 java加载properties文件的方式主要分为两大类:一种是通过import java. ...
- Java中的Properties类
目录 Java中的Properties类 前言 主要方法 读取Properties文件 相关实例 Java中的Properties类 前言 Java中的Properties类属于配置文件,以键值对的方 ...
- spring boot 在框架中注入properties文件里的值(Spring三)
前一篇博客实现了打开第一个页面 链接:https://blog.csdn.net/qq_38175040/article/details/105709758 本篇博客实现在框架中注入propertie ...
- C#和Java中执行SQL文件脚本的代码(非常有用)
原文:C#和Java中执行SQL文件脚本的代码(非常有用) 我们在做程序的时候有事后会涉及到利用sql文件 直接执行,可是在sql文件中有很多注释,我们要一句一句的执行首先必须的得把sql文件解析 去 ...
随机推荐
- 【转】博弈论——acm
转自http://blog.csdn.net/lgdblue/article/details/15809893 序:博弈是信息学和数学试题中常会出现的一种类型,算法灵活多变是其最大特点,而其中有一类试 ...
- python学习之路(25)
继承和多态 在OOP程序设计中,当我们定义一个class的时候,可以从某个现有的class继承,新的class称为子类(Subclass),而被继承的class称为基类.父类或超类(Base clas ...
- CodeChef---- February Challenge 2018----Chef and odd queries(复杂度分块计算)
链接 https://www.codechef.com/FEB18/problems/CHANOQ/ Chef and odd queries Problem Code: CHANOQ Chef ...
- JDK中String类的源码分析(一)
1.String类是final的,不允许被继承 /** The value is used for character storage. */ private final char value[]; ...
- c# SQLite 判断表、字段是否存在的方法,新增、删除、重命名列
SQLiteHelper class: using System; using System.Collections.Generic; using System.Text; using System. ...
- 原生JS去重
方式一: function deleteRepetionChar(arr){ //先判断输入进来的是数组对象还是字符串 if( typeof arr == "object"){ v ...
- Vue知识整理15:组件注册
采用局部注册组件: 将代码放在vue的一个实例中,而不是单列申明.
- 阶段3 1.Mybatis_01.Mybatis课程介绍及环境搭建_07.环境搭建的注意事项
2 resources下面创建目录要一级一级的创建,下面这个创建的就是一级目录而不是三级 在文件夹下看到的目录也是一级的 因此这里创建目录需要一个个的去创建 配置文件和dao类这两个目录要保持一致,这 ...
- IDEA激活—免费永久激活(lookdiv.com)
网址: http://lookdiv.com/ 钥匙就是网址 钥匙:lookdiv.com 亲测有效!非常好用.
- python2与3版本的编码问题
python的str默认是ascii编码,和unicode编码冲突,就会报这个标题错误:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 ...