创建第一个Spring Boot项目
1:新建一个Maven工程
2:在pom.xml下添加依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
spring-boot-starter-parent
Mave通过继承这个项目来获得一些合理的默认配置如下
1:默认使用java 8
2:默认使用UTF-8编码
3:引用管理功能(版本管理)
4:资源过滤(Sensible resource filtering)
5:识别插件配置(Sensible plugin configuration(exec plugin,surefire,git commit ID,shade))
6:能够识别application.properties和application.yml
7:默认占位符${...}改为了@...@
spring-boot-starter-web
默认嵌套tomcat的web应用,默认端口8080,可通过配置文件application.yml更改
3:新建一个helloController
@RestController
@SpringBootApplication
public class HelloController {
@RequestMapping("/hello")
String hello() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(HelloController.class, args);
}
}
@RestController = @Controller + @RequestBody
@SpringBootApplication = @Configuration @EnableAutoConfiguration @ComponentScan
1:@Configuration 注册bean到Spring容器管理
2:@EnableAutoConfiguration 自动配置Spring的上下文,自动根据你的类路径和你的 bean定义自动配置。
3:@ComponentScan 自动扫包下标有@Component注解及其子注解 @Repository;@Controller;@Service
4:运行helloControllerl类
浏览器url输入:http://localhost:8080/hello
5:查看结果