做门户网站赚钱吗,手机必备软件,wordpress 数据库下载,长春网络网站制作开发学习 Spring 的源码#xff0c;也可以通过 SpringBoot 搭环境。
不管是什么源码#xff0c;最好写个 demo#xff0c;跑起来#xff0c;然后从常用的类和方法入手#xff0c;跟踪调试。
配置对象
新建一个 SpringBoot 的项目#xff0c; 详情见#xff1a; https://b…学习 Spring 的源码也可以通过 SpringBoot 搭环境。
不管是什么源码最好写个 demo跑起来然后从常用的类和方法入手跟踪调试。
配置对象
新建一个 SpringBoot 的项目 详情见 https://blog.csdn.net/sinat_32502451/article/details/133039001
接着在 com.example.demo.model 路径建一个类 Person属性包括 name和 age (也可以是其它的路径与以下的class路径一致就行) 。
public class Person {private int age;private String name;public int getAge() {return age;}public void setAge(int age) {this.age age;}public String getName() {return name;}public void setName(String name) {this.name name;}
} 然后在 resources 文件夹下添加一个 mySpring.xml 的文件内容如下
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdbean idperson class com.example.demo.model.Personproperty namename valueTom/property nameage value1//bean/beans查看配置的对象值
创建一个类查看 xml 配置的对象的值
public class MySpring {public static void main(String[] args) {//可以在以下这段代码打个断点跟踪调试ApplicationContext context new ClassPathXmlApplicationContext(classpath*:mySpring.xml);Person person context.getBean(person, Person.class);System.out.println( person.getName());}}在 ApplicationContext 开头的这段代码打个断点 然后 跟踪调试。
ClassPathXmlApplicationContext
从 xml 加载定义的 bean 对象并且会通过 refresh 刷新 context 上下文 。 /*** Create a new ClassPathXmlApplicationContext with the given parent,* loading the definitions from the given XML files.* param configLocations array of resource locations* param refresh whether to automatically refresh the context,* loading all bean definitions and creating all singletons.* Alternatively, call refresh manually after further configuring the context.* param parent the parent context* throws BeansException if context creation failed* see #refresh()*/public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, Nullable ApplicationContext parent)throws BeansException {super(parent);//设置配置文件的路径setConfigLocations(configLocations);if (refresh) {//refresh()方法负责初始化 ApplicationContext 容器。可以重点看看。refresh();}}调试方法
可以通过 Step Into 进入方法也就是 Intellij Idea 的 F7 快捷键继续跟踪。
Step Over 也就是 Intellij Idea 的 F8 快捷键单步调试逐行调试。
遇到调用接口如果不清楚是哪个接口实现类可以直接在 Intellij Idea 的 接口上打断点调试时会自动跳转到对应的接口实现类。
调试过程中遇到不懂的也可以百度搜索下。