创建SpringBoot项目
创建一个普通的Maven项目。然后在pom.xml中加入以下的配置
<!--父依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<!--web依赖-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
spring-boot-starter-parent是一个父工程,里面规定了很多的项目版本管理。引入spring-boot-starter-web 就可以快速启动跟springmvc一样效果的应用程序。
之后创建一个启动类 com.gaby.boot.web.Application
package com.gaby.boot.web;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages="com.gaby.boot")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@SpringBootApplication注解相当于@SpringBootConfirguration与@EnableAutoConfiguration和@ComponentScan的作用
@ComponentScan:顾名思义就是包扫描呗,为了获取到Bean并且管理它。
在创建一个com.gaby.boot.controller.UserController类
package com.gaby.boot.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
@RequestMapping("hello")
public void hello() {
System.out.println("hello ");
}
}
然后点击Application启动类中的main方法,整个项目就被启动了。访问路径为 localhost:8080/user/hello