【java】由读取properties文件联想到的......

本文探讨了Java中通过ClassLoader和Class进行资源加载的过程,详细解释了getResourceAsStream()方法的实现原理,并提到了与JDBC及设计模式的相关联想。

下午闲来无事,看了一个教程,关于读取properties的方法,其实,这种代码网上一大片一大片的,都懒的看了。说说我联想到了什么吧。

 

 首先是getResourceAsStream()方法,就是从stream中获取resource而已,那么这个方法是谁提供的呢?

 

角色之一:ClassLoader

 

 public InputStream getResourceAsStream(String name) {
 URL url = getResource(name);
 try {
     return url != null ? url.openStream() : null;
 } catch (IOException e) {
     return null;
 }
    }
这里都是要找老子帮忙的,他爸是李刚吧,nonono,是Bootstrap刚。。。
public URL getResource(String name) { 
 URL url;
 if (parent != null) {
     url = parent.getResource(name);
 } else {
     url = getBootstrapResource(name);
 }
 if (url == null) {
     url = findResource(name);
 }
 return url;
    }
其中url.openStream()打开Stream,具体如下:
public final InputStream openStream() throws java.io.IOException {
 return openConnection().getInputStream();
}

 

好吧,我承认一看到openConnection就想到JDBC了。其实吧,资源啥的我也不太了解。看看getSystemResource

public static URL getSystemResource(String name) {
	ClassLoader system = getSystemClassLoader();
	if (system == null) {
	    return getBootstrapResource(name);
	}
	return system.getResource(name);
    }

 

就是首先获取SystemClassLoader,如果失败,则去找BootstrapResource  谁让BootstrapLoader是老大呢。

底层是调用了这个方法获取资源的。

static URLClassPath getBootstrapClassPath() {
        return sun.misc.Launcher.getBootstrapClassPath();
    }

 bak1:此处就引发了ClassLoader的加载机制,放在下篇学习。

 

角色之二:Class

中午吃饭的时候还和同事讨论ClassLoader的一些东西,暂时先看下Class吧

public InputStream getResourceAsStream(String name) {
        name = resolveName(name);
        ClassLoader cl = getClassLoader0();
        if (cl==null) {
            // A system class. 有的class是没有loader的,so this nei~~
            return ClassLoader.getSystemResourceAsStream(name);
        }
        return cl.getResourceAsStream(name);
    }

 resolved完成path后,依然是由ClassLoader去处理,此处莫非是代理模式,我日,设计模式离我这么遥远吗??

bak2:此处就引发了设计模式,放在下篇学习

 

总结:其实我想说的是,萝卜青菜,各有所爱,不是不爱,是吃饱了

 

 

Microsoft Windows [版本 10.0.19045.3448] (c) Microsoft Corporation。保留所有权利。 D:\nacos-server-2.0.3\nacos\bin>startup.cmd -m standalone "nacos is starting with standalone" ,--. ,--.'| ,--,: : | Nacos 2.0.3 ,`--.'`| ' : ,---. Running in stand alone mode, All function modules | : : | | ' ,'\ .--.--. Port: 8848 : | \ | : ,--.--. ,---. / / | / / ' Pid: 5576 | : ' '; | / \ / \. ; ,. :| : /`./ Console: http://10.30.52.56:8848/nacos/index.html ' ' ;. ;.--. .-. | / / '' | |: :| : ;_ | | | \ | \__\/: . .. ' / ' | .; : \ \ `. https://nacos.io ' : | ; .' ," .--.; |' ; :__| : | `----. \ | | '`--' / / ,. |' | '.'|\ \ / / /`--' / ' : | ; : .' \ : : `----' '--'. / ; |.' | , .-./\ \ / `--'---' '---' `--`---' `----' 2025-09-11 16:01:22,062 INFO Bean 'org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler@6594402a' of type [org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2025-09-11 16:01:22,068 INFO Bean 'methodSecurityMetadataSource' of type [org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2025-09-11 16:01:22,405 WARN Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is java.lang.IllegalStateException: Unable to create the directory [D:\nacos-server-2.0.3\nacos\bin\file:.] to use as the base directory 2025-09-11 16:01:22,408 INFO Nacos Log files: D:\nacos-server-2.0.3\nacos\logs 2025-09-11 16:01:22,409 INFO Nacos Log files: D:\nacos-server-2.0.3\nacos\conf 2025-09-11 16:01:22,409 INFO Nacos Log files: D:\nacos-server-2.0.3\nacos\data 2025-09-11 16:01:22,411 ERROR Startup errors : org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is java.lang.IllegalStateException: Unable to create the directory [D:\nacos-server-2.0.3\nacos\bin\file:.] to use as the base directory at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:156) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:744) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:391) at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1204) at com.alibaba.nacos.Nacos.main(Nacos.java:35) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) at org.springframework.boot.loader.Launcher.launch(Launcher.java:108) at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) at org.springframework.boot.loader.PropertiesLauncher.main(PropertiesLauncher.java:467) Caused by: java.lang.IllegalStateException: Unable to create the directory [D:\nacos-server-2.0.3\nacos\bin\file:.] to use as the base directory at org.apache.catalina.startup.Tomcat.initBaseDir(Tomcat.java:873) at org.apache.catalina.startup.Tomcat.getServer(Tomcat.java:657) at org.apache.catalina.startup.Tomcat.getService(Tomcat.java:589) at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:171) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:180) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:153) ... 16 common frames omitted 2025-09-11 16:01:22,412 WARN [WatchFileCenter] start close 2025-09-11 16:01:22,413 WARN [WatchFileCenter] start to shutdown this watcher which is watch : D:\nacos-server-2.0.3\nacos\conf 2025-09-11 16:01:22,413 WARN [WatchFileCenter] already closed 2025-09-11 16:01:22,413 WARN [NotifyCenter] Start destroying Publisher 2025-09-11 16:01:22,414 WARN [NotifyCenter] Destruction of the end 2025-09-11 16:01:22,414 ERROR Nacos failed to start, please see D:\nacos-server-2.0.3\nacos\logs\nacos.log for more details. 2025-09-11 16:01:22,423 INFO Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2025-09-11 16:01:22,427 ERROR Application run failed org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is java.lang.IllegalStateException: Unable to create the directory [D:\nacos-server-2.0.3\nacos\bin\file:.] to use as the base directory at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:156) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:744) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:391) at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1204) at com.alibaba.nacos.Nacos.main(Nacos.java:35) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) at org.springframework.boot.loader.Launcher.launch(Launcher.java:108) at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) at org.springframework.boot.loader.PropertiesLauncher.main(PropertiesLauncher.java:467) Caused by: java.lang.IllegalStateException: Unable to create the directory [D:\nacos-server-2.0.3\nacos\bin\file:.] to use as the base directory at org.apache.catalina.startup.Tomcat.initBaseDir(Tomcat.java:873) at org.apache.catalina.startup.Tomcat.getServer(Tomcat.java:657) at org.apache.catalina.startup.Tomcat.getService(Tomcat.java:589) at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:171) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:180) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:153) ... 16 common frames omitted 分析报错信息
最新发布
09-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值