版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_23490433/article/details/89159957
1、什么是springBoot?
springBoot的设计目的是为了简化开发,开启了各种自动装配。如果你不想写各种繁琐的配置文件,引入相关的依赖就能迅速的搭建起一个web工程。springBoot采用的是建立生产就绪的应用程序观点,优先于配置的惯例。
2、springBoot入门示例
打开ideaècreate new projectèSpring initializerè填写group、artifactè勾上web(开启web功能)è点击下一步就行了
工程目录结构如下:
pom文件为基本的依赖管理文件
resources资源文件
statics静态资源
Templates模板资源
application.yml配置文件
SpringbootApplication程序的入口
1)、pom.xml的依赖如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cn</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<!--
1、指定parent为spring-boot-starter-parent
parent中已经定义了spring的基本属性
指定了spring的版本号在dependencies中无需再指定版本号
parent中还包含了其它大量默认配置,大大简化了我们的开发
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!-- 2、支持web应用开发,包含springmvc和Tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 3、包含常用的测试所需的依赖,如junit、Mockito和spring-test等 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!--
springBoot的maven插件,能够以maven的方式为应用提供springBoot的支持
能够将springBoot应用打包为可执行的jar或war文件,然后以通常的方式运行springBoot的应用
springBoot的maven 插件的5个Goals
1)、spring-boot:repackage,默认goal。在maven package之后,再次打包可执行的jar/war,同时保留mvn package
生成的jar/war 为.origin
2)、spring-boot:run 运行springBoot应用
3)、spring-boot:start 在mvn integration-test阶段,进行spring Boot应用生命周期的管理
4)、spring-boot:stop 在mvc integration-test阶段,进行spring Boot应用声明周期的管理
5)、spring-boot:build-info 生成actuator使用的构建新型文件build-info.properties
-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2)、新建包com.cn.controller,然后在该包下新建类HelloController,代码如下:
package com.cn.controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* springBoot默认集成springMvc
* */
@RestController //表示该类中的所有方法返回@Controller+@ResponseBody,为微服务提供返回json格式
@EnableAutoConfiguration //扫描范围,默认在当前类里面 第一种启动方式
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "hello springBoot!";
}
public static void main(String[] args) {
SpringApplication.run(HelloController.class,args); //整个程序的入口,启动springBoot项目
}
}
3)、启动main方法,打开浏览器http://localhost:8080/hello,浏览器显示如下:
神奇之处:
1)、我们没有进行任何的web.xml配置
2)、我们没有配置tomcat(因为springBoot内置嵌入了tomcat))
3)、我们没有做任何的spring mvc配置(因为springBoot为我们做了)