❝
大家好呀,我是小羊,如果大家喜欢我的文章的话😁,就关注我一起学习进步吧~
❞
最近在捣鼓spring源码,然后也和大家分享一下吧。
源码下载
github: https://github.com/spring-projects/spring-framework
git clone https://github.com/spring-projects/spring-framework.git
没有梯子的同学可以试试 gitree https://gitee.com/xiao-long-wang/spring-framework
下载完成后可以选择自己想要使用的分支,我这边使用的是 5.3.x 分支。
搭建grade 环境
spring 官方用的 是 grade 不是maven,所以需要配置一下 grade 环境
grade 下载 https://services.gradle.org/distributions/
由于是使用的 5.3.x 版本的 grade,所以我这边使用的是 7.4 版本的

配置 idea gradle 路径。
idea 导入项目
测试
我们这边简单使用spring创建一个 person 并使用spring上下文把它注入到spring容器中,并测试从spring容器获取这个 persion。
spring-test 模块是测试模块,我们就直接用这个模块来写一个bean 测试是否成功了。
-
在下面创建包和Persion类。 -
resource 目录下创建applicationContext.xml文件。
package org.springframework.test.yangzheng;
/**
* @author yangzheng
* @version 1.0
* @description
* @date 2022/6/8 008 20:52
*/
public class Person {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 导入约束之后,即可在此处装配bean了 -->
<bean id="person" class="org.springframework.test.yangzheng.Person" >
<property name="name" value="yangzheng"></property>
<property name="age" value="13"></property>
</bean>
</beans>
测试类
package org.springframework.test.yangzheng;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author yangzheng
* @version 1.0
* @description
* @date 2022/6/8 008 20:59
*/
public class TestBean {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("test-applicationContext.xml");
Person person = context.getBean("person", Person.class);
System.out.printf("person:"+person.toString());
}
}
注意事项
-
下载包可能会比较慢,有条件的同学可以用梯子,没有的话可以把gradle 远程仓库换成国内的源 -
如果报 pring-coresrcmainjavaorgspringframeworkcoreCoroutinesUtils.java:74 异常,或者 import jdk.jfr.Category错误,可以把项目jdk 换成 11 版本的 -
如果报 Cause: zip END header not found 问题,可以把 gradel 换成spring项目配置文件的gradle

好啦,今天的分享就到这里啦。
喜欢我的话,可以给我点个赞呀。
原文始发于微信公众号(小羊架构):spring 源码阅读(1) 搭建idea环境
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/259743.html