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. 【并发编程】ThreadLocal的兄弟InheritableThreadLocal

    本博客系列是学习并发编程过程中的记录总结.由于文章比较多,写的时间也比较散,所以我整理了个目录贴(传送门),方便查阅. 并发编程系列博客传送门 引子 public class InheritableT ...

  2. 人人学IoT 助学思维导图

    原来学IoT记录的学习笔记,学完之后,对考试和工作都有些帮助,特分享给大家 笔记分享链接 https://share.mindmanager.com/#publish/s6TqusKeSG6aflXL ...

  3. vue axios 总结篇

    1.npm --save 和 --save-dev 有什么区别 发布到线上的叫生产环境~,在本地开发的时候叫开发环境,--save就是会打包到线上去并且在线上环境能用到的,比如你npm install ...

  4. SQL Server 2019 深度解读:微软数据平台的野望

    本文为笔者在InfoQ首发的原创文章,主要利用周末时间陆续写成,也算近期用心之作.现转载回自己的公众号,请大家多多指教. 11 月 4 日,微软正式发布了其新一代数据库产品 SQL Server 20 ...

  5. 用Python爬虫轻松挣个上万块行不行?

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者: 小猿猿er 文章首发于我的技术博客:你可以在上面看到更多的Pytho ...

  6. ARTS-S centos修改hostname

    hostnamectl set-hostname newhostname 重启

  7. 让外部的开发机直接访问Kubernetes群集内的服务!

    让外部的开发机直接访问Kubernetes群集内的服务! 1.场景 容器化+K8s编排已经是现在进行时把网站的多个项目设计为云原生(Cloud Native)或老项改造为云原生可以获得诸多能力例如无云 ...

  8. Es6中箭头函数与普通函数的区别

    Es6中箭头函数与普通函数的区别? 普通function的声明在变量提升中是最高的,箭头函数没有函数提升 箭头函数没有属于自己的this,arguments 箭头函数不能作为构造函数,不能被new,没 ...

  9. 201871010119-帖佼佼《面向对象程序设计(java)》第十三周学习总结

    博客正文开头格式: 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nw ...

  10. 搭建react native所遇到的坑

    一.所遇问题 在搭建react native环境中,遇到执行react native  run-android命令出现如下问题 Could not resolve all dependencies f ...