1. 程式人生 >實用技巧 >其它 >面試筆記-springboot

面試筆記-springboot


參考:https://blog.csdn.net/cx521600/article/details/90205359

一、Springboot定義

Spring Boot 是 Spring 開源組織下的子專案,是 Spring 元件一站式解決方案,主要是簡化了使用 Spring 的難度,簡省了繁重xml的配置,提供了各種啟動器,在執行過程中自定配置, 開發者能快速上手。

面試筆記-springboot

二、為什麼要用Springboot

1、獨立執行: Spring Boot而且內嵌了各種servlet容器,Tomcat、Jetty等,現在不再需要打成war包部署到容器中,Spring Boot只要打成一個可執行的jar包就能獨立執行,所有的依賴包都在一個jar包內。

2、簡化配置: spring-boot-starter-web啟動器自動依賴其他元件,簡少了maven的配置。

3、自動配置

4、無程式碼生成和XML配置:無需XML配置檔案就能完成所有配置工作,這一切都是藉助於條件註解完成的,這也是Spring4.x的核心功能之一

5、應用監控

三、SpringBoot的核心註解

@SpringBootApplication

@SpringBootConfiguration 組合了 @Configuration 註解,裝配所有bean事務,提供了一個spring的上下文環境

@EnableAutoConfiguration 根據應用所宣告的依賴來對Spring框架進行自動配置

@ComponentScan Spring元件掃描

四、Springboot啟動

public static void main(String[] args) {
   SpringApplication.run(DemoApplication.class, args);
}

五、SpringBoot應用類啟動器

Spring Boot 也提供了其它的啟動器專案包括,包括用於開發特定型別應用程式的典型依賴項。

spring-boot-starter-web-services - SOAP Web Services

spring-boot-starter-web - Web 和 RESTful 應用程式

spring-boot-starter-test - 單元測試和整合測試

spring-boot-starter-jdbc - 傳統的 JDBC

spring-boot-starter-hateoas - 為服務新增 HATEOAS 功能

spring-boot-starter-security - 使用 SpringSecurity 進行身份驗證和授權

spring-boot-starter-data-jpa - 帶有 Hibeernate 的 Spring Data JPA

spring-boot-starter-data-rest - 使用 Spring Data REST 公佈簡單的 REST 服務

常見面試題:

1、Runner啟動器-如何在 Spring Boot 啟動的時候執行一些特定的程式碼?

實現介面 ApplicationRunner 或者 CommandLineRunner

如果啟動的時候有多個ApplicationRunner和CommandLineRunner,想控制它們的啟動順序,可以實現org.springframework.core.Ordered介面或者使用org.springframework.core.annotation.Order註解。/2、

2、Spring Boot 有哪幾種讀取配置的方式?

properties和yml檔案

@Value("${info.address}") 修飾變數

@ConfigurationProperties(prefix = "info") 修飾類註解讀取方式

@PropertySource(value = { "config/db-config.properties" }) 這個是指定路徑用的

3、你如何理解 Spring Boot 配置載入順序?

在 Spring Boot 裡面,可以使用以下幾種方式來載入配置。

1)properties檔案;

2)YAML檔案;

3)系統環境變數;

4)命令列引數;

4、 Spring Boot的安全問題

https://mp.weixin.qq.com/s?__biz=MzI3ODcxMzQzMw==&mid=2247487049&idx=1&sn=5a2ee3aa80a288cea3c86a3f58b931e0&scene=21#wechat_redirect

  1. 在生產中使用HTTPS:HTTPS屬於加密協議傳輸,更安全。

  2. 使用Snyk檢查你的依賴關係

  3. 升級到最新版本

  4. 啟用CSRF保護

  5. 使用內容安全策略防止XSS攻擊

    @EnableWebSecurity
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure( HttpSecurity http) throws Exception{
            //XSS 攻擊
            http.headers().contentSecurityPolicy("script-src 'self' https://trustedscripts.example.com; object-src https://trustedplugins.example.com; report-uri /csp-report-endpoint/");
            //csrf 跨站點請求偽造(Cross-Site Request Forgery )
            http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
        }
    }