spring源码学习2 - 创建Demo调试spring源码
1、创建Demo项目
创建一个Gradle Module ![](https://img2018.cnblogs.com/blog/1373276/201903/1373276-20190306163342459-1119854604.png)2、修改build.gradle
build.gradle 需要添加以下依赖(PS:网上关于这块的帖子实在太少了,调试过程中各种报错,本人走了不少弯路才总结出来的,目前最新版的spring至少要以下5种依赖) ``` plugins { id 'java' }group 'org.springframework'
version '5.2.0.BUILD-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile(project(":spring-beans"))
compile(project(":spring-core"))
compile(project(":spring-expression"))
compile(project(":spring-context"))
compile(project(":spring-instrument"))
testCompile group: 'junit', name: 'junit', version: '4.12'
}
创建一个service类
package com.springsource;
public class LoginService {
public String login(String username){
System.out.println(username + "登录...");
return "success";
}
}
applicationContext.xml
<bean id="loginService" class="com.springsource.LoginService"/>
```
创建测试类:
```
package com.springsource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
LoginService service = (LoginService)context.getBean("loginService");
service.login("admin");
}
}
<h1>3、运行</h1>
运行测试类,成功!
![](https://img2018.cnblogs.com/blog/1373276/201903/1373276-20190306173358492-288823965.png)
最终的项目结构如下:
![](https://img2018.cnblogs.com/blog/1373276/201903/1373276-20190306172634015-877189142.png)