一直觉得spring很强大,也是目前最流行的框架,虽然现在有了springboot,但是springboot也是建立在原先的spring基础上的,所以个人觉得阅读spring的源码很有必要,以后跳槽什么的,说自己看过spring源码,甚至能手写一个spring框架,就觉得很牛不是嘛?
于是下决心花点时间来学习spring的源码。
Spring 的核心springcode里包含了四个模块,core,bean,context,apo和expression语言表达式支持包。
就先从配置方式入手,一步步调代码来学吧。先写一个hello world然后一步步学习。
首先是创建一个maven项目SpringCode
2.pom文件引入spring的jar包
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.weiguozhui</groupId>
<artifactId>SpringCode</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringCode</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
</dependencies>
</project>
3.编写bean类
package com.weiguozhui.SpringCode;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestMain {
public static void main(String[] args) {
//加载配置文件
ApplicationContext context=new ClassPathXmlApplicationContext("/application-context.xml");
// 读取配置文件中配置的bean
Hello hello=(Hello) context.getBean("hello");
System.out.println(hello.getName()+hello.getAge());
}
5.运行输出结果
搭建完成,这里不多啰嗦搭建的步骤环节。仅为记录自己的进度,有缘看到可共勉