1、properties user.properties
name=zhangshan age=18
2、xml Pen1.xml
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <entry key="color">red</entry> <entry key="length">100M</entry> </properties>
3、code
package com.example.demo; import org.springframework.core.io.UrlResource; import org.springframework.core.io.support.PropertiesLoaderUtils; import org.springframework.util.ResourceUtils; import java.io.IOException; import java.net.URL; import java.util.Map; import java.util.Properties; public class Te { public static void main(String[] args) throws IOException { Properties properties = new Properties(); URL url = ResourceUtils.getURL("classpath:user.properties"); UrlResource resource = new UrlResource(url); PropertiesLoaderUtils.fillProperties(properties, resource); for (Map.Entry<Object, Object> entry : properties.entrySet()) { System.out.println(entry.getKey() + "=" + entry.getValue()); } System.out.println("========================"); URL url1 = ResourceUtils.getURL("classpath:Pen1.xml"); UrlResource resource1 = new UrlResource(url1); PropertiesLoaderUtils.fillProperties(properties, resource1); for (Map.Entry<Object, Object> entry : properties.entrySet()) { System.out.println(entry.getKey() + "=" + entry.getValue()); } } }
标签:xml,java,自定义,配置文件,UrlResource,entry,import,properties From: https://www.cnblogs.com/niun/p/17528971.html