基础入门
1.引入依赖
创建一个普通jar类型的maven工程,pom.xml:
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.5.RELEASE</version><relativePath/></parent><dependencies><!-- springBoot的各种启动器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies>
spring-boot-starter-parent作用
在pom.xml中引入spring-boot-start-parent,它可以提供dependency management,也就是说依赖管理,引入以后在申明其它dependency的时候就不需要version了,后面可以看到。
spring-boot-starter-web作用
springweb 核心组件
2.编写控制器
@Controllerpublic class HelloController {@RequestMapping("/hello")@ResponseBodypublic Map<String, Object> showHello() {Map<String, Object> map = new HashMap<>();map.put("msg", "hello world");return map;}}
3.编写启动器
@SpringBootApplicationpublic class App {public static void main(String[] args) {SpringApplication.run(App.class, args);System.out.println(args);}}
@SpringBootApplication其实就是以下三个注解的总和
@Configuration:用于定义一个配置类
@EnableAutoConfiguration :Spring Boot会自动根据你jar包的依赖来自动配置项目。
@ComponentScan:告诉Spring 哪个packages 的用注解标识的类 会被spring自动扫描并且装入bean容器。
静态资源的访问
Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则:
/static
/public
/resources
/META-INF/resources
