Spring Boot 需要独立的容器运行吗?

可以不需要,内置了 Tomcat/ Jetty 等容器。通过pom.xml中导入依赖:

<!--spring-boot-starter-web:代表web模块,在这个模块中含了许多JAR包,-->
<!--有spring相关的jar,内置tomcat服务器,jackson等,这些web项目中常用的的功能都会自动引入-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

Spring Boot的核心注解是哪个?它主要由哪几个注解组成的?

启动类上面的注解是@SpringBootApplication,它也是 Spring Boot 的核心注解,主要组合包含了以下 3 个注解:

@SpringBootConfiguration

@EnableAutoConfiguration:打开自动配置的功能,也可以关闭某个自动配置的选项,如关闭数据源自动配置功能:@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })

@ComponentScan:Spring组件扫描。

如何理解Spring Boot中Starters含义?

Starters是什么?

Starters可以理解为启动器,它包含了一系列可以集成到应用里面的依赖包,你可以一站式集成Spring及其他技术,而不需要到处找示例代码和依赖包。如你想使用Spring JPA访问数据库,只要加入spring-boot-starter-data-jpa启动器依赖就能使用了。Starters包含了许多项目中需要用到的依赖,它们能快速持续的运行,都是一系列得到支持的管理传递性依赖。

Starters命名?

Spring Boot官方的启动器都是以spring-boot-starter-命名的,代表了一个特定的应用类型。第三方的启动器不能以spring-boot开头命名,它们都被Spring Boot官方保留。一般一个第三方的应该这样命名,像mybatis的mybatis-spring-boot-starter

Starters分类:

1)Spring boot提供的启动器

启动器名称 功能描述
spring-boot-starter 核心模块,包含自动配置支持、日志库和对 YAML 配置文件的支持。
spring-boot-starter-amqp 通过 spring-rabbit 来支持AMQP协议(Advanced Message Queuing Protocol)
spring-boot-starter-aop 支持面向方面的编程即AOP,包括 spring-aop 和 AspectJ
spring-boot-starter-artemis 通过 Apache Artemis 支持 JMS 的 API(Java Message Service API)
spring-boot-starter-batch 支持 Spring Batch,包括 HSQLDB 数据库
spring-boot-starter-cache 支持 Spring 的 Cache 抽象
spring-boot-starter-cloud-connectors 支持 Spring Cloud Connectors,简化了在像 Cloud Foundry 或 Heroku 这样的云平台上连接服务
spring-boot-starter-data-elasticsearch 支持 ElasticSearch 搜索和分析引擎,包括 spring-data-elasticsearch
spring-boot-starter-data-gemfire 支持 GemFire 分布式数据存储,包括 spring-data-gemfire
spring-boot-starter-data-jpa 支持 JPA(Java Persistence API),包括 spring-data-jpa、spring-orm、Hibernate
spring-boot-starter-data-solr 支持 Apache Solr 搜索平台,包括 spring-data-solr
spring-boot-starter-data-mongodb 支持MongoDB数据,包括spring-data-mongodb
spring-boot-starter-data-rest 通过 spring-data-rest-webmvc,支持通过 REST 暴露 Spring Data 数据仓库
spring-boot-starter-redis 支持 Redis 键值存储数据库,包括 spring-redis
spring-boot-starter-data-jdbc 支持 JDBC 访问数据库
spring-boot-starter-jta-atomikos 通过 Atomikos 支持 JTA 分布式事务处理
spring-boot-starter-jta-bitronix 通过Bitronix支持JTA分布式事务处理
spring-boot-starter-security 支持 spring-security
spring-boot-starter-test 支持常规的测试依赖,包括JUnit、Hamcrest、Mockito以及spring-test模块
spring-boot-starter-velocity 支持Velocity模板引擎
spring-boot-starter-freemarker 支持 FreeMarker 模板引擎
spring-boot-starter-thymeleaf 支持 Thymeleaf 模板引擎,包括与Spring的集成
spring-boot-starter-mustache 支持 Mustache 模板引擎
spring-boot-starter-web 支持全栈式 Web 开发,包括 Tomcat 和 spring-webmvc
spring-boot-starter-websocket 支持 WebSocket 开发
spring-boot-starter-ws 支持 Spring Web Services
spring-boot-starter-groovy-templates 支持 Groovy 模板引擎
spring-boot-starter-hateoas 通过 spring-hateoas 支持基于 HATEOAS 的 RESTful Web 服务
spring-boot-starter-hornetq 通过 HornetQ 支持 JMS
spring-boot-starter-log4j 支持 Log4J 日志框架
spring-boot-starter-logging 引入了 Spring Boot 默认的日志框架 Logback
spring-boot-starter-integration 支持通用的 spring-integration 模块
spring-boot-starter-jersey 支持 Jersey RESTful Web 服务框架
spring-boot-starter-mail 支持 javax.mail 模块
spring-boot-starter-mobile 支持 spring-mobile
spring-boot-starter-social-facebook 支持 spring-social-facebook
spring-boot-starter-social-linkedin 支持 spring-social-linkedin
spring-boot-starter-social-twitter 支持 spring-social-twitter
spring-boot-starter-actuator 增加了面向产品上线相关的功能,比如测量和监控
spring-boot-starter-remote-shell 增加了远程ssh shell的支持
spring-boot-starter-tomcat 引入了 Spring Boot 默认的 HTTP 引擎 Tomcat
spring-boot-starter-jetty 引入了Jetty HTTP引擎(用于替换Tomcat)
spring-boot-starter-undertow 引入了Undertow HTTP引擎(用于替换Tomcat)

