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>

 

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM