1. 程式人生 > 程式設計 >Spring Framework常用面試題及答案彙總

Spring Framework常用面試題及答案彙總

1.什麼是Spring Framework ?

Spring Framework 是一個提供了完整性的程式設計或配置一個現代化的基於JAVA的企業應用,各種基礎設施的支援。

參見官方(https://spring.io/projects/spring-framework#overview):

The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform.

Spring makes it easy to create Java enterprise applications. It provides everything you need to embrace the Java language in an enterprise environment,with support for Groovy and Kotlin as alternative languages on the JVM,and with the flexibility to create many kinds of architectures depending on an application's needs. As of Spring Framework 5.1,Spring requires JDK 8+ (Java SE 8+) and provides out-of-the-box support for JDK 11 LTS. Java SE 8 update 60 is suggested as the minimum patch release for Java 8,but it is generally recommended to use a recent patch release.

2.Spring Framework有哪些核心模組 ?

  • spring-context : 事件驅動,註解驅動,模組驅動等
  • spring-core : Spring基礎API模組,如資源管理、泛型處理
  • spring-beans : Spring Bean 相關,如依賴查詢、依賴注入
  • spring-aop : Spring AOP 處理,如動態代理、AOP位元組碼提升
  • spring-expression : Spring表示式語言模組

(專案使用Maven進行管理時,引入 spring-context模組後,則會傳遞依賴載入其他4個模組)

3.什麼是IOC ?

IOC是控制反轉,類似於好萊塢原則(你不要打電話給我,我會打電話給你),主要包含依賴查詢和依賴注入

4.依賴注入和依賴查詢的區別 ?

依賴查詢是主動或手動的依賴查詢方式,通常需要依賴容器或標準API實現。而依賴注入則是手動或自動依賴繫結的方式,無需依賴特定的容器和API

5.Spring作為IOC容器的優勢有哪些 ?

典型的IOC容器管理,依賴注入、依賴查詢

  • AOP抽象
  • 事物抽象
  • 事件機制
  • SPI擴充套件
  • 強大的第三方整合
  • 易測試性 等

6.Spring 中 BeanFactory和FactoryBean區別 ?

BeanFactory是IOC底層容器

FactoryBean 是建立Bean的一種方式,幫助實現複雜的初始化邏輯

7.Spring 中 BeanFactory和ObjectFactory區別 ?

ObjectFactory和BeanFactory均提供依賴查詢的能力;

ObjectFactory僅關注一個或一種型別的Bean的依賴查詢,並且自身不具備依賴查詢的能力,能力則由BeanFactory輸出;

BeanFactory則提供了單一型別、集合型別以及層次性等多種依賴查詢方式;

8.BeanFactory.getBean 操作是否執行緒安全 ?

BeanFactory.getBean 方法的執行是執行緒安全的,操作過程中會增加互斥鎖.

9.Spring有多少種依賴注入的方式 ?

  • 構造器注入
  • Setter方法注入
  • 欄位注入
  • 方法注入
  • 介面回撥注入

10.Spring偏好構造器注入還是Setter注入 ?

兩種依賴注入方式均可以使用,如果是必須依賴的話,推薦使用構造器注入,Setter注入用於可選依賴

11.Spring注入和依賴來源是否相同 ?

不相同,依賴查詢的來源僅限於Spring BeanDefinition 以及單例物件;依賴注入的來源還包括 ResolvableDependency以及@Value所標註的外部化配置

12.單例物件能在Ioc容器啟動後註冊嗎 ?

可以的,單例物件的註冊於BeanDefinition不同,BeanDefinition會被ConfigurableListableBeanFactory#freezeConfiguration()方法影響,從而凍結註冊,單例物件則沒有這個限制

13.Spring依賴注入的來源有哪些 ?

Spring BeanDefinition

單例物件

  Resolvable Dependency

@Value 外部化配置

14.Spring內建的Bean作用域有幾種 ?

  • singleton -- 預設單例 ☆
  • prototype -- 原型
  • request -- Web中使用
  • session
  • application
  • websocket

15.Spring 中 singleton Bean 是否在一個應用中是唯一的 ?

否,singleton bean 僅在當前Spring IoC 容器(Bean Factory)中是單例物件; 而BeanFactory可能存在父容器

16.Spring 中 BeanPostProcessor 的使用場景有哪些 ?

Spring 中 BeanPostProcessor 提供 Spring Bean 初始化前和初始化後的生命週期回撥;分別對應 postProcessBeforeInitialization 以及 postProcessAfterInitialization 方法,允許對關心的 Bean 進行擴充套件,甚至替換。

其中 ApplicationContext 相關的 Aware 回撥也是基於 BeanPostProcessor 實現,即 ApplicationContextAwareProcessor

17.Spring 中 BeanFactoryPostProcessor 與 BeanPostProcessor 的區別 ?

BeanFactoryPostProcessor 是 Spring BeanFactory(實際為 ConfigureableListableBeanFactory)的後置處理器,用於擴充套件 BeanFactory,或通過 BeanFactory 進行依賴查詢或依賴注入;

BeanFactoryPostProcessor 必須有 Spring ApplicationContext 執行,BeanFactory 無法與其直接互動;

BeanPostProcessor 則直接與 BeanFactory 關聯,屬於N對1的關係。

18.Spring 中 BeanFactory 是如何處理 Bean 的生命週期 ?

BeanFactory的預設實現為 DefaultListableBeanFactory,其中Bean生命週期與方法對映如下:

  • BeanDefinition 註冊階段 -- registerBeanDefinition
  • BeanDefinition 合併階段 -- getMergedBeanDefinition
  • Bean 例項化前階段 -- resolveBeforeInstantiation
  • Bean 例項化階段 -- createBeanInstance
  • Bean 例項化後階段 --populateBean
  • Bean 屬性賦值前階段 -- populateBean
  • Bean Aware 介面回撥階段 -- initializeBean
  • Bean 初始化前階段 -- initializeBean
  • Bean 初始化階段 -- initializeBean
  • Bean 初始化後階段 -- initializeBean
  • Bean 初始化完成階段 -- preInstantiateSingletons
  • Bean 銷燬前階段 -- destroyBean
  • Bean 銷燬階段 -- destroyBean

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。