javascript
100份Spring面试问答-最终名单(PDF下载)
上次更新時間:2019年2月11日
這是有關Spring框架的一些最重要問題的摘要,在面試或面試測試過程中可能會要求您回答這些問題! 您無需擔心下一次面試的機會,因為Java Code Geeks在這里為您服務!
您可能會被問到的大多數事情都收集在下面的列表中。 簡短地介紹了所有核心模塊,從基本的Spring功能(例如Spring Beans)到Spring MVC框架。 在檢查了面試問題之后,您應該查看我們的Spring Tutorials頁面。
所以,走吧...!
目錄
A.Spring概述 B.依賴注入 C.Spring Bean D.Spring注釋 E.Spring數據訪問 F.Spring面向方面的編程(AOP) G.Spring模型視圖控制器(MVC) H.認證與授權A.Spring概述
1.什么是春天?
Spring是企業Java的開源開發框架。 Spring Framework的核心功能可用于開發任何Java應用程序,但是在Java EE平臺上存在一些擴展,可用于構建Web應用程序。 Spring框架旨在通過啟用基于POJO的編程模型來使Java EE開發更易于使用并促進良好的編程習慣。
2. Spring Framework有什么好處?
- 輕量級:在尺寸和透明度方面,Spring是輕量級的。 spring框架的基本版本約為2MB。
- 控制反轉(IOC):使用控制反轉技術在Spring中實現松耦合。 對象提供其依賴關系,而不是創建或尋找依賴對象。
- 面向方面(AOP): Spring支持面向方面的編程 ,并將應用程序業務邏輯與系統服務分開。
- 容器: Spring包含并管理應用程序對象的生命周期和配置。
- MVC框架: Spring的Web框架是一個經過精心設計的Web MVC框架 ,它提供了Web框架的替代方案。
- 事務管理: Spring提供了一個一致的事務管理接口,該接口可以縮小到本地事務,也可以擴大到全局事務(JTA)。
- 異常處理: Spring提供了一個方便的API,可將技術特定的異常(由JDBC,Hibernate或JDO拋出)轉換為一致的未經檢查的異常。
3. Spring框架模塊是哪些?
大約有20個模塊,它們被概括為核心容器,數據訪問/集成,Web,AOP(面向方面??的編程),儀器和測試。 Spring框架的基本模塊是:
彈簧芯容器
該層基本上是Spring框架的核心。 它包含以下模塊:
- 核心模塊
- Bean模塊
- 上下文模塊
- 表達語言模塊
數據訪問/集成
該層提供了與數據庫交互的支持。 它包含以下模塊:
- JDBC模塊
- 對象關系映射(ORM)模塊
- Java Messaging Service(JMS)模塊
- 對象XML映射器(OXM)模塊
- 交易管理模塊
網頁
該層提供了創建Web應用程序的支持。 它包含以下模塊:
- 網絡模塊
- Web-MVC模塊
- Web套接字模塊
- Web-Portlet模塊
面向方面的編程( AOP )
在此層中,可以使用建議,切入點等來分離代碼。
工具-該層為類工具和類加載器實現提供支持。
測試
該層為使用JUnit和TestNG的測試提供支持。
訊息傳遞
該模塊提供對STOMP的支持。 它還支持注釋編程模型,該模型用于路由和處理來自WebSocket客戶端的STOMP消息。
方面
該模塊提供了與AspectJ集成的支持。
4.解釋核心容器(應用程序上下文)模塊
這是基本的Spring模塊,提供了Spring框架的基本功能。 BeanFactory是任何基于Spring的應用程序的心臟。 Spring框架建立在該模塊的頂部,該模塊構成了Spring容器。
5. BeanFactory實現示例
BeanFactory是工廠模式的實現,該模式應用控制反轉將應用程序的配置和依賴項與實際應用程序代碼分開。
最常用的BeanFactory實現是XmlBeanFactory類。
6. XMLBeanFactory
最有用的是org.springframework.beans.factory.xml.XmlBeanFactory ,它根據XML文件中包含的定義加載其bean。 該容器從XML文件讀取配置元數據,并使用它創建完全配置的系統或應用程序。
7.解釋AOP模塊
AOP模塊用于為支持Spring的應用程序開發方面。 AOP聯盟已經提供了很多支持,以確保Spring和其他AOP框架之間的互操作性。 該模塊還向Spring引入了元數據編程。
8.解釋JDBC抽象和DAO模塊
使用JDBC抽象和DAO模塊,我們可以確保我們保持數據庫代碼簡潔明了,并防止由于關閉數據庫資源失敗而導致的問題。 它在多個數據庫服務器給出的錯誤消息之上提供了一層有意義的異常。 它還利用Spring的AOP模塊為Spring應用程序中的對象提供事務管理服務。
9.解釋對象/關系映射集成模塊
通過提供ORM模塊,Spring還支持在純JDBC上使用對象/關系映射(ORM)工具。 Spring提供了與多種流行的ORM框架結合的支持,包括Hibernate,JDO和iBATIS SQL Maps。 Spring的事務管理支持所有這些ORM框架以及JDBC。
10.解釋Web模塊
Spring Web模塊建立在應用程序上下文模塊的基礎上,提供適用于基于Web的應用程序的上下文。 該模塊還包含對一些面向Web的任務的支持,例如透明地處理文件上傳的多部分請求以及將請求參數以編程方式綁定到業務對象。 它還包含與Jakarta Struts的集成支持。
11.解釋Spring MVC模塊
Spring提供了MVC框架來構建Web應用程序。 Spring可以輕松地與其他MVC框架集成,但是Spring的MVC框架是一個更好的選擇,因為它使用IoC來提供控制器邏輯與業務對象的清晰分離。 使用Spring MVC,您可以聲明式地將請求參數綁定到業務對象。
12. Spring配置文件
Spring配置文件是一個XML文件。 該文件包含類信息,并描述如何配置這些類以及如何將它們相互引入。
13.我們如何擁有多個Spring配置文件?
- web.xml contextConfigLocation:您可以通過ContextConfigLocation元素將它們全部加載到Web應用程序上下文中。 假設您正在編寫Web應用程序,那么您已經在這里擁有了主要的applicationContext。 您需要做的就是在下一個上下文的聲明之間放置一些空格。
- applicationContext.xml導入資源:您可以將主applicationContext.xml添加到web.xml,然后在該主上下文中使用import語句。
14. ApplicationContext的常見實現是什么?
- FileSystemXmlApplicationContext容器從XML文件加載Bean的定義。 必須將XML bean配置文件的完整路徑提供給構造函數。
- ClassPathXmlApplicationContext容器還從XML文件加載Bean的定義。 在這里,您需要正確設置CLASSPATH ,因為此容器將在CLASSPATH查找bean配置XML文件。
- WebXmlApplicationContext:容器使用Web應用程序中的所有bean定義加載XML文件。
15. Bean Factory和ApplicationContext有什么區別?
應用程序上下文提供了一種解決文本消息的方法,一種加載文件資源(例如圖像)的通用方法,它們可以將事件發布到注冊為偵聽器的bean。 另外,必須在應用程序上下文中聲明性地處理對容器或容器中的Bean的操作,這些操作必須由Bean工廠以編程方式進行處理。 應用程序上下文實現MessageSource ,這是一個用于獲取本地化消息的接口,實際實現是可插入的。
16. Spring框架有哪些最佳實踐?
Spring框架的一些最佳實踐是:
- 定義名稱與類或接口名稱相同的單例bean
- 將Spring bean配置文件放在文件夾而不是根文件夾下
- 為Spring bean配置文件提供通用的前綴或后綴
- 盡量避免在Spring XML配置文件中使用導入元素
- 避免基于XML的Bean配置中的自動接線
- 始終使用屬性占位符將bean屬性值外部化
- 導入名稱空間定義時選擇默認的無版本XSD
- 始終在資源路徑中放置類路徑前綴
- 即使使用現場級自動接線,也要創建設置器方法
- 即使服務方法幾乎沒有將其職責委托給相應的DAO方法,也要創建一個單獨的服務層
17.使用Spring Framework的方式有哪些?
您可以使用Spring Framework:
- 用于編寫Web應用程序
- 公開RESTful服務
- 保護您的Web應用程序
- 用于與數據庫通信
- 用于處理長期運行的工作
- 處理您必須使用的外部資源或系統
- 用于測試目的
- 用于獨立的Java項目
- 將您的應用程序轉換為可執行文件
- 將社交媒體集成到您的應用程序中
18.我們如何使用Spring創建返回JSON響應的Restful Web Service?
只要Jackson2在類路徑中,Spring Boot應用程序中的任何Spring @RestController默認情況下都應呈現JSON響應。
19. Spring vs Spring MVC vs Spring Boot?
- Spring:Spring的最重要特征是依賴注入或控制反轉。
- Spring MVC:是一個完整的面向HTTP的MVC框架,由Spring框架管理,基于Servlet。 它相當于JavaEE堆棧中的JSF。
- Spring Boot:一個實用程序,用于快速設置應用程序,提供開箱即用的配置以構建Spring支持的應用程序。
20. Spring應用程序是什么樣的?
- 接口:定義功能的接口。
- Bean類:它包含屬性,其setter和getter方法,函數等。
- Spring AOP:提供跨領域關注點的功能。
- 配置XML文件:包含類的信息以及如何配置它們。
- 客戶端程序:使用該功能。
B.依賴注入
21.什么是Spring IoC容器?
Spring IoC負責創建對象,通過依賴項注入(DI)管理它們,將它們連接在一起,對其進行配置以及管理其完整生命周期。
22.國際奧委會有什么好處?
- IOC或依賴項注入可最大程度地減少應用程序中的代碼量。
- 由于在單元測試中不需要單例或JNDI查找機制,因此它使測試應用程序變得容易。
- 以最小的努力和最少的干擾機制促進了松耦合。
- IOC容器支持急切的實例化和服務的延遲加載。
23.Spring有多少種IOC容器?
- BeanFactory:本質上,BeanFactory只是高級工廠的接口,該工廠能夠維護不同Bean及其依賴項的注冊表。 BeanFactory使您可以讀取Bean定義并使用Bean工廠訪問它們。
- ApplicationContext:ApplicationContext是Spring應用程序中的中央接口,用于向應用程序提供配置信息。 它在運行時是只讀的,但是可以在必要時重新加載并得到應用程序的支持。 許多類實現ApplicationContext接口,從而允許使用各種配置選項和應用程序類型。
24. BeanFactory與ApplicationContext
應用環境 :
- Bean實例化/接線
- 自動BeanPostProcessor注冊
- 自動BeanFactoryPostProcessor注冊
- 方便的MessageSource訪問(適用于i18n)
- ApplicationEvent發布
BeanFactor :
- Bean實例化/接線
25. Spring中的依賴注入是什么?
依賴注入是控制反轉(IoC)的一個方面,是一個通用概念,可以用許多不同的方式表示。此概念表示您不創建對象而是描述了如何創建對象。 您不會直接用代碼將組件和服務連接在一起,而是描述配置文件中的哪些組件需要哪些服務。 然后,一個容器(IOC容器)負責將其全部掛接。
26.緊耦合和松耦合有什么區別?
緊耦合 :
- 緊密耦合是一組類彼此高度依賴時。
松耦合 :
- 松耦合是通過促進單一責任和關注點分離的設計實現的。
27. IoC(依賴注入)有哪些不同類型?
- 基于構造函數的依賴項注入:當容器調用帶有多個參數的類構造函數時,將完成基于構造函數的DI,每個參數代表對其他類的依賴關系。
- 基于setter的依賴項注入:基于setter的DI是通過在調用無參數構造函數或無參數靜態工廠方法實例化bean之后,在bean上調用setter方法來完成的。
28.您會建議哪個DI基于構造函數或Setter的DI?
您可以同時使用基于構造函數和基于Setter的依賴注入。 最好的解決方案是將構造函數參數用于強制性依賴項,將setter用于可選的依賴性。
C.Spring Bean
29.什么是Spring Bean?
Spring Bean是構成Spring應用程序主干的Java對象。 它們由Spring IoC容器實例化,組裝和管理。 這些bean是使用提供給容器的配置元數據創建的,例如以XML <bean/>定義的形式。
Spring框架中定義的Bean是單例Bean。 如果指定為true,則bean標記中有一個名為"singleton"的屬性,則bean成為單例;如果設置為false,則bean成為原型bean。 默認情況下,它設置為true。 因此,默認情況下,spring框架中的所有bean都是單例bean。
30. Spring Bean定義包含什么?
Spring Bean定義包含容器了解如何創建Bean所需的所有配置元數據,其生命周期詳細信息及其依賴項。
31.如何為Spring容器提供配置元數據?
有三種重要的方法可以為Spring容器提供配置元數據:
- 基于XML的配置文件。
- 基于注釋的配置
- 基于Java的配置
32.您如何定義bean的范圍?
在Spring中定義<bean>時,我們還可以聲明bean的作用域。 可以通過bean定義中的scope屬性定義。 例如,當Spring每次需要一個新的bean實例時,該bean的scope屬性為prototype 。 另一方面,當每次每次都必須由Spring返回bean的相同實例時,必須將bean scope屬性設置為singleton 。
33.解釋Spring支持的bean作用域
Spring框架提供了五個作用域,支持以下五個作用域:
- 在單例范圍中,Spring將每個Spring IoC容器的bean定義范圍限制為一個實例。
- 在原型范圍內,單個bean定義可以具有任意數量的對象實例。
- 在請求范圍內,將bean定義為HTTP請求。 該范圍僅在可感知網絡的Spring ApplicationContext中有效。
- 在會話范圍中,bean定義的范圍是HTTP會話。 此范圍也僅在可感知網絡的Spring ApplicationContext中有效。
- 在全局會話作用域中,bean定義的作用域是全局HTTP會話。 在Web感知的Spring ApplicationContext中也是這種情況。
Spring Bean的默認范圍是Singleton 。
34. Singleton bean在Spring Framework中線程安全嗎?
不,在Spring框架中,單例bean不是線程安全的。
35.在Spring框架中解釋Bean的生命周期
- spring容器從XML文件中找到bean的定義并實例化bean。
- Spring填充bean定義(DI)中指定的所有屬性。
- 如果bean實現BeanNameAware接口,則spring將bean的id傳遞給setBeanName()方法。
- 如果Bean實現BeanFactoryAware接口,則spring將beanfactory傳遞給setBeanFactory()方法。
- 如果有任何與Bean相關聯的BeanPostProcessors ,Spring會調用postProcesserBeforeInitialization()方法。
- 如果bean實現IntializingBean ,其afterPropertySet()方法被調用。 如果bean具有init方法聲明,則調用指定的初始化方法。
- 如果有任何與Bean關聯的BeanPostProcessor,則將調用其postProcessAfterInitialization()方法。
- 如果Bean實現DisposableBean ,它將調用destroy()方法。
您可以覆蓋它們嗎?
bean有兩種重要的生命周期方法。 第一個是setup ,當將Bean裝入容器時調用。 第二種方法是teardown方法,當從容器中卸載bean時會調用該方法。
bean標簽具有兩個重要的屬性( init-method和destroy-method ),您可以使用它們定義自己的自定義初始化和destroy方法。 還有相應的注釋( @PostConstruct和@PreDestroy )。
37.Spring的內在豆是什么?
當一個bean僅用作另一個bean的屬性時,可以將其聲明為內部bean。 Spring的基于XML的配置元數據提供了在bean定義的<property/>或<constructor-arg/>元素內使用<bean/>元素的功能,以便定義所謂的內部bean。 內部bean始終是匿名的,并且始終以原型為范圍。
38.如何在Spring中注入Java Collection?
Spring提供以下類型的集合配置元素 :
- 在允許重復的情況下, <list>類型用于注入值列表。
- <set>類型用于連接一組值,但不能重復。
- <map>類型用于注入名稱-值對的集合,其中名稱和值可以是任何類型。
- <props>類型可用于注入名稱-值對的集合,其中名稱和值均為字符串。
39.什么是豆類接線?
在Spring容器中將bean組合在一起時,就是進行接線,否則就進行bean接線。 在對bean進行接線時,Spring容器需要知道需要哪些bean,以及如何使用依賴注入將它們綁定在一起。
40.什么是bean自動裝配?
Spring容器能夠自動裝配協作bean之間的關系 。 這意味著可以通過檢查BeanFactory的內容而無需使用<constructor-arg>和<property>元素來自動讓Spring通過bean解析協作者(其他bean)。
41.解釋自動接線的不同模式嗎?
自動裝配功能具有五種模式,可用于指示Spring容器使用自動裝配進行依賴項注入:
- 否:這是默認設置。 顯式bean參考應該用于接線。
- byName:自動byName ,Spring容器查看XML配置文件byName autowire屬性設置為byName autowire屬性。 然后,它嘗試將其屬性與配置文件中由相同名稱定義的bean進行匹配和關聯。
- byType:按datatype自動裝配時,Spring容器會查看XML配置文件中將autowire屬性設置為byType autowire屬性。 然后,如果屬性的類型與配置文件中的bean名稱之一完全匹配,它將嘗試匹配并連接屬性。 如果存在多個這樣的bean,則會引發致命異常。
- 構造函數:此模式類似于byType ,但類型適用于構造函數參數。 如果容器中不存在構造函數參數類型的一個bean,則將引發致命錯誤。
- autodetect: Spring首先嘗試通過構造函數使用autowire進行連線,如果它不起作用,Spring嘗試通過byType進行自動byType 。
42.自動裝配是否有限制?
自動裝配的局限性是:
- 覆蓋:您仍然可以使用<constructor-arg>和<property>設置指定依賴項,這些設置將始終覆蓋自動裝配。
- 原始數據類型:您不能自動連接簡單屬性,例如原始數據,字符串和類。
- 令人困惑的性質:自動裝配不如顯式接線精確,因此,如果可能的話,更喜歡使用顯式接線。
43.可以在Spring中注入空字符串值嗎?
是的你可以。
D.Spring注釋
44.什么是重要的Spring注釋?
我在項目中使用的一些Spring注釋是:
- @Component用于指示類是組件。 這些類用于自動檢測,并在使用基于注釋的配置時配置為Bean。
- @Controller是一種特定類型的組件,在MVC應用程序中使用,并且通常與@RequestMapping批注一起使用。
- @Repository批注用于指示組件用作存儲庫,以及用于存儲/檢索/搜索數據的機制。 我們可以將此注釋與DAO模式實現類一起應用。
- @Service用于指示類是服務。 通常,提供一些服務的Business Facade類都帶有此注釋。
- @Required –此注釋僅指示必須在配置時通過bean定義中的顯式屬性值或通過自動裝配來填充受影響的bean屬性。 如果尚未填充受影響的bean屬性,則容器將引發BeanInitializationException。
- @ResponseBody –用于發送對象作為響應,通常用于發送XML或JSON數據作為響應。
- @PathVariable –用于將動態值從URI映射到處理程序方法參數。
- @Autowired –提供更細粒度的控制,以實現自動布線的位置和方式。 它可以用于在setter方法上自動裝配bean,就像@Required批注,在構造函數上,在屬性或具有任意名稱和/或多個參數的pn方法上一樣。
- @Qualifier –如果有多個相同類型的bean,并且只需要一個屬性進行連接,則@Qualifier注釋與@Autowired注釋一起使用,以通過指定要連接的確切bean來消除混淆。
- @Scope –用于配置Spring bean的范圍。
- @Configuration –指示Spring IoC容器可以將該類用作bean定義的源。
- @ComponentScan –應用此注釋時,將掃描軟件包下所有可用的類。
- @Bean –對于基于Java的配置,告訴spring用@Bean注釋的方法將返回一個對象,該對象應在spring應用程序上下文中注冊為bean。
- 用于配置方面和建議的AspectJ批注,@ Aspect,@ Before,@ After,@ Around,@ Pointcut等。
45. @RequestParam批注做什么?
spring中的@RequestParam批注將查詢字符串的參數值綁定到控制器的method參數。
46. @Primary批注的重要性是什么
當存在多個具有相同數據類型的bean時,開發人員將使用特定于Spring的@Primary批注 ,該批注自動為特定bean賦予更高的優先級。 此注釋可用于直接或間接使用@Component注釋進行注釋的任何類,或用于使用@Bean注釋進行注釋的方法。
47.配置類型XML和注釋之間有什么區別?
注釋的優點 :
- 所有信息都在一個文件中
- 當類更改時,無需修改xml文件
XML文件的優點 :
- POJO與行為之間的清晰區分
- 當您不知道哪個POJO負責該行為時,更容易找到該POJO
48. @SpringBootApplication的作用是什么?
@SpringBootApplication注釋是在Spring Boot 1.2.0中引入的,它啟用了自動配置功能。
該注釋封裝了三種不同注釋的工作:
- @Configuration :允許開發人員顯式注冊Bean
- @ComponentScan :啟用組件掃描,以便在Spring的應用程序上下文中自動發現控制器類和其他組件并將其注冊為Bean
- @EnableAutoConfiguration :啟用Spring Boot的自動配置功能
該注釋采用以下可選參數:
- exclude :從自動配置中排除類列表
- excludeNames :從自動配置中排除標準類名的列表
- scanBasePackage :提供必須應用于掃描的軟件包列表
- scanBasePackageClasses :提供其他包中必須用于掃描的類的列表
49.解釋@InitBinder?
該注釋以聲明日期格式的方法修飾,并且在整個類中,都使用定義的日期格式。 每當使用日期字段@InitBinder進行綁定時; 批注說使用CustomDateEditor,而CustomDateEditor使用@InitBinder中提到的日期格式。
50.定義@ControllerAdvice?
帶有@ControllerAdvice的類可以顯式聲明為Spring Bean,或通過類路徑掃描自動檢測。 所有此類bean均通過AnnotationAwareOrderComparator進行排序,即基于@Order和Ordered,并在運行時以該順序應用。 為了處理異常,將在第一個建議中使用匹配的異常處理程序方法選擇@ExceptionHandler。 對于模型屬性和InitBinder初始化,@ ModelAttribute和@InitBinder方法也將遵循@ControllerAdvice順序。
51.我們可以發送一個對象作為控制器處理程序方法的響應嗎?
是的,我們可以使用@ResponseBody批注在靜態 Web服務中發送基于JSON或XML的響應。
52.解釋@ModelAttribute?
@ModelAttribute批注引用Model對象的屬性,用于準備模型數據。 該注釋將方法變量或模型對象綁定到命名的模型屬性。 批注接受一個可選值,該值指示模型屬性的名稱。 @ModelAttribute批注可以在參數級別或方法級別使用。 在參數級別使用此注釋是為了接受請求表單的值,而在方法級別使用的是將默認值分配給模型。 讓我借助一些示例進一步解釋您。
53. @RequestMapping批注
@RequestMapping批注用于將Web請求映射到處理程序類(即Controller)或處理程序方法上,并且可以在方法級別或類級別使用。 如果開發人員在類級別使用@RequestMapping批注,它將用作方法級別路徑的相對路徑。
給出一些注釋示例。
基于Java的配置選項使您無需XML即可編寫大多數Spring配置,而借助少量基于Java的注釋。
一個示例是@Configuration批注,該批注指示該類可以由Spring IoC容器用作Bean定義的源。 另一個示例是@Bean注釋的方法,該方法將返回一個對象,該對象應在Spring應用程序上下文中注冊為Bean。
55.什么是基于注釋的容器配置?
基于注釋的配置提供了XML設置的替代方法,該配置依賴字節碼元數據來連接組件,而不是尖括號聲明。 通過使用相關類,方法或字段聲明上的注釋,開發人員無需使用XML來描述bean的連接,而是將配置移入組件類本身。
56.如何打開注釋布線?
默認情況下,Spring容器中的注釋接線未打開。 為了使用基于注釋的接線,我們必須通過配置<context:annotation-config/>元素在Spring配置文件中啟用它。
E.Spring數據訪問
57. spring JDBC API中存在哪些類?
Spring框架為Jdbc數據庫訪問提供了以下方法:
- Jdbc模板
- SimpleJdbcTemplate
- NamedParameterJdbcTemplate
- SimpleJdbcInsert
- SimpleJdbcCall
58.如何在Spring框架中更有效地使用JDBC?
使用Spring JDBC框架時,減輕了資源管理和錯誤處理的負擔。 因此,開發人員只需編寫語句和查詢即可將數據往返數據庫。 借助于Spring框架提供的模板類JdbcTemplate ( 此處為示例),可以更有效地使用JDBC。
59. JdbcTemplate
JdbcTemplate類提供了許多方便的方法來執行操作,例如將數據庫數據轉換為原語或對象,執行準備好的和可調用的語句以及提供自定義數據庫錯誤處理。
60.如何通過Spring JdbcTemplate獲取記錄?
有兩個接口可用于從數據庫中獲取記錄:
- ResultSetExtractor
- 行映射器
61. NamedParameterJdbcTemplate的優點是什么?
NamedParameterJdbcTemplate建立在spring提供的JDBCTemplate之上,用于與數據庫的低層通信。 它使得可以將SQL查詢參數作為鍵值對傳遞。 結果,程序代碼比索引或“?”更具可讀性,因此可以作為更好的文檔。 占位符方法。 如果參數數量巨大,則后者尤其難以遵循。
62.什么是Spring JDBCTemplate類以及如何使用它?
JdbcTemplate類執行SQL查詢,更新語句和存儲過程調用,對ResultSet執行迭代并提取返回的參數值。 它處理資源的創建和釋放,從而避免出現諸如忘記關閉連接之類的錯誤。 它還捕獲JDBC異常,并將其轉換為org.springframework.dao包中定義的通用,信息量更大的異常層次結構。
63. JDBC和Spring JDBC有什么區別?
Spring JDBC頂層提供的Spring JDBC增值
64. Spring DAO支持
The Data Access Object (DAO) support in Spring is aimed at making it easy to work with data access technologies like JDBC, Hibernate or JDO in a consistent way. This allows us to switch between the persistence technologies fairly easily and to code without worrying about catching exceptions that are specific to each technology.
65. What are the ways to access Hibernate by using Spring?
There are two ways to access Hibernate with Spring:
- Inversion of Control with a Hibernate Template and Callback.
- Extending HibernateDAOSupport and Applying an AOP Interceptor node.
66. ORM's Spring support
Spring supports the following ORM's:
- 冬眠
- iBatis
- JPA (Java Persistence API)
- TopLink
- JDO (Java Data Objects)
- OJB
67. How can we integrate Spring and Hibernate using HibernateDaoSupport?
Use Spring's SessionFactory called LocalSessionFactory . The integration process is of 3 steps:
- Configure the Hibernate SessionFactory
- Extend a DAO Implementation from HibernateDaoSupport
- Wire in Transaction Support with AOP
68. Types of the transaction management Spring support
Spring supports two types of transaction management:
- Programmatic transaction management: This means that you have managed the transaction with the help of programming. That gives you extreme flexibility, but it is difficult to maintain.
- Declarative transaction management: This means you separate transaction management from the business code . You only use annotations or XML based configuration to manage the transactions.
69. What are the benefits of the Spring Framework's transaction management?
- It provides a consistent programming model across different transaction APIs such as JTA, JDBC, Hibernate, JPA, and JDO.
- It provides a simpler API for programmatic transaction management than a number of complex transaction APIs such as JTA.
- It supports declarative transaction management.
- It integrates very well with Spring's various data access abstractions.
70. Which Transaction management type is more preferable?
Most users of the Spring Framework choose declarative transaction management because it is the option with the least impact on application code, and hence is most consistent with the ideals of a non-invasive lightweight container. Declarative transaction management is preferable over programmatic transaction management though it is less flexible than programmatic transaction management, which allows you to control transactions through your code.
F.Spring Aspect Oriented Programming (AOP)
71. Explain AOP
Aspect-oriented programming, or AOP, is a programming technique that allows programmers to modularize crosscutting concerns, or behavior that cuts across the typical divisions of responsibility, such as logging and transaction management.
72. What are the advantages of spring AOP?
一個。 It is non-invasive
- Your service/domain classes get advised by the aspects (cross cutting concerns) without adding any Spring AOP related classes or interfaces into the service/domain classes.
- Allows the developer to concentrate on the business code, instead the cross cutting concerns.
b。 Its implemented in pure Java
- No need for a special compilation unit, or a special class loader
C。 It uses Spring's IOC for dependency injection
- Aspects can be configured as normal spring beans.
d。 As any other AOP framework, it weaves cross cutting concerns into the classes, without making a call to the cross cutting concerns from those classes.
e。 Centralize or modularize the cross cutting concerns
- Easy to maintain and make changes to the aspects
- Changes need to be made in one place.
- In one of your classes you don't want to have logging, it can easily be achieved by modifying the point cut in the respective aspect (logging aspect). So you need to make changes in only one place.
F。 Provision to create aspects using schema based (XML configuration) or @AspectJ annotation based style.
G。 Easy to configure
73. What are the AOP implementation?
AOP implementations:
- Spring AOP :
- Runtime weaving through proxy is done
- It supports only method level PointCut
- It is DTD based
- Apache AspectJ
- Compile time weaving through AspectJ Java tools is done
- It suports field level Pointcuts
- It is schema based and Annotation configuration
- JBoss AOP
- JBoss AOP is not only a framework, but also a prepackaged set of aspects that are applied via annotations, pointcut expressions, or dynamically at runtime. Some of these include caching, asynchronous communication, transactions, security, remoting, and many many more.
74. What are the AOP terminology?
75. Aspect
The core construct of AOP is the aspect, which encapsulates behaviors affecting multiple classes into reusable modules. It ia a module which has a set of APIs providing cross-cutting requirements. For example, a logging module would be called AOP aspect for logging. An application can have any number of aspects depending on the requirement. In Spring AOP, aspects are implemented using regular classes annotated with the @Aspect annotation ( @AspectJ style).
76. Join point
The join point represents a point in an application where we can plug-in an AOP aspect. It is the actual place in the application where an action will be taken using Spring AOP framework.
77. Advice
The advice is the actual action that will be taken either before or after the method execution. This is actual piece of code that is invoked during the program execution by the Spring AOP framework.
Spring aspects can work with five kinds of advice:
- before: Run advice before the a method execution.
- after: Run advice after the a method execution regardless of its outcome.
- after-returning: Run advice after the a method execution only if method completes successfully.
- after-throwing: Run advice after the a method execution only if method exits by throwing an exception.
- around: Run advice before and after the advised method is invoked.
78. Pointcut
The pointcut is a set of one or more joinpoints where an advice should be executed. You can specify pointcuts using expressions or patterns.
79. What is Introduction?
An Introduction allows us to add new methods or attributes to existing classes.
80. What is Target object?
The target object is an object being advised by one or more aspects. It will always be a proxy object. It is also referred to as the advised object.
81. What is a Proxy?
A proxy is an object that is created after applying advice to a target object. When you think of client objects the target object and the proxy object are the same.
82. What are the different types of AutoProxying?
- BeanNameAutoProxyCreator
- DefaultAdvisorAutoProxyCreator
- Metadata autoproxying
83. What is Weaving? What are the different points where weaving can be applied?
Weaving is the process of linking aspects with other application types or objects to create an advised object.
Weaving can be done at compile time, at load time, or at runtime.
84. What is the difference between concern and cross-cutting concern in Spring AOP
The Concern is behavior we want to have in a module of an application. A Concern may be defined as a functionality we want to implement.
The cross-cutting concern is a concern which is applicable throughout the application and it affects the entire application. For example, logging, security and data transfer are the concerns which are needed in almost every module of an application, hence they are cross-cutting concerns.
85. Explain XML Schema-based aspect implementation?
In this implementation case, aspects are implemented using regular classes along with XML based configuration.
86. Explain annotation-based (@AspectJ based) aspect implementation
This implementation case ( @AspectJ based implementation) refers to a style of declaring aspects as regular Java classes annotated with Java 5 annotations.
G.Spring Model View Controller (MVC)
87. What is Spring MVC framework?
Spring comes with a full-featured MVC framework for building web applications . Although Spring can easily be integrated with other MVC frameworks, such as Struts, Spring's MVC framework uses IoC to provide a clean separation of controller logic from business objects. It also allows to declaratively bind request parameters to business objects.
88. What are the minimum configurations needed to create Spring MVC application?
For creating a simple Spring MVC application, we would need to do the following tasks:
- Add spring-context and spring-webmvc dependencies in the project.
Configure DispatcherServlet in the web.xml file to handle requests through spring container. - Spring bean configuration file to define beans, if using annotations then it has to be configured here. Also we need to configure view resolver for view pages.
- Controller class with request mappings defined to handle the client requests.
89. List out all the concepts that are available in the MVC Architecture?
- The browser sends a request to DispatcherServlet
- DispatcherServlet knows the HanderMapping and can find the appropriate controllers
- Controllers execute the request and put the data in the model and return back the view name to the DispatcherServlet.
- DispatcherServlet uses the view name and ViewResolver to map to the view.
90. DispatcherServlet
The Spring Web MVC framework is designed around a DispatcherServlet that handles all the HTTP requests and responses.
91. WebApplicationContext
The WebApplicationContext is an extension of the plain ApplicationContext that has some extra features necessary for web applications. It differs from a normal ApplicationContext in that it is capable of resolving themes, and that it knows which servlet it is associated with.
92. What is Controller in Spring MVC framework?
Controllers provide access to the application behavior that you typically define through a service interface. Controllers interpret user input and transform it into a model that is represented to the user by the view. Spring implements a controller in a very abstract way, which enables you to create a wide variety of controllers.
93. How would you relate Spring MVC Framework to MVC architecture?
Spring MVC framework :
The Spring Framework is an open source application framework and inversion of control container for the Java platform.
MVC architecture :
Model View Controller (MVC) as it is popularly called, is a software design pattern for developing web applications
94. What is ViewResolver in Spring MVC?
Spring provides ViewResolver , which enable you to render models in a browser without tying you to a specific view technology. Out of the box, Spring enables you to use JSPs, Velocity templates and XSLT views, for example. The two interfaces which are important to the way Spring handles views are ViewResolver and View. The ViewResolver provides a mapping between view names and actual views. The View interface addresses the preparation of the request and hands the request over to one of the view technologies.
95. What is a MultipartResolver and when its used?
Spring MVC provide multipart support with MultipartResolver . The MultipartResolver parses inbound multipart requests. You can enable multipart support by registering a MultipartResolver bean in the DispatcherServlet application context.
96. How to upload file in Spring MVC Application?
Spring provides built-in support for uploading files through MultipartResolver interface implementations. There is also a validator for the field, which will be used to check if the file uploaded is of size greater than zero. There is finally a simple view that contains a form with the option to upload a file.
97. How to validate form data in Spring Web MVC Framework?
There are 3 different ways to perform validation : using annotation, manually, or a mix of both.
98. What is Spring MVC Interceptor and how to use it?
Spring's handler mapping mechanism includes handler interceptors , which are useful when you want to apply specific functionality to certain requests, for example, checking for a principal. Interceptors must implement HandlerInterceptor from the org.springframework.web.servlet package. This interface defines three methods:
- preHandle is called before the actual handler is executed.
- postHandle is called after the handler is executed.
- afterCompletion is called after the complete request has finished .
H.Authentication and authorization
99. What is Spring Security?
Spring security is one of the most important modules of the Spring framework. It enables the developers to integrate the security features easily and in a managed way. In the following example, we will show how to implement Spring Security in a Spring MVC application.
100. Why Spring Boot?
Here are some useful benefits of using Spring Boot :
- Automatic configuration of an application uses intelligent defaults based on the classpath and the application context, but they can be overridden to suit the developer's requirements as needed.
- When creating a Spring Boot Starter project, you select the features that your application needs and Spring Boot will manage the dependencies for you.
- A Spring Boot application can be packaged as a JAR file. The application can be run as a standalone Java application from the command line using the java -jar command.
- When developing a web application, Spring Boot configures an embedded Tomcat server so that it can be run as a standalone application. (Tomcat is the default, but you can configure Jetty or Undertow instead.) You can package the application as a WAR file and deploy it to an external servlet container if you prefer
- Spring Boot includes many useful non-functional features (such as security and health checks) right out of the box.
好的,現在您可以開始面試了! Don't forget to check our dedicated page full of Spring Tutorials , and our Examples dedicated subsection !
如果您喜歡此功能,請訂閱我們的時事通訊,以享受每周更新和免費白皮書! 另外,請查看我們的課程以獲得更高級的培訓!
歡迎您提出您的意見,我們會將其納入本文!
翻譯自: https://www.javacodegeeks.com/2014/05/spring-interview-questions-and-answers.html
總結
以上是生活随笔為你收集整理的100份Spring面试问答-最终名单(PDF下载)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 三国演义魏国代表人物介绍(魏国主要名将历
- 下一篇: 检测Maven依赖中介