总结
总体来说,如果你想转行从事程序员的工作,Java开发一定可以作为你的第一选择。但是不管你选择什么编程语言,提升自己的硬件实力才是拿高薪的唯一手段。
如果你以这份学习路线来学习,你会有一个比较系统化的知识网络,也不至于把知识学习得很零散。我个人是完全不建议刚开始就看《Java编程思想》、《Java核心技术》这些书籍,看完你肯定会放弃学习。建议可以看一些视频来学习,当自己能上手再买这些书看又是非常有收获的事了。
Resource[] getResources(String locationPattern) throws IOException;
同时新增了一个协议前缀,该前缀由子类负责实现
String CLASSPATH_ALL_URL_PREFIX = “classpath*:”;
在继承关系表中可以发现ApplicationContext
也间接实现了ResourceLoader
接口
AbstractApplicationContext
继承了DefaultResourceLoader
,并且里面有getResourcePatternResolver
方法,用于生成PathMatchingResourcePatternResolver
这个实例。
这个方法是在 AbstractApplicationContext 的构造函数里用的,也就意味着AbstractApplicationContext 可以调用 ResourcePatternResolver 的 getResources 方法,来根据路径返回多个Resource实例。
综上所述,AbstractApplicationContext 的实现完全可以支持ResourceLoader和ResourcePatternResolver,这也是高级容器为什么支持统一资源加载的原因。委托给了PathMatchingResourcePatternResolver 和 DefaultResourceLoader 来执行。
ResourceLoader 的使用者BeanDefinitionReader
=====================================================================================================
-
读取BeanDefinition
-
BeanDefinitionRegistry
BeanDefinitionReader会利用ResourceLoader或者ResourcePatternResolver将配置信息解析成一个个的BeanDefinition,并最终借助BeanDefinitionRegistry的注册接口,将BeanDefinition给注册到容器里。
BeanDefinitionReader定义了一系列加载BeanDefinition的接口,针对单个配置文件:
int loadBeanDefinitions(String location) throws BeanDefinitionStoreException;
针对多个配置文件:
int loadBeanDefinitions(String… locations) throws BeanDefinitionStoreException;
针对单个Resource资源实例:
int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException;
针对多个Resource资源实例:
int loadBeanDefinitions(Resource… resources) throws BeanDefinitionStoreException;
最终的目的就是将配置文件的配置转换成一个个的BeanDefinition
还有其他方法:
/**
-
获取BeanDefinitionRegistry对象,这个类的主要作用将BeanDefinition注册到BeanDefinition的注册表中
-
@return
*/
BeanDefinitionRegistry getRegistry();
/**
-
Bean的名字生成器,为匿名bean生成一个名字,就是id
-
@return
*/
BeanNameGenerator getBeanNameGenerator();
AbstractBeanDefinitionReader
实现了公共处理逻辑,主要的一个方法是loadBeanDefinitions
,方法内先获取ResourceLoader,再判断获取的实例是哪一个实例,如果是ResourcePatternResolver的话则代表需要加载多个资源,else里面是只加载单个资源,最终都会调用loadBeanDefinitions
方法做进一步的加载。
public int loadBeanDefinitions(String location, @Nullable Set actualResources) throws BeanDefinitionStoreException {
// 获取资源加载器,主要的功能就是根据路径和类加载器获取Resource对象
ResourceLoader resourceLoader = getResourceLoader();
// 判断资源加载器是否为空
if (resourceLoader == null) {
throw new BeanDefinitionStoreException(
“Cannot load bean definitions from location [” + location + “]: no ResourceLoader available”);
}
// ResourcePatternResolver 用于加载多个文件或者能够加载Ant风格路径的文件资源
if (resourceLoader instanceof ResourcePatternResolver) {
// Resource pattern matching available.
try {
Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
int count = loadBeanDefinitions(resources);
if (actualResources != null) {
Collections.addAll(actualResources, resources);
}
if (logger.isTraceEnabled()) {
logger.trace(“Loaded " + count + " bean definitions from location pattern [” + location + “]”);
}
return count;
}
catch (IOException ex) {
throw new BeanDefinitionStoreException(
“Could not resolve bean definition resource pattern [” + location + “]”, ex);
}
}
else {
// Can only load single resources by absolute URL.
// 加载单个文件资源
// 直接使用ResouceLoader加载
Resource resource = resourceLoader.getResource(location);
int count = loadBeanDefinitions(resource);
if (actualResources != null) {
actualResources.add(resource);
}
if (logger.isTraceEnabled()) {
logger.trace(“Loaded " + count + " bean definitions from location [” + location + “]”);
}
return count;
}
}
从xml文件中读取bean definitions的方法
@Override
public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {
return loadBeanDefinitions(new EncodedResource(resource));
}
又主要调用了上面的loadBeanDefinitions的if里面的逻辑
会根据location获取到Resource实例。
loadBeanDefinitions方法会先指定编码以解析xml资源:
new EncodedResource(resource)
之后执行XmlBeanDefinitionReader的loadBeanDefinitions
,这个方法返回一个int类型的数据,表示本次加载并注册到容器里面的bean definition的个数。
为了支持多线程加载,加载是一个耗时过程,方法里面用到了resourcesCurrentlyBeingLoaded
,是一个ThreadLocal类型的本地变量来存储正在加载的资源。(使用ThreadLocal修饰的变量只能当前线程对其修改,key是线程号,value是线程对应的资源)。
public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
Assert.notNull(encodedResource, “EncodedResource must not be null”);
if (logger.isTraceEnabled()) {
logger.trace("Loading XML bean definitions from " + encodedResource);
}
// 从本地线程变量中获取当前的正在加载的资源
Set currentResources = this.resourcesCurrentlyBeingLoaded.get();
// 如果本地线程变量中不存在正在加载的资源,那么将其添加进去
if (currentResources == null) {
currentResources = new HashSet<>(4);
this.resourcesCurrentlyBeingLoaded.set(currentResources);
}
// 如果encodedResource添加进入currentResources失败,表明其中已经存在这个资源,只不过没有加载完成
if (!currentResources.add(encodedResource)) {
throw new BeanDefinitionStoreException(
“Detected cyclic loading of " + encodedResource + " - check your import definitions!”);
}
try {
// 获取文件的输入流
InputStream inputStream = encodedResource.getResource().getInputStream();
try {
// 封装成InputSource,其中指定了输入流和编码格式
InputSource inputSource = new InputSource(inputStream);
// 如果存在编码,那么将其添加进入InputSource中
if (encodedResource.getEncoding() != null) {
inputSource.setEncoding(encodedResource.getEncoding());
}
// 调用同类的方法继续解析
return doLoadBeanDefinitions(inputSource, encodedResource.getResource());
}
finally {
// 关闭输入流
inputStream.close();
}
}
catch (IOException ex) {
throw new BeanDefinitionStoreException(
总结
虽然我个人也经常自嘲,十年之后要去成为外卖专员,但实际上依靠自身的努力,是能够减少三十五岁之后的焦虑的,毕竟好的架构师并不多。
架构师,是我们大部分技术人的职业目标,一名好的架构师来源于机遇(公司)、个人努力(吃得苦、肯钻研)、天分(真的热爱)的三者协作的结果,实践+机遇+努力才能助你成为优秀的架构师。
如果你也想成为一名好的架构师,那或许这份Java成长笔记你需要阅读阅读,希望能够对你的职业发展有所帮助。
构师并不多。
架构师,是我们大部分技术人的职业目标,一名好的架构师来源于机遇(公司)、个人努力(吃得苦、肯钻研)、天分(真的热爱)的三者协作的结果,实践+机遇+努力才能助你成为优秀的架构师。
如果你也想成为一名好的架构师,那或许这份Java成长笔记你需要阅读阅读,希望能够对你的职业发展有所帮助。
[外链图片转存中…(img-H7K0koMN-1715583857487)]