开篇
最近想来研究研究下spring的源代码,在此之前都是使用其部分的功能,比如springmvc,事务管理等,并未对其内部的原理进行研究过,对于这么一个经常使用的开源框架 得好好的去学习它里面的一些设计原理和思想,对自己来说也是一个进步。
目前使用的是spring3.2.2的版本,那么就拿这个版本来学习吧。
目录:
1.下载源代码
2.一个简单的HelloWorld示例
3.如何阅读源码
如何下载源代码:
https://github.com/SpringSource/spring-framework
选择对应的版本即可
jar的选择:
可以暂时将所有的spring包都添加进来
第一个包commons-logging是必需的,没有的话,运行会抛出异常,将这些包都导入到工程的classpath路径下
helloWorld 示例 :
package org.spring.read.read01;
public class HelloWorld {
private String name = null;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package org.spring.read.read01;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring.xml");
HelloWorld helloWord = (HelloWorld)context.getBean("helloWord");
System.out.println(helloWord.getName());
}
}
配置文件spring.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<bean id="helloWord" class="org.spring.read.read01.HelloWorld">
<property name="name">
<value>hello World</value>
</property>
</bean>
</beans>
将spring.xml文件放入src根目录下
如何关联源代码:
右键选中当前工程spirng,Build Path->Configure Build Path->
选择相应的source源码包路径
设置完成之后,可以采用单步调试的方法进行源代码