1.spring boot搭建项目
1.1创建maven项目
略
1.2添加依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
spring-boot-starter-parent是一个特殊的starter,提供了一些maven的默认配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.7.RELEASE</version>
</dependency>
spring-boot-starter-web依赖引入
1.3编写启动类
//@EnableAutoConfiguration
//ComponentScan
@SpringBootApplication
public class Demotest01 {
public static void main(String[] args) {
SpringApplication.run(Demotest01.class,args);
System.out.println("开始");
}
}
- EnableAutoConfiguration注解表示开启自动化配置,由于项目中添加了spring-boot-starter-web依赖,因此在开启自动化配置之后会自动进行Spring和SpringMVC的配置。
- 在java项目的main方法,通过SpringApplication中的方法开启项目。第一个参数传入本类,告诉Spring的那个是主要组件,第二个参数是运行时输入的其他参数。
1.4创建SpringMvc的控制器
@RestController
public class LoginController {
@RequestMapping("/login")
public String index (){
return "index";
}
}
控制器中提供一个"/login"的接口,因此需要配置包扫描才能将LoginController注册到SpringMVC的容器中,因此在启动类上加上@ComponentScan进行扫描。