Spring源码解析-配置文件的加载
spring是一个很有名的java开源框架,作为一名javaer还是有必要了解spring的设计原理和机制,beans、core、context作为spring的三个核心组件。而三个组件中最重要的就是beans组件了。
从一个简单实例来查看spring加载配置文件中的bean。
public class Student {
private int stId;
private String studentName;
private String studentAge;
public int getStId() {
return stId;
}
public void setStId(int stId) {
this.stId = stId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStudentAge() {
return studentAge;
}
public void setStudentAge(String studentAge) {
this.studentAge = studentAge;
}
}
spring-beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="student" class="org.lzyer.test.Student"></bean>
</beans>
测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({
"classpath:spring/spring-beans.xml"})
public class SpringSourceTest {
@Test
public void testSpring1(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-beans.xml");
Student student = (Student)context.getBean("student");
System.out.println(student);
}
}
spring配置文件加载流程

(点击查看大图)
下面对每一个流程的代码展示

刷新context

获取beanFactory

刷新beanFacory



获取配置文件,并且加载beanDefinition

读取配置文件

解析xml文件

解析beanDefinition



注册beanDefinition

最后将beanDefinition注册到beanDefinitionMap中

Spring源码解析-配置文件的加载的更多相关文章
- 从SpringBoot源码分析 配置文件的加载原理和优先级
本文从SpringBoot源码分析 配置文件的加载原理和配置文件的优先级 跟入源码之前,先提一个问题: SpringBoot 既可以加载指定目录下的配置文件获取配置项,也可以通过启动参数( ...
- Spring源码分析:Bean加载流程概览及配置文件读取
很多朋友可能想看Spring源码,但是不知道应当如何入手去看,这个可以理解:Java开发者通常从事的都是Java Web的工作,对于程序员来说,一个Web项目用到Spring,只是配置一下配置文件而已 ...
- 【Spring源码分析】Bean加载流程概览
代码入口 之前写文章都会啰啰嗦嗦一大堆再开始,进入[Spring源码分析]这个板块就直接切入正题了. 很多朋友可能想看Spring源码,但是不知道应当如何入手去看,这个可以理解:Java开发者通常从事 ...
- 【Spring源码分析】Bean加载流程概览(转)
转载自:https://www.cnblogs.com/xrq730/p/6285358.html 代码入口 之前写文章都会啰啰嗦嗦一大堆再开始,进入[Spring源码分析]这个板块就直接切入正题了. ...
- mybatis源码解析之Configuration加载(四)
概述 上一篇文章,我们主要讲了datasource的相关内容,那么<environments>标签下的内容就看的差不多了,今天就来看一下在拿到transationManager和datas ...
- mybatis源码解析之Configuration加载(五)
概述 前面几篇文章主要看了mybatis配置文件configuation.xml中<setting>,<environments>标签的加载,接下来看一下mapper标签的解析 ...
- mybatis源码解析之Configuration加载(二)
概述 上一篇我们讲了configuation.xml中几个标签的解析,例如<properties>,<typeAlises>,<settings>等,今天我们来介绍 ...
- 五、spring源码阅读之ClassPathXmlApplicationContext加载beanFactory
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml&q ...
- webpack4.X源码解析之懒加载
本文针对Webpack懒加载构建和加载的原理,对构建后的源码进行分析. 一.准备工作 首先,init之后创建一个简单的webpack基本的配置,在src目录下创建两个js文件(一个主入口文件和一个非主 ...
随机推荐
- 基于pygame的打砖块游戏,做到一半,不带做了
跟着一个博主做的,前面的变量的定义全是内个哥们的,没带任何改动,就做了个界面,背景音乐,绘制了个小球,绘制了挡板 小球可以撞到边界反弹,然后做了砖块,定义了一个存放砖块的列表,,,就没有下文了 原博主 ...
- python之三元运算
三元运算(三目运算):用于较简单的判断(if else). if True: return s = "aaaa" else: return s = "bbbb&quo ...
- Linux命令备忘录:mount用于加载文件系统到指定的加载点
mount命令用于加载文件系统到指定的加载点.此命令的最常用于挂载cdrom,使我们可以访问cdrom中的数据,因为你将光盘插入cdrom中,Linux并不会自动挂载,必须使用Linux mount命 ...
- 004---os & sys
os模块和sys模块 这两个模块都提供了很多与操作系统之间交互的功能 使用 import os #当前脚本的工作目录,不是脚本目录 print(os.getcwd()) # 获取指定目录下的所有文件和 ...
- centos 安装java1.8
https://www.cnblogs.com/xuliangxing/p/7066913.html
- 【Java】关于Spring框架的总结 (二)
上文提到了 Spring 的 IoC 特性和 AOP 特性,只提到个别的实现方法.本文将对 IoC 和 AOP 其他方法进行讲解. 多种方式实现依赖注入 1.设值注入 上文中使用的注入方法:通过 se ...
- Android面试收集录 Android系统的资源+其他
1.Android应用程序的资源是如何存储的,如何使用? res文件夹或者assets文件夹 res目录中的资源在R类中生成一个int变量,然后再布局文件中可以直接使用,在代码中,要getResour ...
- php-configure错误解决
configure: error: libjpeg.(a|so) not foundconfigure: error: libjpeg.(a|so) not foundln -sf libjpeg.s ...
- 记一次艰难的CTP调试
一个atmel,mxt540e的CTP触摸屏. 中断配置为下降沿,输入上拉. 总是只能触发一次中断,中断脚就一直低电平,无法拉高.这只是表面现象 不停找底层I2C驱动,改代码,没用.要靠波形来说话 ...
- c++ class as sort function
// constructing sets #include <iostream> #include <set> #include <string.h> bool f ...