一、springboot简介
- 简化spring应用开发的一个框架。
- 整个Spring技术栈的大整合。
- 简化了j2ee的笨重开发,提供一站式解决方案。
1、优点:
- 快速创建独立运行的spring项目和主流框架继成
- 使用嵌入式的servlet,应用无需打成war包
- starters自动依赖与版本控制,比如spring-boot-starter-web,spring-boot-starter-test,都是starter。
- 大量的自动配置,简化开发,也可修改默认值
- 无需配置xml,无代码生成,开箱即用。
- 准生产环境的运行时应用监控。
- 与云计算天然集成。目前个人还没感受到这方面的好处。
2、微服务
- 服务微化,每个功能单元都独立出来,服务之间通过某种方式进行互访,如http。
- springboot快速开发一个服务,使用springcloud进行互访。
3.springboot helloworld例子
- 创建一个springboot工程,idea->new project->Spring Initializr->web 成功创建一个springboot项目。
- idea为我们自动添加了依赖
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>helloworld</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<!-- springboot 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>
<!-- 这个插件可以把项目打包成一个可执行的jar包 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.HelloworldApplication启动类
@SpringBootApplication
public class HelloworldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloworldApplication.class, args);
}
}
@SpringBootApplication的作用,包含了以下三个功能:
- Spring的@Configuration:标明该类使用Spring基于Java的配置。
- Spring的@ComponentScan:启用组件扫描,这样你写的Web控制器类和其他组件才能被 自动发现并注册为Spring应用程序上下文里的Bean。稍后会写一个简单的Spring MVC 控制器,使用@Controller进行注解,这样组件扫描才能找到它。
- Spring Boot的@EnableAutoConfiguration:这个不起眼的小注解也可以称为 @Abracadabra1,就是这一行配置开启了Spring Boot自动配置的魔力,让你不用再写成 篇的配置了。
4.编写 HelloController
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("hello")
public String hello(){
return "hello world";
}
}
5.直接运行访问即可。
6.打成jar包,maven projects->Lifecycle->package
7.部署到服务器上,java -jar ***.jar运行。
4.pom.xml文件解析
- 父项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
它里面还有个父项目,真正管理springboot里面的所有依赖包的版本号。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
以后导入的依赖不需要写版本号,但是在父项目中没有版本号的,就需要额外添加了。
- 启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring-boot-starter:springboot的场景启动器。
spring-boot-starter-web:导入了web模块正常运行所依赖的组件。
springboot将所有的功能场景都抽取出来,做成一个个的starters(启动器), 像mq,mail,web开发,只需要在项目中导入这些starter相关的场景的所有依赖都会导入,需要什么功能就导入什么场景的启动器。
说明跳转地址
- @SpringBootApplication
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM,
classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
springboot 应用标注在某个类上说明这类是springboot的主配置类,springboot就应该运行在这类的main方法来启动springboot应用。
- @SpringBootConfiguration是包装了@Configuration,@Configuration配置类上标注这个注解。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
}
配置类代替配置文件,配置类就需要带上注解,配置类也是容器中的组件。
- @EnableAutoConfiguration:开启自动配置功能
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
-
@AutoConfigurationPackage:自动配置包
@Import(AutoConfigurationImportSelector.class)
spring的底层注解@Import,给容器中导入一个组件,导入的组件由
AutoConfigurationPackages.Registrar.class 将主配置(@SpringBootapplication)类所在的包下的所有组件导入容器。其他路径下的组件是扫描不进来的。 -
@Import(AutoConfigurationImportSelector.class)
给容器中导入组件,
AutoConfigurationImportSelector:导入哪些组件的容器
将所有需要导入的组件以全类名的方式返回; 这些组件就会添加到容器中,会给容器中导入非常多的自动配置类(xxxAutoConfiguration);就是给容器中导入这个场景所需要的所有组件,并配置好这些组件。
Spring Boot在启动的时候从类路径喜爱的META/spring.factories中获取enableAutoConfiguration指定的值,将这些值作为自动配置类导入到容器中,自动配置类就生效类。
5.默认创建项目结构
resources文件中的目录结构
-
statics: 保存所有的静态资源:js css images
-
templates: 保存所有的模版页面;(Spring Boot默认jar包使用嵌入式tomcat,默认不支持jsp,以前也试过使用jsp开发页面,个人感觉很恶心,各种兼容问题);可以使用模版引擎(freemarker、thymeleaf)
-
application.properties: Spring Boot应用的配置文件;可以修改一些默认设置。