Sping学习笔记(一)----Spring源码阅读环境的搭建,
分享于 点击 8355 次 点评:11
Sping学习笔记(一)----Spring源码阅读环境的搭建,
- idea搭建spring源码阅读环境
- 安装gradle
- Github下载Spring源码
- 新建学习spring源码的项目
idea搭建spring源码阅读环境
安装gradle
- 在官网中下载gradle二进制文件下载地址
- 解压到本地后,配置gradle路径到环境变量中( 不知道怎么配的自行百度gradle配置教程 )
Github下载Spring源码
- spring-framework项目已经迁移至github,我们可以直接从github中获取spring的源码
- 项目地址
- 可以直接下载zip包
- 解压到本地后使用idea打开
- 打开后idea会询问是否开始构建该gradle项目(看配置,也可能自动开始构建gradle项目)
- 构建可能会受网络环境的影响,建议gradle调整至国内源
- idea构建完成并且建立好文件索引后,就可以开始新建一个项目来专门学习和阅读spring源码了
新建学习spring源码的项目
接下来以图片为主
- 填写gradle的相关信息后创建一个新的module
- 接下来可以根据自己的喜好命名,不需要按部就班
- 在项目内创建相关的包名和类名
- SimpleComponent
package top.fresh00air.spring.component; import org.springframework.stereotype.Component; @Componentpublic class SimpleComponent { public void helloWorld(){ // 建议使用logger,需要添加日志组件,推荐使用logback,顺便可以学习一下logback的配置方法 System.out.println("HelloWorld"); } }
- SimpleTestConfig
package top.fresh00air.spring.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import top.fresh00air.spring.component.SimpleComponent; //声明是配置类
@Configuration
public class SimpleTestConfig { //创建一个bean,当然也可以配置包扫描 @Bean public SimpleComponent simpleComponent(){ return new SimpleComponent(); } }
- SimpleTest(主方法)
package top.fresh00air.spring.test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import top.fresh00air.spring.component.SimpleComponent; import top.fresh00air.spring.config.SimpleTestConfig; public class SimpleTest { public static void main(String[] args) { //spring入口 AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(SimpleTestConfig.class); // 测试获取已注册的SimpleComponent这个bean的实例 SimpleComponent simpleComponent = annotationConfigApplicationContext.getBean(SimpleComponent.class); // 测试调用方法 simpleComponent.helloWorld(); } }
-
运行一下main方法,看看是否可以成功在控制台中打印出
helloworld
这样就搭建好了一个基本的Spring阅读环境了!
相关文章
- 暂无相关文章
用户点评