基础入门

1.引入依赖

创建一个普通jar类型的maven工程,pom.xml:

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.1.5.RELEASE</version>
  5. <relativePath/>
  6. </parent>
  7. <dependencies>
  8. <!-- springBoot的各种启动器 -->
  9. <dependency>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-web</artifactId>
  12. </dependency>
  13. </dependencies>

spring-boot-starter-parent作用
在pom.xml中引入spring-boot-start-parent,它可以提供dependency management,也就是说依赖管理,引入以后在申明其它dependency的时候就不需要version了,后面可以看到。
spring-boot-starter-web作用
springweb 核心组件

2.编写控制器

  1. @Controller
  2. public class HelloController {
  3. @RequestMapping("/hello")
  4. @ResponseBody
  5. public Map<String, Object> showHello() {
  6. Map<String, Object> map = new HashMap<>();
  7. map.put("msg", "hello world");
  8. return map;
  9. }
  10. }

3.编写启动器

  1. @SpringBootApplication
  2. public class App {
  3. public static void main(String[] args) {
  4. SpringApplication.run(App.class, args);
  5. System.out.println(args);
  6. }
  7. }

@SpringBootApplication其实就是以下三个注解的总和

@Configuration:用于定义一个配置类
@EnableAutoConfiguration :Spring Boot会自动根据你jar包的依赖来自动配置项目。
@ComponentScan:告诉Spring 哪个packages 的用注解标识的类 会被spring自动扫描并且装入bean容器。

静态资源的访问

Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则:
/static
/public
/resources
/META-INF/resources