1.什么是 Spring Boot ?
关于这个问题,我想网上众说纷纭,每个了解springboot的人对于这个神奇的东西都会有一些自己的理解。为了深入解答这个问题,我特意去springboot官网找了官方对于springboot的描述。毕竟我们用的是人家的东西,一切还是以官方为准。
原文如下:
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.
We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.
翻译如下:
Spring Boot使您可以轻松地创建独立的、生产级的、基于Spring的应用程序,您可以“直接运行”该程序。
我们对Spring平台和第三方库有一个独到的见解(即集成了大量第三方库的配置),这样您就可以以最少的麻烦开始应用。并且大多数SpringBoot应用程序只需要很少的Spring配置。
从这段话中,我们可以提炼到两点信息:
一、springboot可以轻松地创建出基于spring的生产级应用程序并运行。
二、springboot配置文件很少。
有以上两点,我们可以给出springboot官方定义:
springboot是一款配置文件少、基于spring的、可以轻松创建并运行的生产级框架。
如果在面试的时候,碰到这个问题,只要你把这句话回答出来,面试官绝对不能说你错。你可以很自信地告诉面试官,springboot官方就是这样定义springboot的。
2.Spring Boot 有哪些特点 ?
一、starter简化依赖。
在spring中,如果要创建一个web工程,我们需要在pom.xml中添加很多依赖。而在springboot中,要创建一个web工程,只需要添加一个依赖,那就是starter:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
点击进入该依赖我们可以看到,spring-boot-starter-web
里面已经集成了原来spring所需要的依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.0.4.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>2.0.4.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.0.4.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.11.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.8.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.8.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
starter集成了大量依赖,避免了开发者自己引入依赖所带来的麻烦,大大节省了开发者开发的时间,提高了开发的效率。
二、支持自动配置功能(简化配置)
springboot支持自动配置主要依赖一个注解:
@SpringBootApplication
进入到该注解中,我们可以发现这是一个复合注解:
@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE})
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@java.lang.annotation.Documented
@java.lang.annotation.Inherited
@org.springframework.boot.SpringBootConfiguration
@org.springframework.boot.autoconfigure.EnableAutoConfiguration
而在这些注解中,EnableAutoConfiguration
表示开启自动配置功能。进入到该注解中,我们可以发现这也是一个复合注解:
@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE})
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@java.lang.annotation.Documented
@java.lang.annotation.Inherited
@org.springframework.boot.autoconfigure.AutoConfigurationPackage
@org.springframework.context.annotation.Import({org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.class})
其中@org.springframework.context.annotation.Import({org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.class})
,会给容器导入非常多的字段配置类,就是给容器导入场景所需要的组件,并配置好这些组件。
而当我们进入到@org.springframework.boot.autoconfigure.AutoConfigurationPackage
配置中时,可以看到:
@java.lang.annotation.Target({java.lang.annotation.ElementType.TYPE})
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@java.lang.annotation.Documented
@java.lang.annotation.Inherited
@org.springframework.context.annotation.Import({org.springframework.boot.autoconfigure.AutoConfigurationPackages.Registrar.class}
其中@org.springframework.context.annotation.Import({org.springframework.boot.autoconfigure.AutoConfigurationPackages.Registrar.class}
配置的作用就是将主配置中的包以及子包扫描到spring容器中。
经过上面几步,springboot就完成了自动配置的功能,从而简化了spring原有的复杂配置。这就springboot特有的习惯大于配置的原则,大部分配置使用默认配置即可。
三、其他特点
①内嵌tomcat和jetty容器,不需要单独安装容器,jar包直接发布一个web应用。
②简化maven配置,parent这种方式,一站式引入需要的各种依赖。
③和各种流行框架,spring web mvc,mybatis,spring cloud无缝整合。
3.Spring Boot 中的 parent到底是什么 ?
一、定义了java的默认编译版本为1.8。
<java.version>1.8</java.version>
二、定义了编码字符集为utf-8。
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
三、依赖继承自spring-boot-dependencies
,而在里面已经帮助我们定义了各种依赖的版本号,因此导入相关依赖时不需要再写版本号。
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
四、parent中有一个repackage
,具体如下:
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
repackage
可以理解为执行打包操作的配置。
五、合理的资源过滤。
选取了源码中两段资源过滤处,仅供参考。
<resource>
<filtering>true</filtering>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/application*.yml</include>
<include>**/application*.yaml</include>
<include>**/application*.properties</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
<excludes>
<exclude>**/application*.yml</exclude>
<exclude>**/application*.yaml</exclude>
<exclude>**/application*.properties</exclude>
</excludes>
</resource>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
六、合理的插件配置。
选取了源码中插件配置处,仅供参考。
<properties>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<resource.delimiter>@</resource.delimiter>
<maven.compiler.source>${java.version}</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
七、针对 application.properties 和 application.yml 的资源过滤,包括通过 profile 定义的不同环境的配置文件,例如 application-dev.properties 和 application-dev.yml。
<resource>
<filtering>true</filtering>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/application*.yml</include>
<include>**/application*.yaml</include>
<include>**/application*.properties</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
<excludes>
<exclude>**/application*.yml</exclude>
<exclude>**/application*.yaml</exclude>
<exclude>**/application*.properties</exclude>
</excludes>
</resource>
4.YAML 配置的优势在哪里 ?
现在不管是前端还是后端,yaml配置文件出现的频率都越来越高,那yaml配置的优势究竟有什么魅力呢?
一、配置有序。
yaml配置可以在以下四个地方:
①根目录下的config目录内
②根目录下
③classpath下的config目录内
④classpath下
在这个地方中,yaml配置文件的优先级依次下降。项目启动时,如果有多个yaml配置文件,优先读取优先级高的yaml配置文件。
二、支持数组注入
数组可以注入字符串,在Config类的servers列表中注入了两个字符串。
my:
servers:
- dev.example.com
- another.example.com
@ConfigurationProperties(prefix="my")
@Component
public class Config {
private List<String> servers = new ArrayList<String>();
public List<String> getServers() {
return this.servers;
}
}
yaml除了可以注入字符串,也可以注入对象。
redis:
redisConfigs:
- host: 192.168.66.128
port: 6379
- host: 192.168.66.129
port: 6380
@Component
@ConfigurationProperties(prefix = "redis")
public class RedisCluster {
private List<SingleRedisConfig> redisConfigs;
}
跟相对而言比较成熟的properties配置文件,@PropertySource注解不正常自定义的yaml配置文件。
5.比较一下 Spring Security 和 Shiro 各自的优缺点 ?
Spring Security 和 Shiro相比,主要有以下特点:
①Spring Security 是一个重量级的安全管理框架;而 Shiro是一个轻量级的安全管理框架。
②Spring Security 配置复杂,操作复杂; 而Shiro配置简单,操作简单。
③Spring Security 功能强大,而Shiro功能简单。
但由于在springboot中,提供了大量开箱即用的starter,其中就有Spring Security相关的starter,在springboot项目中,只需要一次依赖配置就可以保护各个接口,带来了极大的便利,所以在springboot项目中一般都使用Spring Security 安全框架。
6.微服务中如何实现 session 共享 ?
在微服务中,一个完整的项目被拆分成多个不相同的独立的服务,各个服务独立部署在不同的服务器上,各自的 session 被从物理空间上隔离开了,但是经常,我们需要在不同微服务之间共享 session ,常见的方案就是 Spring Session + Redis 来实现 session 共享。将所有微服务的 session 统一保存在 Redis 上,当各个微服务对 session 有相关的读写操作时,都去操作 Redis 上的 session 。这样就实现了 session 共享,Spring Session 基于 Spring 中的代理过滤器实现,使得 session 的同步操作对开发人员而言是透明的,非常简便。
7. Spring Boot 中如何实现定时任务 ?
定时任务也是一个常见的需求,Spring Boot 中对于定时任务的支持主要还是来自 Spring 框架。
在 Spring Boot 中使用定时任务主要有两种不同的方式,一个就是使用 Spring 中的 @Scheduled 注解,另一个则是使用第三方框架 Quartz。
使用 Spring 中的 @Scheduled 的方式主要通过 @Scheduled 注解来实现。
使用 Quartz ,则按照 Quartz 的方式,定义 Job 和 Trigger 即可。
8.springboot可以使用xml配置吗?
springboot可以使用xml配置,但需要通过@ImportResource 注解引入一个xml配置,但springboot推荐使用的是java配置。