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文件介绍的更多相关文章

  1. 五种方式让你在java中读取properties文件内容不再是难题

    一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题.就借此机会把Spring+SpringMVC ...

  2. SpringBoot中自定义properties文件配置参数并带有输入提示

    1. 创建配置类 在项目中创建一个参数映射类如下 @ConfigurationProperties(prefix = "user.info") public class MyPro ...

  3. java加载properties文件的六中基本方式实现

    java加载properties文件的方式主要分为两大类:一种是通过import java.util.Properties类中的load(InputStream in)方法加载: 另一种是通过impo ...

  4. Java项目中读取properties文件,以及六种获取路径的方法

    下面1-4的内容是网上收集的相关知识,总结来说,就是如下几个知识点: 最常用读取properties文件的方法 InputStream in = getClass().getResourceAsStr ...

  5. Java中创建操作文件和文件夹的工具类

    Java中创建操作文件和文件夹的工具类 FileUtils.java import java.io.BufferedInputStream; import java.io.BufferedOutput ...

  6. java加载properties文件的六种方法总结

    java加载properties文件的六种方法总结 java加载properties文件的六中基本方式实现 java加载properties文件的方式主要分为两大类:一种是通过import java. ...

  7. Java中的Properties类

    目录 Java中的Properties类 前言 主要方法 读取Properties文件 相关实例 Java中的Properties类 前言 Java中的Properties类属于配置文件,以键值对的方 ...

  8. spring boot 在框架中注入properties文件里的值(Spring三)

    前一篇博客实现了打开第一个页面 链接:https://blog.csdn.net/qq_38175040/article/details/105709758 本篇博客实现在框架中注入propertie ...

  9. C#和Java中执行SQL文件脚本的代码(非常有用)

    原文:C#和Java中执行SQL文件脚本的代码(非常有用) 我们在做程序的时候有事后会涉及到利用sql文件 直接执行,可是在sql文件中有很多注释,我们要一句一句的执行首先必须的得把sql文件解析 去 ...

随机推荐

  1. 苹果cms v10官网下载

    苹果CMS程序是一套采用PHP+MYSQL环境下运行的完善而强大的快速建站系统.经过近多年的开发经验和技术积累,苹果CMS程序已逐步走向成熟,在易用性和功能上已经成为同行中的佼佼者.程序体积小-> ...

  2. codeforces#403—B题(二分,三分)

    B. The Meeting Place Cannot Be Changed time limit per test 5 seconds memory limit per test 256 megab ...

  3. linux系统安装Oracle11g详细步骤

    快速安装指引 ■Reviewing Information About This Guide■Logging In to the System as root 以root用户登录系统■Checking ...

  4. R中rep函数的使用

    官方帮助文档如下写的: Usage rep(x, ...) rep.int(x, times) rep_len(x, length.out) Arguments x a vector (of any ...

  5. 好题Islands

    Orz yjc 吊打候选队 不好的思路是枚举森林的m块,这样DP显然会涉及n当做某一维,最多只能卷积优化一下 生成函数什么的n太大不用想 考虑m,k比较小,能不能把n变成一个系数,而不是维度 所以就是 ...

  6. 大数据笔记(二十五)——Scala函数式编程

    ===================== Scala函数式编程 ======================== 一.Scala中的函数 (*) 函数是Scala中的头等公民,就和数字一样,可以在变 ...

  7. spring cloud:gateway-eureka-filter

    Spring Cloud Gateway 的 Filter 的生命周期不像 Zuul 的那么丰富,它只有两个:“pre” 和 “post”. PRE: 这种过滤器在请求被路由之前调用.我们可利用这种过 ...

  8. java中异常以及处理异常

    一.异常简介 什么是异常? 异常就是有异于常态,和正常情况不一样,有错误出错.在java中,阻止当前方法或作用域的情况,称之为异常. java中异常的体系是怎么样的呢? 1.Java中的所有不正常类都 ...

  9. node、npm、git版本升级

    node版本升级: npm install -g n 或者 npm i -g n --force n stable或者n --stable:安装最近稳定版本 n latest或者n --latest: ...

  10. Make jQuery throw error when it doesn't match an element

    Make jQuery throw error when it doesn't match an element 解答1 You could make a plugin to use to ensur ...