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 websocket15.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以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
