springboot PropertiesLoaderUtils使用


springboot PropertiesLoaderUtils使用

                              

                                      

************************

相关类与接口

                     

PropertiesLoaderUtils

public abstract class PropertiesLoaderUtils {
    private static final String XML_FILE_EXTENSION = ".xml";
    private static final boolean shouldIgnoreXml = SpringProperties.getFlag("spring.xml.ignore");

    public PropertiesLoaderUtils() {
    }

    public static Properties loadProperties(EncodedResource resource) throws IOException {
        Properties props = new Properties();
        fillProperties(props, resource);
        return props;
    }

    public static void fillProperties(Properties props, EncodedResource resource) throws IOException {
        fillProperties(props, resource, ResourcePropertiesPersister.INSTANCE);
    }

    static void fillProperties(Properties props, EncodedResource resource, PropertiesPersister persister) throws IOException {
        InputStream stream = null;
        Reader reader = null;

        try {
            String filename = resource.getResource().getFilename();
            if (filename != null && filename.endsWith(".xml")) {
                if (shouldIgnoreXml) {
                    throw new UnsupportedOperationException("XML support disabled");
                }

                stream = resource.getInputStream();
                persister.loadFromXml(props, stream);
            } else if (resource.requiresReader()) {
                reader = resource.getReader();
                persister.load(props, reader);
            } else {
                stream = resource.getInputStream();
                persister.load(props, stream);
            }
        } finally {
            if (stream != null) {
                stream.close();
            }

            if (reader != null) {
                reader.close();
            }

        }

    }

    public static Properties loadProperties(Resource resource) throws IOException {
        Properties props = new Properties();
        fillProperties(props, resource);
        return props;
    }

    public static void fillProperties(Properties props, Resource resource) throws IOException {
        InputStream is = resource.getInputStream();
        Throwable var3 = null;

        try {
            String filename = resource.getFilename();
            if (filename != null && filename.endsWith(".xml")) {
                if (shouldIgnoreXml) {
                    throw new UnsupportedOperationException("XML support disabled");
                }

                props.loadFromXML(is);
            } else {
                props.load(is);
            }
        } catch (Throwable var12) {
            var3 = var12;
            throw var12;
        } finally {
            if (is != null) {
                if (var3 != null) {
                    try {
                        is.close();
                    } catch (Throwable var11) {
                        var3.addSuppressed(var11);
                    }
                } else {
                    is.close();
                }
            }

        }

    }

    public static Properties loadAllProperties(String resourceName) throws IOException {
        return loadAllProperties(resourceName, (ClassLoader)null);
    }

    public static Properties loadAllProperties(String resourceName, @Nullable ClassLoader classLoader) throws IOException {
        Assert.notNull(resourceName, "Resource name must not be null");
        ClassLoader classLoaderToUse = classLoader;
        if (classLoader == null) {
            classLoaderToUse = ClassUtils.getDefaultClassLoader();
        }

        Enumeration<URL> urls = classLoaderToUse != null ? classLoaderToUse.getResources(resourceName) : ClassLoader.getSystemResources(resourceName);
        Properties props = new Properties();

        while(urls.hasMoreElements()) {
            URL url = (URL)urls.nextElement();
            URLConnection con = url.openConnection();
            ResourceUtils.useCachesIfNecessary(con);
            InputStream is = con.getInputStream();
            Throwable var8 = null;

            try {
                if (resourceName.endsWith(".xml")) {
                    if (shouldIgnoreXml) {
                        throw new UnsupportedOperationException("XML support disabled");
                    }

                    props.loadFromXML(is);
                } else {
                    props.load(is);
                }
            } catch (Throwable var17) {
                var8 = var17;
                throw var17;
            } finally {
                if (is != null) {
                    if (var8 != null) {
                        try {
                            is.close();
                        } catch (Throwable var16) {
                            var8.addSuppressed(var16);
                        }
                    } else {
                        is.close();
                    }
                }

            }
        }

        return props;
    }
}

                      

Resource

public interface Resource extends InputStreamSource {
    boolean exists();

    URL getURL() throws IOException;
    URI getURI() throws IOException;

    File getFile() throws IOException;

    long contentLength() throws IOException;
    long lastModified() throws IOException;

    Resource createRelative(String relativePath) throws IOException;

    @Nullable
    String getFilename();
    String getDescription();

    default boolean isReadable() {
        return this.exists();
    }

    default boolean isOpen() {
        return false;
    }

    default boolean isFile() {
        return false;
    }

    default ReadableByteChannel readableChannel() throws IOException {
        return Channels.newChannel(this.getInputStream());
    }
}

                    

ClassPathResource

public class ClassPathResource extends AbstractFileResolvingResource {
    private final String path;
    @Nullable
    private ClassLoader classLoader;
    @Nullable
    private Class<?> clazz;

    public ClassPathResource(String path) {
        this(path, (ClassLoader)null);
    }

    public ClassPathResource(String path, @Nullable ClassLoader classLoader) {
        Assert.notNull(path, "Path must not be null");
        String pathToUse = StringUtils.cleanPath(path);
        if (pathToUse.startsWith("/")) {
            pathToUse = pathToUse.substring(1);
        }

        this.path = pathToUse;
        this.classLoader = classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader();
    }

    public ClassPathResource(String path, @Nullable Class<?> clazz) {
        Assert.notNull(path, "Path must not be null");
        this.path = StringUtils.cleanPath(path);
        this.clazz = clazz;
    }

                        

FileSystemResource

public class FileSystemResource extends AbstractResource implements WritableResource {
    private final String path;
    @Nullable
    private final File file;
    private final Path filePath;

    public FileSystemResource(String path) {
    public FileSystemResource(File file) {
    public FileSystemResource(Path filePath) {
    public FileSystemResource(FileSystem fileSystem, String path) {

                        

FileURLResource

public class FileUrlResource extends UrlResource implements WritableResource {
    @Nullable
    private volatile File file;

    public FileUrlResource(URL url) {
        super(url);
    }

    public FileUrlResource(String location) throws MalformedURLException {
        super("file", location);
    }

                       

HttpResource

public interface HttpResource extends Resource {
    HttpHeaders getResponseHeaders();
}

                     

EncodedResource

public class EncodedResourceResolver extends AbstractResourceResolver {

***********
内部类:EncodedResource

    static final class EncodedResource extends AbstractResource implements HttpResource {
        private final Resource original;
        private final String coding;
        private final Resource encoded;

        EncodedResource(Resource original, String coding, String extension) throws IOException {
            this.original = original;
            this.coding = coding;
            this.encoded = original.createRelative(original.getFilename() + extension);
        }

                         

                                   

************************

示例

                     

属性文件:application.properties

person.name=gtlx
person.age=20

                      

Test

public class Test {

    public static void main(String[] args) throws Exception{
        FileSystemResource resource=new FileSystemResource("d:"+ File.separator+"application.properties");
        Properties properties= PropertiesLoaderUtils.loadProperties(resource);
        properties.forEach((key,value)->{
            System.out.println(key+" ==> "+value);
        });
    }
}

                               

控制台输出

person.age ==> 20
person.name ==> gtlx

                                          

                                                                    

Guess you like

Origin blog.csdn.net/weixin_43931625/article/details/120468493