准备工作
本示例基于 SpringV5.2.17RELEASE+GradleWapper+jdk1.8.0_131编译。
环境准备
- maven
- idea
- jdk8+
源码下载
进入GitHub - spring-projects/spring-framework: Spring Framework下载源码
直接用Mac版idea打开,就可以编译完成。
添加测试模块代码:
new->module->gradle-->输入模块名称(IocTest)
编写代码测试ioc功能。
1、添加依赖
dependencies {
compile(project(":spring-context"))
}
compile(project(":spring-context")) 代表本项目的。
2、随意添加任意bean
package com.mrkid1018.service;
import org.springframework.stereotype.Service;
@Service
public class MessageService {
public void sayHi(){
System.out.println("Hello Spring!");
}
}
3、添加启动配置类
package com.mrkid1018.service;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.mrkid1018")
public class MainStat {
public static void main(String[] args){
ApplicationContext context = new AnnotationConfigApplicationContext(MainStat.class);
MessageService bean = context.getBean(MessageService.class);
bean.sayHi();
}
}
4、正确输出,大功告成!