简介
SpringBoot是用来简化Spring应用开发的(比如之前使用的ssm框架,在搭建环境时就需要引入很多的依赖包),对spring技术的一个整合。
spring官网链接:click here
- 优点
1、快速创建独立运行环境的spring项目以及主流框架集成
2、使用嵌入式的Servlet容器,应用无需打成WAR包(内置tomcat)
3、starters自动依赖与版本控制(自带许多启动器)
4、大量的自动配置,简化开发,也可修改默认值
5、无需配置XML,无代码生成,开箱即用
6、准生产环境的运行时应用监控
7、与云计算的天然集成
入门小案例
1、创建一个maven项目(我是直接用的IDEA自带的maven)
2、导入soringboot依赖(这个是我在官网上面找到copy下来)
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</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>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<!--这个插件可以将应用打包成一个可执行的jar包,无需安装tomcat-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
这种张图片是我在官网上面截图的,官网上面有给出小案例
3、编写小程序
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//以下注解是用来标注一个主程序类,说明这是一个springboot应用
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);//启动spring应用
}
}
4、编写相关业务逻辑
@Controller
public class Hello{
@ResponseBody
@RequestMapping("/xx")
public String hello(){
return "hello spring boot";
}
}
5、接着就可以在主程序中运行了,在浏览器输入http://localhost:8080/xx,浏览器会打印 hello spring boot("xx"表示自行命名,注意:主程序一定要放在最外层的包里,不能和其它类放在同一个包下 )
看,是不是比当初部署一个ssm项目要简单很多!!!!
为什么变得如此简单呢??
因为在pom文件中的
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
而上面这个的父项目为:
...
<artifactId>spring-boot-dependencies</artifactId>
...
而这个里面的<properties>.....</properties>里面定义了每一个版本的依赖,管理了spring boot 应用的所有版本依赖。但也有一些没在里面,需要自己手写
- 启动器
- spring-boot-starter-web:
依赖包里面的 spring-boot-starter:spring-boot场景启动器;帮我们导入了web模块正常运行所依赖的组件
spring-boot-starter-xx:web、test、mail…
使用Spring Initializer快速创建springboot项目
默认生成的springboot项目:
1、主程序已经生成好了,只需写业务逻辑
2、resources 文件夹目录结构
static:保存所有的静态资源:js,css,images
templates:保存所有的模板页面:springboot默认的jar包使用嵌入式的tomcat,默认不支持jsp页面;可以使用模板引擎(freemarker,thymeleaf);
application.properties:springboot应用的配置文件,可以修改一下默认设置