Spring 导入Idea
在导入的基础上,新建一个模块,专门学习用。
一、 从github上将Spring源码拉到本地。
https://github.com/spring-projects/spring-framework
本文以Spring 4.3.2 为例
如下图所示
二、本地CMD运行编译
根据项目中 ‘import-into-idea.md’ 中所示运行如下图:
前提是电脑中已经安装了gradle
在CMD中,项目还会重新下载一次gradle
三、导入到idea中
选择项目目录位置
下一界面
附cmd中下载的gradle路径地址:
C:/Users/{UserName}/.gradle/wrapper/dists/gradle-2.13-bin/4xsgxlfjcxvrea7akf941nvc7/gradle-2.13
去除掉spring-aspectj模块
四、新建自己的学习模块
如上图中的Spring-study就是新建的学习模块。可以用来调试源码用。
删掉idea自动生成的 build.gradle文件。
在Spring项目中的build.gradle中添加Spring-study模块的信息。
project("spring-study"){
description="Spring Study"
dependencies {
compile(project(":spring-aop"))
compile(project(":spring-beans"))
compile(project(":spring-context"))
compile(project(":spring-core"))
compile(project(":spring-expression"))
compile(project(":spring-web"))
provided("javax.servlet:javax.servlet-api:3.1.0")
optional(project(":spring-context-support")) // for Velocity support
optional(project(":spring-oxm")) // for MarshallingView
optional("javax.servlet.jsp:javax.servlet.jsp-api:2.2.1")
optional("javax.servlet.jsp.jstl:javax.servlet.jsp.jstl-api:1.2.1")
optional("net.sourceforge.jexcelapi:jxl:2.6.12")
optional("org.apache.poi:poi:${poiVersion}")
optional("org.apache.poi:poi-ooxml:${poiVersion}")
optional("org.apache.velocity:velocity:1.7")
optional("velocity-tools:velocity-tools-view:1.4")
optional("org.freemarker:freemarker:${freemarkerVersion}")
optional("org.codehaus.groovy:groovy-all:${groovyVersion}")
optional("com.lowagie:itext:2.1.7")
optional("log4j:log4j:${log4jVersion}")
optional("net.sf.jasperreports:jasperreports:$jasperreportsVersion") {
exclude group: "com.fasterxml.jackson.core", module: "jackson-annotations"
exclude group: "com.fasterxml.jackson.core", module: "jackson-core"
exclude group: "com.fasterxml.jackson.core", module: "jackson-databind"
exclude group: "org.olap4j", module: "olap4j"
exclude group: "xml-apis", module: "xml-apis"
exclude group: "org.springframework", module: "spring-context"
}
optional("com.fasterxml.jackson.core:jackson-databind:${jackson2Version}")
optional("com.fasterxml.jackson.dataformat:jackson-dataformat-xml:${jackson2Version}")
optional("com.rometools:rome:${romeVersion}")
optional("javax.el:javax.el-api:2.2.5")
optional("org.apache.tiles:tiles-api:${tiles3Version}")
optional("org.apache.tiles:tiles-core:${tiles3Version}") {
exclude group: "org.slf4j", module: "jcl-over-slf4j"
}
optional("org.apache.tiles:tiles-servlet:${tiles3Version}") {
exclude group: "org.slf4j", module: "jcl-over-slf4j"
}
optional("org.apache.tiles:tiles-jsp:${tiles3Version}") {
exclude group: "org.slf4j", module: "jcl-over-slf4j"
}
optional("org.apache.tiles:tiles-el:${tiles3Version}") {
exclude group: "org.slf4j", module: "jcl-over-slf4j"
}
optional("org.apache.tiles:tiles-extras:${tiles3Version}") {
exclude group: "org.slf4j", module: "jcl-over-slf4j"
exclude group: "org.springframework", module: "spring-web"
}
optional('org.webjars:webjars-locator:0.32')
testCompile("xmlunit:xmlunit:${xmlunitVersion}")
testCompile("dom4j:dom4j:1.6.1") {
exclude group: "xml-apis", module: "xml-apis"
}
testCompile("jaxen:jaxen:1.1.1") {
exclude group: "xml-apis", module: "xml-apis"
exclude group: "xom", module: "xom"
exclude group: "xerces", module: "xercesImpl"
}
testCompile("org.eclipse.jetty:jetty-servlet:${jettyVersion}") {
exclude group: "javax.servlet", module: "javax.servlet"
}
testCompile("org.eclipse.jetty:jetty-server:${jettyVersion}") {
exclude group: "javax.servlet", module: "javax.servlet"
}
testCompile("javax.validation:validation-api:1.0.0.GA")
testCompile("org.hibernate:hibernate-validator:${hibval4Version}")
testCompile("org.apache.httpcomponents:httpclient:${httpclientVersion}")
testCompile("commons-fileupload:commons-fileupload:${fileuploadVersion}")
testCompile("commons-io:commons-io:1.3")
testCompile("joda-time:joda-time:${jodaVersion}")
testCompile("org.slf4j:slf4j-jcl:${slf4jVersion}")
testCompile("org.mozilla:rhino:1.7.7.1")
testRuntime("org.jruby:jruby:${jrubyVersion}")
testRuntime("org.python:jython-standalone:2.5.3")
testRuntime("org.webjars:underscorejs:1.8.3")
}
}
上面这些没什么,,我直接从Spring-webmvc中拷贝的。 实际上可以根据自己需要的精简。
新建程序:
这个大家应该很熟悉了, Member是实体类,testSpring 是一个main函数。
ApplicationContext是Spring的配置文件
代码如下:
Member.java
/**
* Created by lixiang on 2016/8/3.
*/
public class Member {
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
testSpring.java
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
/**
* Created by lixiang on 2016/8/3.
*/
public class testSpring {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:ApplicationContext.xml");
Member member = applicationContext.getBean("member",Member.class);
System.out.println(member.getAge());
/*ClassPathResource res = new ClassPathResource("ApplicationContext.xml");
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
reader.loadBeanDefinitions(res);
Member personBean = (Member) factory.getBean("member");
System.out.println(personBean.getName());*/
}
}
ApplicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="member" class="Member">
<property name="age">
<value>22</value>
</property>
<property name="name"><value>lixiang</value></property>
</bean>
</beans>
然后进cmd , 再执行一遍导入的命令
gradlew cleanIdea :spring-oxm:compileTestJava
然后运行Main函数
也可以在Spring源码中进行debug :
Last : 祝大家学习愉快 。
博客后期会把学习笔记发出来,大家一起学习!!!