這是我面試了很多中大廠總結出來的高頻面試題,如果對你有幫助,希望大家點點贊,你們的點贊是我創作的動力,
什么是 Spring Boot?
Spring Boot是Spring開源組織下的子專案,是Spring組件一站式解決方案,主要是簡化了使用Spring的難度,簡省了繁重的配置,提供了各種啟動器,開發者能快速上手,
SpringBoot的優點:
- 獨立運行,Spring Boot 而且內嵌了各種 servlet 容器(Tomcat、Jetty 等),Spring Boot 只要打成一個可執行的 jar 包就能獨立運行,java -jar xxx.jar,
- 簡化配置,spring-boot-starter-web 啟動器自動依賴其他組件,簡少了 maven 的配置,
- 自動裝配,Spring Boot 掃描所有jar包的META-INF/spring.factories,自動裝配類
- 應用監控,Spring Boot 提供一系列端點可以監控服務及應用,做健康檢測,
Spring Boot 的核心組態檔有哪幾個?它們的區別是什么?
核心組態檔 application 和 bootstrap,
bootstrap的優先級高于application,如果有一些固定的不能被覆寫的屬性,建議放在bootstrap,反之可以放在application,application更容易理解,
Spring Boot 的組態檔有哪幾種格式?它們有什么區別?
.properties 和 .yml,本質區別就是書寫格式不同,
- properties
server.port = 8080
- yml
server:
port: 8080
注意:yml 格式不支持 @PropertySource 注解匯入配置,
運行 Spring Boot 有哪幾種方式?
1)打包用命令或者放到容器中運行
2)用 Maven/ Gradle 插件運行
3)直接執行 main 方法運行
SpringBoot的核心注解是什么?由那些注解組成?
@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 {
}
主要組合包含了以下 3 個注解:
- @SpringBootConfiguration:組合了 @Configuration 注解,實作組態檔的功能,
- @EnableAutoConfiguration:打開自動配置的功能,也可以關閉某個自動配置的選項,如關閉資料源自動配置功能: @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }),
- @ComponentScan:Spring組件掃描,
Spring Boot 自動配置原理是什么?
Spring Boot的自動配置注解是@EnableAutoConfiguration,從上面的@Import的類可以找到下面自動加載自動配置的映射,
private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
MultiValueMap<String, String> result = cache.get(classLoader);
if (result != null) {
return result;
}
try {
Enumeration<URL> urls = (classLoader != null ?
classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
result = new LinkedMultiValueMap<>();
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
UrlResource resource = new UrlResource(url);
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
for (Map.Entry<?, ?> entry : properties.entrySet()) {
List<String> factoryClassNames = Arrays.asList(
StringUtils.commaDelimitedListToStringArray((String) entry.getValue()));
result.addAll((String) entry.getKey(), factoryClassNames);
}
}
cache.put(classLoader, result);
return result;
}
catch (IOException ex) {
throw new IllegalArgumentException("Unable to load factories from location [" +
FACTORIES_RESOURCE_LOCATION + "]", ex);
}
}
這個方法會加載類路徑及所有jar包下META-INF/spring.factories配置中映射的自動配置的類,
你如何理解 Spring Boot 中的 Starters?
Starters可以理解為啟動器,它包含了一系列可以集成到應用里面的依賴包,你可以一站式集成Spring及其他技術,而不需要到處找示例代碼和依賴包,如你想使用Spring JPA訪問資料庫,只要加入spring-boot-starter-data-jpa啟動器依賴就能使用了,
Starters包含了許多專案中需要用到的依賴,它們能快速持續的運行,都是一系列得到支持的管理傳遞性依賴,
Spring Boot官方的啟動器都是以spring-boot-starter-命名的,代表了一個特定的應用型別,
第三方的啟動器不能以spring-boot開頭命名,它們都被Spring Boot官方保留,一般一個第三方的應該這樣命名,像mybatis的mybatis-spring-boot-starter,
如何在 Spring Boot 啟動的時候運行一些特定的代碼?
如果你想在Spring Boot啟動的時候運行一些特定的代碼,你可以實作介面 ApplicationRunner或者 CommandLineRunner,這兩個介面實作方式一樣,它們都只提供了一個run方法,
如果啟動的時候有多個ApplicationRunner和CommandLineRunner,想控制它們的啟動順序,可以通過@Order注解控制
Spring Boot 有哪幾種讀取配置的方式?
- @Value
@Component
public class HttpClientParam {
@Value("${http.maxTotal}")
private Integer maxTotal;
public String getMaxTotal() {
return maxTotal;
}
public void setMaxTotal(String maxTotal) {
this.maxTotal = maxTotal;
}
}
- @ConfigurationProperties
/**
* HttpClient連接池
*
* @author hzy
*
*/
@Configuration
@ConfigurationProperties(prefix = "http")
public class HttpClientParam {
private Integer maxTotal;
private Integer defaultMaxPerRoute;
private Integer connectTimeout;
private Integer connectionRequestTimeout;
private Integer socketTimeout;
public Integer getMaxTotal() {
return maxTotal;
}
public void setMaxTotal(Integer maxTotal) {
this.maxTotal = maxTotal;
}
public Integer getDefaultMaxPerRoute() {
return defaultMaxPerRoute;
}
public void setDefaultMaxPerRoute(Integer defaultMaxPerRoute) {
this.defaultMaxPerRoute = defaultMaxPerRoute;
}
public Integer getConnectTimeout() {
return connectTimeout;
}
public void setConnectTimeout(Integer connectTimeout) {
this.connectTimeout = connectTimeout;
}
public Integer getConnectionRequestTimeout() {
return connectionRequestTimeout;
}
public void setConnectionRequestTimeout(Integer connectionRequestTimeout) {
this.connectionRequestTimeout = connectionRequestTimeout;
}
public Integer getSocketTimeout() {
return socketTimeout;
}
public void setSocketTimeout(Integer socketTimeout) {
this.socketTimeout = socketTimeout;
}
}
http:
# 最大連接數
maxTotal: 100
# 并發數
defaultMaxPerRoute: 20
connectTimeout: 10000
connectionRequestTimeout: 500
socketTimeout: 10000
- @PropertySource+@Value
@Component
@PropertySource(value = { "config/http-config.properties"})
public class HttpClientParam {
@Value("${http.maxTotal}")
private Integer maxTotal;
public String getMaxTotal() {
return maxTotal;
}
public void setMaxTotal(String maxTotal) {
this.maxTotal = maxTotal;
}
}
Spring Boot 支持哪些日志框架?推薦和默認的日志框架是哪個?
Spring Boot支持Java Util Logging,Log4j2,Lockback作為日志框架,如果你使用starters啟動器,Spring Boot將使用Logback作為默認日志框架,無論使用哪種日志框架,Spring Boot都支持配置將日志輸出到控制臺或者檔案中,
spring-boot-starter啟動器包含spring-boot-starter-logging啟動器并集成了slf4j日志抽象及Logback日志框架,
屬性配置日志
Spring Boot支持屬性配置日志引數,這個不是很靈活,不細講,
參考配置:
# LOGGING
logging.config= # Location of the logging configuration file. For instance `classpath:logback.xml` for Logback
logging.exception-conversion-word=%wEx # Conversion word used when logging exceptions.
logging.file= # Log file name. For instance `myapp.log`
logging.level.*= # Log levels severity mapping. For instance `logging.level.org.springframework=DEBUG`
logging.path= # Location of the log file. For instance `/var/log`
logging.pattern.console= # Appender pattern for output to the console. Only supported with the default logback setup.
logging.pattern.file= # Appender pattern for output to the file. Only supported with the default logback setup.
logging.pattern.level= # Appender pattern for log level (default %5p). Only supported with the default logback setup.
logging.register-shutdown-hook=false # Register a shutdown hook for the logging system when it is initialized.
如:
logging.level.root=DEBUG
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR
自定義日志檔案
根據不同的日志框架,默認加載的日志組態檔的檔案名,放在資源根目錄下,其他的目錄及檔案名不能被加載,
| Logging System | Customization |
|---|---|
| Logback | logback-spring.xml, logback-spring.groovy, logback.xml or logback.groovy |
| Log4j2 | log4j2-spring.xml or log4j2.xml |
| JDK (Java Util Logging) | logging.properties |
既然默認自帶了Logback框架,Logback也是最優秀的日志框架,往資源目錄下創建一個logback-spring.xml即可,下面是一個參考組態檔,
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
<springProperty scope="context" name="APP_NAME" source="spring.application.name"/>
<springProperty scope="context" name="APP_PORT" source="server.port"/>
<springProperty scope="context" name="DEFAULT_APP_PORT" source="spring.application.port"/>
<property name="OS_NAME" value="${os.name}"/>
<if condition='property("OS_NAME").contains("Windows")'>
<then>
<property name="LOG_PATH" value="${LOG_PATH:-E:/logs}" />
</then>
<else>
<property name="LOG_PATH" value="${LOG_PATH:-/log}" />
</else>
</if>
<property name="LOG_DIR" value="${APP_NAME:-system}" />
<property name="APP_PORT" value="${APP_PORT:-${DEFAULT_APP_PORT:-0}}" />
<if condition='!property("APP_PORT").equals("0")'>
<then>
<property name="LOG_DIR" value="${LOG_DIR}-${APP_PORT}" />
</then>
</if>
<!-- 控制臺輸出 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--格式化輸出:%d表示日期,%thread表示執行緒名,%-5level:級別從左顯示5個字符寬度,%msg:日志訊息,%n是換行符 -->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
</encoder>
</appender>
<!-- 按照每天生成日志檔案 -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--日志檔案輸出的檔案名 -->
<FileNamePattern>${LOG_PATH}/${LOG_DIR}/info.log.%d{yyyy-MM-dd}.log</FileNamePattern>
<!--日志檔案保留天數 -->
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--格式化輸出:%d表示日期,%thread表示執行緒名,%-5level:級別從左顯示5個字符寬度%msg:日志訊息,%n是換行符 -->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
</encoder>
<!--日志檔案最大的大小 -->
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
</appender>
<!-- 按照每天生成日志檔案 error級別 -->
<appender name="FILE-ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--日志檔案輸出的檔案名 -->
<FileNamePattern>${LOG_PATH}/${LOG_DIR}/error.log.%d{yyyy-MM-dd}.log</FileNamePattern>
<!--日志檔案保留天數 -->
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--格式化輸出:%d表示日期,%thread表示執行緒名,%-5level:級別從左顯示5個字符寬度%msg:日志訊息,%n是換行符 -->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
</encoder>
<!--日志檔案最大的大小 -->
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
</appender>
<!--myibatis log configure -->
<logger name="com.apache.ibatis" level="TRACE" />
<logger name="java.sql.Connection" level="DEBUG" />
<logger name="java.sql.Statement" level="DEBUG" />
<logger name="java.sql.PreparedStatement" level="DEBUG" />
<!-- 日志輸出級別 -->
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
<appender-ref ref="FILE-ERROR" />
</root>
</configuration>
強烈推薦使用logback-spring.xml作為檔案名,因為logback.xml加載太早,
日志初始化在ApplicationContext創建之前,所以@PropertySources加載的配置是讀取不到的,系統環境變數、Spring Environment及application,bootstrap組態檔中的資訊可以讀取到,
讀取系統環境屬性:
<property name = "LOG_PATH" value = "${LOG_PATH:-E:/logs}" />
讀取當前應用Environment中的屬性:
<springProperty scope="context" name="fluentHost" source="myapp.fluentd.host"
defaultValue="localhost"/>
Spring Boot也支持通過springProfile來加載不同profiles下的配置,
<springProfile name="staging">
<!-- configuration to be enabled when the "staging" profile is active -->
</springProfile>
<springProfile name="dev, staging">
<!-- configuration to be enabled when the "dev" or "staging" profiles are active -->
</springProfile>
<springProfile name="!production">
<!-- configuration to be enabled when the "production" profile is not active -->
</springProfile>
SpringBoot 實作熱部署有哪幾種方式?
主要有兩種方式:
- Spring Loaded
- Spring-boot-devtools
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/295391.html
標籤:java
上一篇:Java學習筆記(五):進制
