- 1. 一般問題
- 1.1. 不同版本的 Spring Framework 有哪些主要功能?
- 1.2. 什么是 Spring Framework?
- 1.3. 列舉 Spring Framework 的優(yōu)點(diǎn)。
- 1.4. Spring Framework 有哪些不同的功能?
- 1.5. Spring Framework 中有多少個(gè)模塊,它們分別是什么?
- 1.6. 什么是 Spring 配置文件?
- 1.7. Spring 應(yīng)用程序有哪些不同組件?
- 1.8. 使用 Spring 有哪些方式?
- 2. 依賴注入(Ioc)
- 2.1. 什么是 Spring IOC 容器?
- 2.2. 什么是依賴注入?
- 2.3. 可以通過多少種方式完成依賴注入?
- 2.4. 區(qū)分構(gòu)造函數(shù)注入和 setter 注入。
- 2.5. spring 中有多少種 IOC 容器?
- 2.6. 區(qū)分 BeanFactory 和 ApplicationContext。
- 2.7. 列舉 IoC 的一些好處。
- 2.8. Spring IoC 的實(shí)現(xiàn)機(jī)制。
- 3. Beans
- 3.1. 什么是 spring bean?
- 3.2. spring 提供了哪些配置方式?
- 3.3. spring 支持集中 bean scope?
- 3.4. spring bean 容器的生命周期是什么樣的?
- 3.5. 什么是 spring 的內(nèi)部 bean?
- 3.6. 什么是 spring 裝配
- 3.7. 自動(dòng)裝配有哪些方式?
- 3.8. 自動(dòng)裝配有什么局限?
- 4. 注解
- 4.1. 你用過哪些重要的 Spring 注解?
- 4.2. 如何在 spring 中啟動(dòng)注解裝配?
- 4.3. @Component, @Controller, @Repository, @Service 有何區(qū)別?
- 4.4. @Required 注解有什么用?
- 4.5. @Autowired 注解有什么用?
- 4.6. @Qualifier 注解有什么用?
- 4.7. @RequestMapping 注解有什么用?
- 5. 數(shù)據(jù)訪問
- 5.1. spring DAO 有什么用?
- 5.2. 列舉 Spring DAO 拋出的異常。
- 5.3. spring JDBC API 中存在哪些類?
- 5.4. 使用 Spring 訪問 Hibernate 的方法有哪些?
- 5.5. 列舉 spring 支持的事務(wù)管理類型
- 5.6. spring 支持哪些 ORM 框架
- 6. AOP
- 6.1. 什么是 AOP?
- 6.2. AOP 中的 Aspect、Advice、Pointcut、JointPoint 和 Advice 參數(shù)分別是什么?
- 6.3. 什么是通知(Advice)?
- 6.4. 有哪些類型的通知(Advice)?
- 6.5. 指出在 spring aop 中 concern 和 cross-cutting concern 的不同之處。
- 6.6. AOP 有哪些實(shí)現(xiàn)方式?
- 6.7. Spring AOP and AspectJ AOP 有什么區(qū)別?
- 6.8. 如何理解 Spring 中的代理?
- 6.9. 什么是編織(Weaving)?
- 7. MVC
- 7.1. Spring MVC 框架有什么用?
- 7.2. 描述一下 DispatcherServlet 的工作流程
- 7.3. 介紹一下 WebApplicationContext
- 8. 資料
1. 一般問題
1.1. 不同版本的 Spring Framework 有哪些主要功能?
1.2. 什么是 Spring Framework?
- Spring 是一個(gè)開源應(yīng)用框架,旨在降低應(yīng)用程序開發(fā)的復(fù)雜度。
- 它是輕量級(jí)、松散耦合的。
- 它具有分層體系結(jié)構(gòu),允許用戶選擇組件,同時(shí)還為 J2EE 應(yīng)用程序開發(fā)提供了一個(gè)有凝聚力的框架。
- 它可以集成其他框架,如 Structs、Hibernate、EJB 等,所以又稱為框架的框架。
1.3. 列舉 Spring Framework 的優(yōu)點(diǎn)。
- 由于 Spring Frameworks 的分層架構(gòu),用戶可以自由選擇自己需要的組件。
- Spring Framework 支持 POJO(Plain Old Java Object) 編程,從而具備持續(xù)集成和可測(cè)試性。
- 由于依賴注入和控制反轉(zhuǎn),JDBC 得以簡化。
- 它是開源免費(fèi)的。
1.4. Spring Framework 有哪些不同的功能?
- 輕量級(jí) - Spring 在代碼量和透明度方面都很輕便。
- IOC - 控制反轉(zhuǎn)
- AOP - 面向切面編程可以將應(yīng)用業(yè)務(wù)邏輯和系統(tǒng)服務(wù)分離,以實(shí)現(xiàn)高內(nèi)聚。
- 容器 - Spring 負(fù)責(zé)創(chuàng)建和管理對(duì)象(Bean)的生命周期和配置。
- MVC - 對(duì) web 應(yīng)用提供了高度可配置性,其他框架的集成也十分方便。
- 事務(wù)管理 - 提供了用于事務(wù)管理的通用抽象層。Spring 的事務(wù)支持也可用于容器較少的環(huán)境。
- JDBC 異常 - Spring 的 JDBC 抽象層提供了一個(gè)異常層次結(jié)構(gòu),簡化了錯(cuò)誤處理策略。
1.5. Spring Framework 中有多少個(gè)模塊,它們分別是什么?
Spring 核心容器 – 該層基本上是 Spring Framework 的核心。它包含以下模塊:
- Spring Core
- Spring Bean
- SpEL (Spring Expression Language)
- Spring Context
數(shù)據(jù)訪問/集成 – 該層提供與數(shù)據(jù)庫交互的支持。它包含以下模塊:
- JDBC (Java DataBase Connectivity)
- ORM (Object Relational Mapping)
- OXM (Object XML Mappers)
- JMS (Java Messaging Service)
- Transaction
Web – 該層提供了創(chuàng)建 Web 應(yīng)用程序的支持。它包含以下模塊:
- Web
- Web – Servlet
- Web – Socket
- Web – Portlet
- AOP – 該層支持面向切面編程
- Instrumentation – 該層為類檢測(cè)和類加載器實(shí)現(xiàn)提供支持。
- Test – 該層為使用 JUnit 和 TestNG 進(jìn)行測(cè)試提供支持。
幾個(gè)雜項(xiàng)模塊:
- Messaging – 該模塊為 STOMP 提供支持。它還支持注解編程模型,該模型用于從 WebSocket 客戶端路由和處理 STOMP 消息。
- Aspects – 該模塊為與 AspectJ 的集成提供支持。
1.6. 什么是 Spring 配置文件?
Spring 配置文件是 XML 文件。該文件主要包含類信息。它描述了這些類是如何配置以及相互引入的。但是,XML 配置文件冗長且更加干凈。如果沒有正確規(guī)劃和編寫,那么在大項(xiàng)目中管理變得非常困難。
1.7. Spring 應(yīng)用程序有哪些不同組件?
Spring 應(yīng)用一般有以下組件:
- 接口 - 定義功能。
- Bean 類 - 它包含屬性,setter 和 getter 方法,函數(shù)等。
- Spring 面向切面編程(AOP) - 提供面向切面編程的功能。
- Bean 配置文件 - 包含類的信息以及如何配置它們。
- 用戶程序 - 它使用接口。
1.8. 使用 Spring 有哪些方式?
使用 Spring 有以下方式:
- 作為一個(gè)成熟的 Spring Web 應(yīng)用程序。
- 作為第三方 Web 框架,使用 Spring Frameworks 中間層。
- 用于遠(yuǎn)程使用。
- 作為企業(yè)級(jí) Java Bean,它可以包裝現(xiàn)有的 POJO(Plain Old Java Objects)。
2. 依賴注入(Ioc)
2.1. 什么是 Spring IOC 容器?
Spring 框架的核心是 Spring 容器。容器創(chuàng)建對(duì)象,將它們裝配在一起,配置它們并管理它們的完整生命周期。Spring 容器使用依賴注入來管理組成應(yīng)用程序的組件。
容器通過讀取提供的配置元數(shù)據(jù)來接收對(duì)象進(jìn)行實(shí)例化,配置和組裝的指令。該元數(shù)據(jù)可以通過 XML,Java 注解或 Java 代碼提供。
2.2. 什么是依賴注入?
在依賴注入中,您不必創(chuàng)建對(duì)象,但必須描述如何創(chuàng)建它們。您不是直接在代碼中將組件和服務(wù)連接在一起,而是描述配置文件中哪些組件需要哪些服務(wù)。由 IoC 容器將它們裝配在一起。
2.3. 可以通過多少種方式完成依賴注入?
通常,依賴注入可以通過三種方式完成,即:
- 構(gòu)造函數(shù)注入
- setter 注入
- 接口注入
在 Spring Framework 中,僅使用構(gòu)造函數(shù)和 setter 注入。
2.4. 區(qū)分構(gòu)造函數(shù)注入和 setter 注入。
2.5. spring 中有多少種 IOC 容器?
- BeanFactory - BeanFactory 就像一個(gè)包含 bean 集合的工廠類。它會(huì)在客戶端要求時(shí)實(shí)例化 bean。
- ApplicationContext - ApplicationContext 接口擴(kuò)展了 BeanFactory 接口。它在 BeanFactory 基礎(chǔ)上提供了一些額外的功能。
2.6. 區(qū)分 BeanFactory 和 ApplicationContext。
2.7. 列舉 IoC 的一些好處。
IoC 的一些好處是:
- 它將最小化應(yīng)用程序中的代碼量。
- 它將使您的應(yīng)用程序易于測(cè)試,因?yàn)樗恍枰獑卧獪y(cè)試用例中的任何單例或 JNDI 查找機(jī)制。
- 它以最小的影響和最少的侵入機(jī)制促進(jìn)松耦合。
- 它支持即時(shí)的實(shí)例化和延遲加載服務(wù)。
2.8. Spring IoC 的實(shí)現(xiàn)機(jī)制。
Spring 中的 IoC 的實(shí)現(xiàn)原理就是工廠模式加反射機(jī)制。
示例:
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">interface Fruit {
public abstract void eat();
}
class Apple implements Fruit {
public void eat(){
System.out.println("Apple");
}
}
class Orange implements Fruit {
public void eat(){
System.out.println("Orange");
}
}
class Factory {
public static Fruit getInstance(String ClassName) {
Fruit f=null;
try {
f=(Fruit)Class.forName(ClassName).newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return f;
}
}
class Client {
public static void main(String[] a) {
Fruit f=Factory.getInstance("io.github.dunwu.spring.Apple");
if(f!=null){
f.eat();
}
}
}
</pre>
3. Beans
3.1. 什么是 spring bean?
- 它們是構(gòu)成用戶應(yīng)用程序主干的對(duì)象。
- Bean 由 Spring IoC 容器管理。
- 它們由 Spring IoC 容器實(shí)例化,配置,裝配和管理。
- Bean 是基于用戶提供給容器的配置元數(shù)據(jù)創(chuàng)建。
3.2. spring 提供了哪些配置方式?
- 基于 xml 配置
bean 所需的依賴項(xiàng)和服務(wù)在 XML 格式的配置文件中指定。這些配置文件通常包含許多 bean 定義和特定于應(yīng)用程序的配置選項(xiàng)。它們通常以 bean 標(biāo)簽開頭。例如:
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><bean id="studentbean" class="org.edureka.firstSpring.StudentBean">
<property name="name" value="Edureka"></property>
</bean>
</pre>
- 基于注解配置
您可以通過在相關(guān)的類,方法或字段聲明上使用注解,將 bean 配置為組件類本身,而不是使用 XML 來描述 bean 裝配。默認(rèn)情況下,Spring 容器中未打開注解裝配。因此,您需要在使用它之前在 Spring 配置文件中啟用它。例如:
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><beans>
<context:annotation-config/>
</beans>
</pre>
- 基于 Java API 配置
Spring 的 Java 配置是通過使用 @Bean 和 @Configuration 來實(shí)現(xiàn)。
- @Bean 注解扮演與 元素相同的角色。
- @Configuration 類允許通過簡單地調(diào)用同一個(gè)類中的其他 @Bean 方法來定義 bean 間依賴關(guān)系。
例如:
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@Configuration
public class StudentConfig {
@Bean
public StudentBean myStudent() {
return new StudentBean();
}
}
</pre>
3.3. spring 支持集中 bean scope?
Spring bean 支持 5 種 scope:
- Singleton - 每個(gè) Spring IoC 容器僅有一個(gè)單實(shí)例。
- Prototype - 每次請(qǐng)求都會(huì)產(chǎn)生一個(gè)新的實(shí)例。
- Request - 每一次 HTTP 請(qǐng)求都會(huì)產(chǎn)生一個(gè)新的實(shí)例,并且該 bean 僅在當(dāng)前 HTTP 請(qǐng)求內(nèi)有效。
- Session - 每一次 HTTP 請(qǐng)求都會(huì)產(chǎn)生一個(gè)新的 bean,同時(shí)該 bean 僅在當(dāng)前 HTTP session 內(nèi)有效。
- Global-session - 類似于標(biāo)準(zhǔn)的 HTTP Session 作用域,不過它僅僅在基于 portlet 的 web 應(yīng)用中才有意義。Portlet 規(guī)范定義了全局 Session 的概念,它被所有構(gòu)成某個(gè) portlet web 應(yīng)用的各種不同的 portlet 所共享。在 global session 作用域中定義的 bean 被限定于全局 portlet Session 的生命周期范圍內(nèi)。如果你在 web 中使用 global session 作用域來標(biāo)識(shí) bean,那么 web 會(huì)自動(dòng)當(dāng)成 session 類型來使用。
僅當(dāng)用戶使用支持 Web 的 ApplicationContext 時(shí),最后三個(gè)才可用。更多spring內(nèi)容,可以看這里:Spring內(nèi)容合集
3.4. spring bean 容器的生命周期是什么樣的?
spring bean 容器的生命周期流程如下:
- Spring 容器根據(jù)配置中的 bean 定義中實(shí)例化 bean。
- Spring 使用依賴注入填充所有屬性,如 bean 中所定義的配置。
- 如果 bean 實(shí)現(xiàn) BeanNameAware 接口,則工廠通過傳遞 bean 的 ID 來調(diào)用 setBeanName()。
- 如果 bean 實(shí)現(xiàn) BeanFactoryAware 接口,工廠通過傳遞自身的實(shí)例來調(diào)用 setBeanFactory()。
- 如果存在與 bean 關(guān)聯(lián)的任何 BeanPostProcessors,則調(diào)用 preProcessBeforeInitialization() 方法。
- 如果為 bean 指定了 init 方法( 的 init-method 屬性),那么將調(diào)用它。
- 最后,如果存在與 bean 關(guān)聯(lián)的任何 BeanPostProcessors,則將調(diào)用 postProcessAfterInitialization() 方法。
- 如果 bean 實(shí)現(xiàn) DisposableBean 接口,當(dāng) spring 容器關(guān)閉時(shí),會(huì)調(diào)用 destory()。
- 如果為 bean 指定了 destroy 方法( 的 destroy-method 屬性),那么將調(diào)用它。
3.5. 什么是 spring 的內(nèi)部 bean?
只有將 bean 用作另一個(gè) bean 的屬性時(shí),才能將 bean 聲明為內(nèi)部 bean。為了定義 bean,Spring 的基于 XML 的配置元數(shù)據(jù)在 <property> 或 <constructor-arg> 中提供了 <bean> 元素的使用。內(nèi)部 bean 總是匿名的,它們總是作為原型。
例如,假設(shè)我們有一個(gè) Student 類,其中引用了 Person 類。這里我們將只創(chuàng)建一個(gè) Person 類實(shí)例并在 Student 中使用它。
Student.java
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class Student {
private Person person;
//Setters and Getters
}
public class Person {
private String name;
private String address;
//Setters and Getters
}
</pre>
bean.xml
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><bean id=“StudentBean" class="com.edureka.Student">
<property name="person">
<bean class="com.edureka.Person">
<property name="name" value=“Scott"></property>
<property name="address" value=“Bangalore"></property>
</bean>
</property>
</bean>
</pre>
3.6. 什么是 spring 裝配
當(dāng) bean 在 Spring 容器中組合在一起時(shí),它被稱為裝配或 bean 裝配。Spring 容器需要知道需要什么 bean 以及容器應(yīng)該如何使用依賴注入來將 bean 綁定在一起,同時(shí)裝配 bean。
3.7. 自動(dòng)裝配有哪些方式?
Spring 容器能夠自動(dòng)裝配 bean。也就是說,可以通過檢查 BeanFactory 的內(nèi)容讓 Spring 自動(dòng)解析 bean 的協(xié)作者。更多spring內(nèi)容,可以看這里:Spring內(nèi)容合集
自動(dòng)裝配的不同模式:
- no - 這是默認(rèn)設(shè)置,表示沒有自動(dòng)裝配。應(yīng)使用顯式 bean 引用進(jìn)行裝配。
- byName - 它根據(jù) bean 的名稱注入對(duì)象依賴項(xiàng)。它匹配并裝配其屬性與 XML 文件中由相同名稱定義的 bean。
- byType - 它根據(jù)類型注入對(duì)象依賴項(xiàng)。如果屬性的類型與 XML 文件中的一個(gè) bean 名稱匹配,則匹配并裝配屬性。
- 構(gòu)造函數(shù) - 它通過調(diào)用類的構(gòu)造函數(shù)來注入依賴項(xiàng)。它有大量的參數(shù)。
- autodetect - 首先容器嘗試通過構(gòu)造函數(shù)使用 autowire 裝配,如果不能,則嘗試通過 byType 自動(dòng)裝配。
3.8. 自動(dòng)裝配有什么局限?
- 覆蓋的可能性 - 您始終可以使用 和 設(shè)置指定依賴項(xiàng),這將覆蓋自動(dòng)裝配。
- 基本元數(shù)據(jù)類型 - 簡單屬性(如原數(shù)據(jù)類型,字符串和類)無法自動(dòng)裝配。
- 令人困惑的性質(zhì) - 總是喜歡使用明確的裝配,因?yàn)樽詣?dòng)裝配不太精確。
4. 注解
4.1. 你用過哪些重要的 Spring 注解?
- @Controller - 用于 Spring MVC 項(xiàng)目中的控制器類。
- @Service - 用于服務(wù)類。
- @RequestMapping - 用于在控制器處理程序方法中配置 URI 映射。
- @ResponseBody - 用于發(fā)送 Object 作為響應(yīng),通常用于發(fā)送 XML 或 JSON 數(shù)據(jù)作為響應(yīng)。
- @PathVariable - 用于將動(dòng)態(tài)值從 URI 映射到處理程序方法參數(shù)。
- @Autowired - 用于在 spring bean 中自動(dòng)裝配依賴項(xiàng)。
- @Qualifier - 使用 @Autowired 注解,以避免在存在多個(gè) bean 類型實(shí)例時(shí)出現(xiàn)混淆。
- @Scope - 用于配置 spring bean 的范圍。
- @Configuration,@ComponentScan 和 @Bean - 用于基于 java 的配置。
- @Aspect,@Before,@After,@Around,@Pointcut - 用于切面編程(AOP)。
4.2. 如何在 spring 中啟動(dòng)注解裝配?
默認(rèn)情況下,Spring 容器中未打開注解裝配。因此,要使用基于注解裝配,我們必須通過配置<context:annotation-config /> 元素在 Spring 配置文件中啟用它。
4.3. @Component, @Controller, @Repository, @Service 有何區(qū)別?
- @Component:這將 java 類標(biāo)記為 bean。它是任何 Spring 管理組件的通用構(gòu)造型。spring 的組件掃描機(jī)制現(xiàn)在可以將其拾取并將其拉入應(yīng)用程序環(huán)境中。
- @Controller:這將一個(gè)類標(biāo)記為 Spring Web MVC 控制器。標(biāo)有它的 Bean 會(huì)自動(dòng)導(dǎo)入到 IoC 容器中。
- @Service:此注解是組件注解的特化。它不會(huì)對(duì) @Component 注解提供任何其他行為。您可以在服務(wù)層類中使用 @Service 而不是 @Component,因?yàn)樗愿玫姆绞街付艘鈭D。
- @Repository:這個(gè)注解是具有類似用途和功能的 @Component 注解的特化。它為 DAO 提供了額外的好處。它將 DAO 導(dǎo)入 IoC 容器,并使未經(jīng)檢查的異常有資格轉(zhuǎn)換為 Spring DataAccessException。
4.4. @Required 注解有什么用?
@Required 應(yīng)用于 bean 屬性 setter 方法。此注解僅指示必須在配置時(shí)使用 bean 定義中的顯式屬性值或使用自動(dòng)裝配填充受影響的 bean 屬性。如果尚未填充受影響的 bean 屬性,則容器將拋出 BeanInitializationException。
示例:
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class Employee {
private String name;
@Required
public void setName(String name){
this.name=name;
}
public string getName(){
return name;
}
}
</pre>
4.5. @Autowired 注解有什么用?
@Autowired 可以更準(zhǔn)確地控制應(yīng)該在何處以及如何進(jìn)行自動(dòng)裝配。此注解用于在 setter 方法,構(gòu)造函數(shù),具有任意名稱或多個(gè)參數(shù)的屬性或方法上自動(dòng)裝配 bean。默認(rèn)情況下,它是類型驅(qū)動(dòng)的注入。
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class Employee {
private String name;
@Autowired
public void setName(String name) {
this.name=name;
}
public string getName(){
return name;
}
}
</pre>
4.6. @Qualifier 注解有什么用?
當(dāng)您創(chuàng)建多個(gè)相同類型的 bean 并希望僅使用屬性裝配其中一個(gè) bean 時(shí),您可以使用@Qualifier 注解和 @Autowired 通過指定應(yīng)該裝配哪個(gè)確切的 bean 來消除歧義。
例如,這里我們分別有兩個(gè)類,Employee 和 EmpAccount。在 EmpAccount 中,使用@Qualifier 指定了必須裝配 id 為 emp1 的 bean。
Employee.java
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class Employee {
private String name;
@Autowired
public void setName(String name) {
this.name=name;
}
public string getName() {
return name;
}
}
</pre>
EmpAccount.java
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public class EmpAccount {
private Employee emp;
@Autowired
@Qualifier(emp1)
public void showName() {
System.out.println(“Employee name : ”+emp.getName);
}
}
</pre>
4.7. @RequestMapping 注解有什么用?
@RequestMapping 注解用于將特定 HTTP 請(qǐng)求方法映射到將處理相應(yīng)請(qǐng)求的控制器中的特定類/方法。此注解可應(yīng)用于兩個(gè)級(jí)別:
- 類級(jí)別:映射請(qǐng)求的 URL
- 方法級(jí)別:映射 URL 以及 HTTP 請(qǐng)求方法
5. 數(shù)據(jù)訪問
5.1. spring DAO 有什么用?
Spring DAO 使得 JDBC,Hibernate 或 JDO 這樣的數(shù)據(jù)訪問技術(shù)更容易以一種統(tǒng)一的方式工作。這使得用戶容易在持久性技術(shù)之間切換。它還允許您在編寫代碼時(shí),無需考慮捕獲每種技術(shù)不同的異常。更多spring內(nèi)容,可以看這里:Spring內(nèi)容合集
5.2. 列舉 Spring DAO 拋出的異常。
5.3. spring JDBC API 中存在哪些類?
- JdbcTemplate
- SimpleJdbcTemplate
- NamedParameterJdbcTemplate
- SimpleJdbcInsert
- SimpleJdbcCall
5.4. 使用 Spring 訪問 Hibernate 的方法有哪些?
我們可以通過兩種方式使用 Spring 訪問 Hibernate:
- 使用 Hibernate 模板和回調(diào)進(jìn)行控制反轉(zhuǎn)
- 擴(kuò)展 HibernateDAOSupport 并應(yīng)用 AOP 攔截器節(jié)點(diǎn)
5.5. 列舉 spring 支持的事務(wù)管理類型
Spring 支持兩種類型的事務(wù)管理:
- 程序化事務(wù)管理:在此過程中,在編程的幫助下管理事務(wù)。它為您提供極大的靈活性,但維護(hù)起來非常困難。
- 聲明式事務(wù)管理:在此,事務(wù)管理與業(yè)務(wù)代碼分離。僅使用注解或基于 XML 的配置來管理事務(wù)。
5.6. spring 支持哪些 ORM 框架
- Hibernate
- iBatis
- JPA
- JDO
- OJB
6. AOP
6.1. 什么是 AOP?
AOP(Aspect-Oriented Programming), 即 面向切面編程, 它與 OOP( Object-Oriented Programming, 面向?qū)ο缶幊? 相輔相成, 提供了與 OOP 不同的抽象軟件結(jié)構(gòu)的視角.
在 OOP 中, 我們以類(class)作為我們的基本單元, 而 AOP 中的基本單元是 Aspect(切面)
6.2. AOP 中的 Aspect、Advice、Pointcut、JointPoint 和 Advice 參數(shù)分別是什么?
- Aspect - Aspect 是一個(gè)實(shí)現(xiàn)交叉問題的類,例如事務(wù)管理。方面可以是配置的普通類,然后在 Spring Bean 配置文件中配置,或者我們可以使用 Spring AspectJ 支持使用 @Aspect 注解將類聲明為 Aspect。
- Advice - Advice 是針對(duì)特定 JoinPoint 采取的操作。在編程方面,它們是在應(yīng)用程序中達(dá)到具有匹配切入點(diǎn)的特定 JoinPoint 時(shí)執(zhí)行的方法。您可以將 Advice 視為 Spring 攔截器(Interceptor)或 Servlet 過濾器(filter)。
- Advice Arguments - 我們可以在 advice 方法中傳遞參數(shù)。我們可以在切入點(diǎn)中使用 args() 表達(dá)式來應(yīng)用于與參數(shù)模式匹配的任何方法。如果我們使用它,那么我們需要在確定參數(shù)類型的 advice 方法中使用相同的名稱。
- Pointcut - Pointcut 是與 JoinPoint 匹配的正則表達(dá)式,用于確定是否需要執(zhí)行 Advice。Pointcut 使用與 JoinPoint 匹配的不同類型的表達(dá)式。Spring 框架使用 AspectJ Pointcut 表達(dá)式語言來確定將應(yīng)用通知方法的 JoinPoint。
- JoinPoint - JoinPoint 是應(yīng)用程序中的特定點(diǎn),例如方法執(zhí)行,異常處理,更改對(duì)象變量值等。在 Spring AOP 中,JoinPoint 始終是方法的執(zhí)行器。
6.3. 什么是通知(Advice)?
特定 JoinPoint 處的 Aspect 所采取的動(dòng)作稱為 Advice。Spring AOP 使用一個(gè) Advice 作為攔截器,在 JoinPoint “周圍”維護(hù)一系列的攔截器。
6.4. 有哪些類型的通知(Advice)?
- Before - 這些類型的 Advice 在 joinpoint 方法之前執(zhí)行,并使用 @Before 注解標(biāo)記進(jìn)行配置。
- After Returning - 這些類型的 Advice 在連接點(diǎn)方法正常執(zhí)行后執(zhí)行,并使用@AfterReturning 注解標(biāo)記進(jìn)行配置。
- After Throwing - 這些類型的 Advice 僅在 joinpoint 方法通過拋出異常退出并使用 @AfterThrowing 注解標(biāo)記配置時(shí)執(zhí)行。
- After (finally) - 這些類型的 Advice 在連接點(diǎn)方法之后執(zhí)行,無論方法退出是正常還是異常返回,并使用 @After 注解標(biāo)記進(jìn)行配置。
- Around - 這些類型的 Advice 在連接點(diǎn)之前和之后執(zhí)行,并使用 @Around 注解標(biāo)記進(jìn)行配置。
6.5. 指出在 spring aop 中 concern 和 cross-cutting concern 的不同之處。
concern 是我們想要在應(yīng)用程序的特定模塊中定義的行為。它可以定義為我們想要實(shí)現(xiàn)的功能。
cross-cutting concern 是一個(gè)適用于整個(gè)應(yīng)用的行為,這會(huì)影響整個(gè)應(yīng)用程序。例如,日志記錄,安全性和數(shù)據(jù)傳輸是應(yīng)用程序幾乎每個(gè)模塊都需要關(guān)注的問題,因此它們是跨領(lǐng)域的問題。
6.6. AOP 有哪些實(shí)現(xiàn)方式?
實(shí)現(xiàn) AOP 的技術(shù),主要分為兩大類:
- 靜態(tài)代理 - 指使用 AOP 框架提供的命令進(jìn)行編譯,從而在編譯階段就可生成 AOP 代理類,因此也稱為編譯時(shí)增強(qiáng);
- 編譯時(shí)編織(特殊編譯器實(shí)現(xiàn))
- 類加載時(shí)編織(特殊的類加載器實(shí)現(xiàn))。
- 動(dòng)態(tài)代理 - 在運(yùn)行時(shí)在內(nèi)存中“臨時(shí)”生成 AOP 動(dòng)態(tài)代理類,因此也被稱為運(yùn)行時(shí)增強(qiáng)。
- JDK 動(dòng)態(tài)代理
- CGLIB
6.7. Spring AOP and AspectJ AOP 有什么區(qū)別?
Spring AOP 基于動(dòng)態(tài)代理方式實(shí)現(xiàn);AspectJ 基于靜態(tài)代理方式實(shí)現(xiàn)。
Spring AOP 僅支持方法級(jí)別的 PointCut;提供了完全的 AOP 支持,它還支持屬性級(jí)別的 PointCut。
6.8. 如何理解 Spring 中的代理?
將 Advice 應(yīng)用于目標(biāo)對(duì)象后創(chuàng)建的對(duì)象稱為代理。在客戶端對(duì)象的情況下,目標(biāo)對(duì)象和代理對(duì)象是相同的。
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">Advice + Target Object = Proxy
</pre>
6.9. 什么是編織(Weaving)?
為了創(chuàng)建一個(gè) advice 對(duì)象而鏈接一個(gè) aspect 和其它應(yīng)用類型或?qū)ο?,稱為編織(Weaving)。在 Spring AOP 中,編織在運(yùn)行時(shí)執(zhí)行。請(qǐng)參考下圖:
7. MVC
7.1. Spring MVC 框架有什么用?
Spring Web MVC 框架提供 模型-視圖-控制器 架構(gòu)和隨時(shí)可用的組件,用于開發(fā)靈活且松散耦合的 Web 應(yīng)用程序。MVC 模式有助于分離應(yīng)用程序的不同方面,如輸入邏輯,業(yè)務(wù)邏輯和 UI 邏輯,同時(shí)在所有這些元素之間提供松散耦合。
7.2. 描述一下 DispatcherServlet 的工作流程
DispatcherServlet 的工作流程可以用一幅圖來說明:
- 向服務(wù)器發(fā)送 HTTP 請(qǐng)求,請(qǐng)求被前端控制器 DispatcherServlet 捕獲。
- DispatcherServlet 根據(jù) -servlet.xml 中的配置對(duì)請(qǐng)求的 URL 進(jìn)行解析,得到請(qǐng)求資源標(biāo)識(shí)符(URI)。然后根據(jù)該 URI,調(diào)用 HandlerMapping 獲得該 Handler 配置的所有相關(guān)的對(duì)象(包括 Handler 對(duì)象以及 Handler 對(duì)象對(duì)應(yīng)的攔截器),最后以HandlerExecutionChain 對(duì)象的形式返回。
- DispatcherServlet 根據(jù)獲得的Handler,選擇一個(gè)合適的 HandlerAdapter。(附注:如果成功獲得HandlerAdapter后,此時(shí)將開始執(zhí)行攔截器的 preHandler(…)方法)。
- 提取Request中的模型數(shù)據(jù),填充Handler入?yún)?,開始執(zhí)行Handler(Controller)。 在填充Handler的入?yún)⑦^程中,根據(jù)你的配置,Spring 將幫你做一些額外的工作:
- HttpMessageConveter:將請(qǐng)求消息(如 Json、xml 等數(shù)據(jù))轉(zhuǎn)換成一個(gè)對(duì)象,將對(duì)象轉(zhuǎn)換為指定的響應(yīng)信息。
- 數(shù)據(jù)轉(zhuǎn)換:對(duì)請(qǐng)求消息進(jìn)行數(shù)據(jù)轉(zhuǎn)換。如
String轉(zhuǎn)換成Integer、Double等。 - 數(shù)據(jù)根式化:對(duì)請(qǐng)求消息進(jìn)行數(shù)據(jù)格式化。如將字符串轉(zhuǎn)換成格式化數(shù)字或格式化日期等。
- 數(shù)據(jù)驗(yàn)證:驗(yàn)證數(shù)據(jù)的有效性(長度、格式等),驗(yàn)證結(jié)果存儲(chǔ)到
BindingResult或Error中。
- Handler(Controller)執(zhí)行完成后,向 DispatcherServlet 返回一個(gè) ModelAndView 對(duì)象;
- 根據(jù)返回的ModelAndView,選擇一個(gè)適合的 ViewResolver(必須是已經(jīng)注冊(cè)到 Spring 容器中的ViewResolver)返回給DispatcherServlet。
- ViewResolver 結(jié)合Model和View,來渲染視圖。
- 視圖負(fù)責(zé)將渲染結(jié)果返回給客戶端。
7.3. 介紹一下 WebApplicationContext
WebApplicationContext 是 ApplicationContext 的擴(kuò)展。它具有 Web 應(yīng)用程序所需的一些額外功能。它與普通的 ApplicationContext 在解析主題和決定與哪個(gè) servlet 關(guān)聯(lián)的能力方面有所不同。
8. 資料
歡迎工作一到五年的Java工程師朋友們加入JavaQQ群:957734884,群內(nèi)提供免費(fèi)的Java架構(gòu)學(xué)習(xí)資料(里面有高可用、高并發(fā)、高性能及分布式、Jvm性能調(diào)優(yōu)、Spring源碼,MyBatis,Netty,Redis,Kafka,Mysql,Zookeeper,Tomcat,Docker,Dubbo,Nginx等多個(gè)知識(shí)點(diǎn)的架構(gòu)資料)合理利用自己每一分每一秒的時(shí)間來學(xué)習(xí)提升自己,不要再用"沒有時(shí)間“來掩飾自己思想上的懶惰!趁年輕,使勁拼,給未來的自己一個(gè)交代!
