V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
VKRUSSIA
V2EX  ›  Java

这个配置文件的工具类有没有存在的必要?

  •  
  •   VKRUSSIA · Feb 9, 2022 · 1466 views
    This topic created in 1550 days ago, the information mentioned may be changed or developed.

    `public class PropertiesUtils extends Properties {

    private static final long serialVersionUID = 1L;
    private static final String DEFAULT_PROPERTIES;
    static {
    	DEFAULT_PROPERTIES = "config/application.properties";
    }
    
    @SuppressWarnings("unused")
    private PropertiesUtils() {
    
    }
    
    public PropertiesUtils(@Nullable ClassLoader classLoader, String... configPath) {
    	String file = configPath.length == 0 ? DEFAULT_PROPERTIES : configPath[0];
    	Optional<InputStream> inStream = Optional.of(Optional.ofNullable(classLoader).orElseGet(() -> {
    		ClassLoader[] classLoaders = new ClassLoader[] { Thread.currentThread().getContextClassLoader(),
    				ClassLoader.getSystemClassLoader(), PropertiesUtils.class.getClassLoader() };
    		return Arrays.stream(classLoaders).filter(cl -> cl.getResourceAsStream(file) != null).findFirst().get();
    	}).getResourceAsStream(file));
    	if (inStream.isPresent())
    		try {
    			super.load(inStream.get());
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    
    }
    

    }`

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3461 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 10:41 · PVG 18:41 · LAX 03:41 · JFK 06:41
    ♥ Do have faith in what you're doing.