JAVA document describes the custom properties

The use Gradle

1. gradle.properties

buid.gradle and gradle.properties project can use, in the same project, build.gradle can get their sibling or parent (parent should have build.gradle) of properties files directly. The following is an example (assuming that they are similar):

gradle.properties:

csdn = "www.csdn.com"

build.gradle:

println csdn

2. Use a .properties file

When the file name is not gradle.properties properties (for example test.properties) or if not at the same level or parent directory, the default is not automatically introduced, this time you can use the Java approach to the introduction, there are many ways online also can refer to the above official website of the API.

Refer to: https://blog.csdn.net/Senton/article/details/4083127

Here are just two simple examples: the default file under the same directory, other directory, change the file name path.

The first

Properties properties = new Properties()  
properties.load(new FileInputStream("test.properties"))
println properties.getProperty("csdn")

The second

def config = new ConfigSlurper().parse(new File("test.properties").toURL()) println config.csdn 

The second way in addition to load properties file, you can also load the file or gradle groovy file.

Java read the six methods Properties file

1. Use load java.util.Properties class () Method
Example:

 InputStream in = lnew BufferedInputStream(new FileInputStream(name));
Properties p = new Properties();
p.load(in);

 

2. Use getBundle java.util.ResourceBundle class () Method
Example: 

ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());

 

3. Java.util.PropertyResourceBundle using the constructor class
example:

 InputStream in = new BufferedInputStream(new FileInputStream(name));
ResourceBundle rb = new PropertyResourceBundle(in);

 

4. Class variables using the getResourceAsStream () Method
Example:

 InputStream in = JProperties.class.getResourceAsStream(name);
Properties p = new Properties();
p.load(in);

 

5. The use java.lang.ClassLoader getResourceAsStream class.getClassLoader () of the obtained () Method
Example:

 InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);
Properties p = new Properties();
p.load(in);

 

6. Use getSystemResourceAsStream java.lang.ClassLoader class () static method
Example:

 InputStream in = ClassLoader.getSystemResourceAsStream(name);
Properties p = new Properties();
p.load(in);

 

supplement

Servlet can be used in the getResourceAsStream javax.servlet.ServletContext () Method
Example:

InputStream in = context.getResourceAsStream(path);
Properties p = new Properties();
p.load(in);

 

 

spring use a custom properties file

 

Spring simplifies the configuration to load resource files can be loaded to go through, the wording of this element is as follows:

 

<context:property-placeholder location="classpath:jdbc.properties"/>

 

 

If you want to configure multiple properties files

 

<context:property-placeholder location="classpath:jdbc.properties"/>

<context:property-placeholder location="classpath:jdbc.properties"/>

 

This approach is not permitted, it will be a "Could not resolve placeholder".

 

solution:

 

(1) In Spring 3.0, you can write:

 

<context:property-placeholder location="xxx.properties" ignore-unresolvable="true"/>
 
<context:property-placeholder location="xxx.properties" ignore-unresolvable="true"/>

 

(2) However, in Spring 2.5, no ignore-unresolvable property, you can not use that method to the above configuration,

 

The format can be changed as follows:

 

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
    <list>
      <value>classpath:/jdbc.properties</value>
    </list>
  </property>
</bean>

 

 



Guess you like

Origin www.cnblogs.com/Im-Victor/p/10995939.html