欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Sping学习笔记(一)----Spring源码阅读环境的搭建,

来源: javaer 分享于  点击 8355 次 点评:11

Sping学习笔记(一)----Spring源码阅读环境的搭建,


  • idea搭建spring源码阅读环境
    • 安装gradle
    • Github下载Spring源码
    • 新建学习spring源码的项目

idea搭建spring源码阅读环境

安装gradle

  • 在官网中下载gradle二进制文件下载地址
    1584fe7d527794500c8a21cd86e6c082.png
  • 解压到本地后,配置gradle路径到环境变量中( 不知道怎么配的自行百度gradle配置教程 )

Github下载Spring源码

  • spring-framework项目已经迁移至github,我们可以直接从github中获取spring的源码
  • 项目地址
  • 可以直接下载zip包
    0e6d1a2aac1738f7c5549b53af873493.png
  • 解压到本地后使用idea打开
    258feaa8ddb3c8b2fce95766292f2d58.png
  • 打开后idea会询问是否开始构建该gradle项目(看配置,也可能自动开始构建gradle项目)
  • 构建可能会受网络环境的影响,建议gradle调整至国内源
  • idea构建完成并且建立好文件索引后,就可以开始新建一个项目来专门学习和阅读spring源码了

新建学习spring源码的项目

接下来以图片为主

  • 219602485970d393055cd4f13418b2dc.png
  • 5f2e32fe1d116c66eda8842f354144bf.png
  • 填写gradle的相关信息后创建一个新的module
    4fb87a84191a90df1c7408369f40b9d4.png
  • 接下来可以根据自己的喜好命名,不需要按部就班
  • 在项目内创建相关的包名和类名
    7193d7cac3a69292deec2d5a550780bf.png
  • 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方法,看看是否可以成功在控制台中打印出helloworld9beb9677d3128880889c20e4e5ecd7c0.png

    

这样就搭建好了一个基本的Spring阅读环境了!

相关文章

    暂无相关文章
相关栏目:

用户点评