有两种方式可以创建 1是使用spring-boot-start-parent ,2是使用spring-boot-dependencies (即父项目dependencyManagement)
(同理springcloud 项目也可以使用两种方式创建,推荐使用dependencyManagement,后续笔记中补充)
1.使用 spring-boot-start-parent创建
org.springframework.boot
spring-boot-starter
1.5.9.RELEASE
junit
junit
4.11
test
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-maven-plugin
2.使用 spring-boot-dependencies创建 (推荐使用此种方式)
2.1 创建一个父级maven项目,并在父pom中添加依赖
org.springframework.boot
spring-boot-dependencies
1.5.9.RELEASE
pom
import
2.2 在子模块中 添加依赖
junit
junit
4.11
test
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-maven-plugin
3. 编写java代码
3.1新增一个controller
packagecom.itstudy.controller;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RestController;
@RestControllerpublic classIndexController {
@GetMapping("/index")publicString getIndex()
{return "hello world";
}
}
3.2 修改主函数(App.java中的main函数)
packagecom.itstudy;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;/*** Hello world!*/@SpringBootApplicationpublic classApp {public static voidmain(String[] args) {
SpringApplication.run(App.class);
}
}
说明@SpringBootApplication会扫描 自己所在的包以及子包下面所有的类 。
如果需要扫描不在同一包下的类,需要增加
@ComponentScan({"jar包名"})