4)其他第三方启动器(略)

Spring Boot实现热部署有哪几种方式?

在Spring Boot实现代码热部署是一件很简单的事情,代码的修改可以自动部署并重新热启动项目。

1)引用devtools依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>

修改一个java类时就可以实现热更新了。

2)自定义配置热部署

以下配置用于自定义配置热部署,可以不设置。

#热部署开关,false即不启用热部署

spring.devtools.restart,enabled: true

#指定热部署的目录

#spring.devtools.restart.additional-paths: src/main/java

#指定目录不更新

spring.devtools.restart.exclude: test/**

3)Intellij Idea工具修改实现热部署

需要改以下两个位置:

勾上自动编译或者手动重新编译

File > Settings > Compiler-Build Project automatically

注册使用快捷键的方式:

ctrl + shift + alt + / > Registry > 勾选Compiler autoMake allow when app running

注意事项:

1)生产环境devtools将被禁用,如java -jar方式或者自定义的类加载器等都会识别为生产环境。

2)打包应用默认不会包含devtools,除非你禁用SpringBoot Maven插件的excludeDevtools属性。

3)Thymeleaf无需配置 spring.thymeleaf.cache:false,devtools默认会自动设置,参考完整属性。

下面是devtools自动配置的完整源码:

@Order(Ordered.LOWEST_PRECEDENCE)
public class DevToolsPropertyDefaultsPostProcessor implements EnvironmentPostProcessor {
private static final Map<String, Object> PROPERTIES;
static {
Map<String, Object> devToolsProperties = new HashMap<>();
devToolsProperties.put("spring.thymeleaf.cache", "false");
devToolsProperties.put("spring.freemarker.cache", "false");
devToolsProperties.put("spring.groovy.template.cache", "false");
devToolsProperties.put("spring.mustache.cache", "false");
devToolsProperties.put("server.servlet.session.persistent", "true");
devToolsProperties.put("spring.h2.console.enabled", "true");
devToolsProperties.put("spring.resources.cache.period", "0");
devToolsProperties.put("spring.resources.chain.cache", "false");
devToolsProperties.put("spring.template.provider.cache", "false");
devToolsProperties.put("spring.mvc.log-resolved-exception", "true");
devToolsProperties.put("server.servlet.jsp.init-parameters.development", "true");
devToolsProperties.put("spring.reactor.stacktrace-mode.enabled", "true");
PROPERTIES = Collections.unmodifiableMap(devToolsProperties);
}
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application) {
if (isLocalApplication(environment) && canAddProperties(environment)) {
PropertySource<?> propertySource = new MapPropertySource("refresh",
PROPERTIES);
environment.getPropertySources().addLast(propertySource);
}
}
private boolean isLocalApplication(ConfigurableEnvironment environment) {
return environment.getPropertySources().get("remoteUrl") == null;
}
private boolean canAddProperties(Environment environment) {
return isRestarterInitialized() || isRemoteRestartEnabled(environment);
}
private boolean isRestarterInitialized() {
try {
Restarter restarter = Restarter.getInstance();
return (restarter != null && restarter.getInitialUrls() != null);
}
catch (Exception ex) {
return false;
}
}
private boolean isRemoteRestartEnabled(Environment environment) {
return environment.containsProperty("spring.devtools.remote.secret");
}
}

4)devtools会在windows资源管理器占用java进程,在开发工具里面杀不掉,只能手动kill掉,不然重启会选成端口重复绑定报错。

Spring Boot如何定义多套不同环境配置?

简单来说,Profile就是Spring Boot可以对不同环境或者指令来读取不同的配置文件。

假如有开发、测试、生产三个不同的环境,需要定义三个不同环境下的配置。

1)基于properties文件类型可以另外建立3个环境下的配置文件:

applcation.properties
application-dev.properties
application-test.properties
application-prod.properties

然后在applcation.properties文件中指定当前的环境spring.profiles.active=test,这时候读取的就是application-test.properties文件。

2)基于yml文件类型,只需要一个applcation.yml文件即可,推荐此方式。关注微信公众号“Java精选”(w_z90110),回复关键字领取资料:如MysqlHadoopDubboCAS源码等等,免费领取视频教程、资料文档和项目源码。Java面试题持续更新中...

spring:
profiles:
active: prod
---
spring:
profiles: dev
server:
port: 8080 ---
spring:
profiles: test
server:
port: 8081
---
spring.profiles: prod
spring.profiles.include:
- proddb
- prodmq
server:
port: 8082 ---
spring:
profiles: proddb
db:
name: mysql ---
spring:
profiles: prodmq
mq:
address: localhost

其中dev代表开发,test代表测试,prod代表正式环境。此时读取的就是prod的配置,prod包含proddb,prodmq,此时可以读取proddb,prodmq下的配置。也可以同时激活三个配置,如下:

spring.profiles.active: prod,proddb,prodmq

  

3)基于Java代码,在JAVA配置代码中也可以加不同Profile下定义不同的配置文件,@Profile注解只能组合使用@Configuration@Component注解。

@Configuration
@Profile("prod")
public class ProductionConfiguration {
// ...
}

  

4)指定Profile

main方法启动方式:在Eclipse Arguments里面添加

--spring.profiles.active=prod

插件启动方式:

spring-boot:run -Drun.profiles=prod

jar运行方式:

java -jar xx.jar --spring.profiles.active=prod

除了在配置文件和命令行中指定Profile,还可以在启动类中写死指定,通过SpringApplication.setAdditionalProfiles方法

SpringApplication.class
public void setAdditionalProfiles(String... profiles) {
this.additionalProfiles = new LinkedHashSet<String>(Arrays.asList(profiles));
}

转载自:互联网大厂Java面试题集—Spring boot面试题(一)

互联网大厂Java面试题集—Spring boot面试题(一)的更多相关文章

  1. 互联网大厂Java面试题集—Spring boot常见面试题(二)

    Spring Boot的核心功能与使用优点? 核心功能: 1)Spring Boot项目为独立运行的spring项目,java -jar xx.jar即可运行. 2)内嵌servlet容器(可以选择内 ...

  2. Java精选面试题之Spring Boot 三十三问

    Spring Boot Spring Boot 是微服务中最好的 Java 框架. 我们建议你能够成为一名 Spring Boot 的专家. 问题一: Spring Boot.Spring MVC 和 ...

  3. Java面试题(Spring Boot/Spring Cloud篇)

    Spring Boot/Spring Cloud 104.什么是 spring boot? SpringBoot是一个框架,一种全新的编程规范,他的产生简化了框架的使用,所谓简化是指简化了Spring ...

  4. Spring Boot面试题

    Spring Boot 是微服务中最好的 Java 框架. 我们建议你能够成为一名 Spring Boot 的专家. 问题一 Spring Boot.Spring MVC 和 Spring 有什么区别 ...

  5. 吐血整理 20 道 Spring Boot 面试题,我经常拿来面试别人!

    面试了一些人,简历上都说自己熟悉 Spring Boot, 或者说正在学习 Spring Boot,一问他们时,都只停留在简单的使用阶段,很多东西都不清楚,也让我对面试者大失所望. 下面,我给大家总结 ...

  6. 紧急整理了 20 道 Spring Boot 面试题,我经常拿来面试别人!

    面试了一些人,简历上都说自己熟悉 Spring Boot, 或者说正在学习 Spring Boot,一问他们时,都只停留在简单的使用阶段,很多东西都不清楚,也让我对面试者大失所望. 下面,我给大家总结 ...

  7. 20 道 Spring Boot 面试题

    转自:微信公众号:Java技术栈(id: javastack) 面试了一些人,简历上都说自己熟悉 Spring Boot, 或者说正在学习 Spring Boot,一问他们时,都只停留在简单的使用阶段 ...

  8. 【转】20道Spring Boot面试题

    面试了少量人,简历上都说自己熟习 Spring Boot, 或者者说正在学习 Spring Boot,一问他们时,都只停留在简单的使用阶段,很多东西都不清楚,也让我对面试者大失所望. 下面,我给大家总 ...

  9. 20道spring boot面试题

    面试了少量人,简历上都说自己熟习 Spring Boot, 或者者说正在学习 Spring Boot,一问他们时,都只停留在简单的使用阶段,很多东西都不清楚,也让我对面试者大失所望. 下面,我给大家总 ...

随机推荐

  1. C # socket 实例

    同步客户端存储示例 下面的示例程序创建连接到服务器的客户端.             客户端使用一个同步套接字生成,因此,客户端应用程序的执行挂起,直到服务器返回响应.  应用程序将字符串发送到服务器 ...

  2. css圆,背景,img填满等样式

    background 属性 属性值 描述 background-color 单词颜色表示法.rgb.十六进制 设置元素的背景颜色 background-image url('http://www.aa ...

  3. c 语言I博客作业02

    这个作业属于哪个课程 C语言程序设计1 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/SE2019-2/homework/8687 我在这个课程的目标是 ...

  4. 2019-2020-4 20199317《Linux内核原理与分析》第四周作业

    第3章  MenuOS的构造 1  Linux内核源代码简介        计算机的“3大法宝”:存储程序计算机.函数调用堆栈和中断. 操作系统的“两把宝剑”:一把是中断上下文的切换——保存现场和恢复 ...

  5. Java泛型之上、下界通配符的理解(适合初学)

    泛型的由来 为什么需要泛型   Java的数据类型一般都是在定义时就需要确定,这种强制的好处就是类型安全,不会出现像弄一个ClassCastException的数据给jvm,数据安全那么执行的clas ...

  6. list tuple dict 方法

    >>> dir(list)['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir_ ...

  7. 洛谷 P2388 阶乘之乘 题解

    本蒟蒻又来发题解了QwQ; 看到这个题目,本蒟蒻第一眼就想写打个暴力: 嗯,坏习惯: 但是,动动脑子想一想就知道,普通的的暴力是过不了的: 但是,身为蒟蒻的我,也想不出什么高级的数学方法来优化: 好, ...

  8. 洛谷 题解 P5595 【【XR-4】歌唱比赛】

    本蒟蒻又双叒叕被爆踩了. 考试时一遍过 其实这题还是很简单的,难度不会大于普及组T1. CSP 2019 RP++ 看开始看到题目,觉得特别长,不想看... 我来和你们分析分析题目,你们就都可以秒了. ...

  9. IPV6-ONLY

    1.ipv4地址已经耗尽,未来可能只支持ipv6-only. 2.在一个纯IPV6环境下,路由器会自动将IPV4地址转成IPv6地址. 苹果这样要求,对于大多数开发者而言,并不困难.目前大多数应用无需 ...

  10. Elasticsearch logstash filter

    参照官方文档: https://www.elastic.co/guide/en/logstash/current/advanced-pipeline.html demo-pipeline.conf i ...