Spring源码学习-第一篇-在源码中添加自定义Demo代码

引言:
1.为什么要读源码?(读源码的有哪些好处?)
2.为什么读spring源码?(读懂Spring源码的有哪些好处?)
3.读源码会遇到哪些困难和挑战?(读源码的学习方法总结)

学源码前准备

1、JDK1.8版本
2、spring 5.1.3.RELEASE最新版本
3、需补一下Lambda表达式的知识
4、安装配置gradle
Spring源码下载
1)git clone --branch v5.1.3.RELEASE https://gitee.com/Z201/spring-framework.git
2)gradle下载,gradle要JDK8的版本
3)到下载的spring源码路径执行gradle命令,gradlew :spring-oxm:compileTestJava
4)用idea打开spring源码工程,在idea中安装插件kotlin,重启idea
5)把编译好的源码导入到工程中

在源码中添加一段自定义代码,进行运行

1、在spring-context 模块中添加最简单的代码结构

在这里插入图片描述

2.主要的代码

Student – 一个简单的bean对象
MyTest – 调用入口
spring.xml – 主要是配置对Student进行加载注入
对应的代码:

package com.chenyanwu.bean;

import org.springframework.stereotype.Service;

@Service
public class Student {

	private String username = "chenyanwu";

	private String password;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}
package com.chenyanwu.test;

import com.chenyanwu.bean.Student;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {

	@Test
	public void test1() {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
		Student student = (Student)applicationContext.getBean("student");
		System.out.println(student.getUsername());
	}
}
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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/context
    http://www.springframework.org/schema/context/spring-context.xsd
	">
    <context:component-scan base-package="com.chenyanwu"/>
</beans>

其中spring.xml 也可以使用模式来进行注入bean对象。

代码写完后,就可以使用MyTest中的test1进行调用即可。

此篇文章主要是环境搭建和代码导入,以一个简单的事例来运行,因此篇幅不多,如有疑问可以留言!

============================================================================
更多精彩,更多技术请关注:码蚁在线(coding_online)
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值