- Spring 框架是一個為 Java 應用程式的開發提供了綜合、廣泛的基礎性支持的 Java 平臺,
- Spring 幫助開發者解決了開發中基礎性的問題,使得開發人員可以專注于應用程式的開發,
- Spring 框架本身亦是按照設計模式精心打造,這使得我們可以在開發環境中安心的集成 Spring 框架,不必擔心 Spring 是如何在后臺進行作業的,
- Spring 框架至今已集成了 20 多個模塊,這些模塊主要被分如下圖所示的核心容器、資料訪問/集成,、Web、AOP(面向切面編程)、工具、訊息和測驗模塊,

使用 Spring 框架能帶來哪些好處?
使用Spring帶來的好處是顯而易見的
-
Dependency Injection(DI) 方法使得構造器和 JavaBean properties 檔案中的依賴關系一目了然,
-
與 EJB 容器相比較,IoC 容器更加趨向于輕量級,這樣一來 IoC 容器在有限的記憶體和 CPU 資源的情況下進行應用程式的開發和發布就變得十分有利,
-
Spring 并沒有閉門造車,Spring 利用了已有的技術比如 ORM 框架、logging 框架、J2EE、Quartz 和 JDK Timer,以及其他視圖技術,
-
Spring 框架是按照模塊的形式來組織的,由包和類的編號就可以看出其所屬的模塊,開發者僅僅需要選用他們需要的模塊即可,
-
要測驗一項用 Spring 開發的應用程式十分簡單,因為測驗相關的環境代碼都已經囊括在框架中了,更加簡單的是,利用 JavaBean 形式的 POJO 類,可以很方便的利用依賴注入來寫入測驗資料,
-
Spring 的 Web 框架亦是一個精心設計的 Web MVC 框架,為開發者們在 web 框架的選擇上提供了一個除了主流框架比如 Struts、過度設計的、不流行 web 框架的以外的有力選項,
-
Spring 提供了一個便捷的事務管理介面,適用于小型的本地事物處理(比如在單 DB 的環境下)和復雜的共同事物處理(比如利用 JTA 的復雜 DB 環境),
本人在這里整理了20多家公司的面試題,以及各種關于Spring、Spring boot、Spring MVC、MyBatis、MySQL、JVM等知識點,如果有需要的小伙伴可以加群1149778920 暗號:qf

Spring 框架中的 IoC
Spring 中的 org.springframework.beans 包和 org.springframework.context 包構成了 Spring 框架 IoC 容器的基礎,
BeanFactory 介面提供了一個先進的配置機制,使得任何型別的物件的配置成為可能,
ApplicationContex 介面對 BeanFactory (是一個子介面)進行了擴展,在 BeanFactory的基礎上添加了其他功能,比如與 Spring 的 AOP 更容易集成,也提供了處理 message resource的機制(用于國際化)、事件傳播以及應用層的特別配置,比如針對 Web 應用的WebApplicationContext,
org.springframework.beans.factory.BeanFactory 是 Spring IoC 容器的具體實作,用來包裝和管理前面提到的各種 bean,BeanFactory 介面是 Spring IoC 容器的核心介面,
IOC:把物件的創建、初始化、銷毀交給 spring 來管理,而不是由開發者控制,實作控制反轉,
BeanFactory 和 和 ApplicationContext 有什么區別?

BeanFactory 可以理解為含有 bean 集合的工廠類,BeanFactory 包含了種 bean 的定義,以便在接收到客戶端請求時將對應的 bean 實體化,
BeanFactory 還能在實體化物件的時生成協作類之間的關系,此舉將 bean 自身與 bean 客戶端的配置中解放出來,BeanFactory 還包含 了 bean 生命周期的控制,呼叫客戶端的初始化方法(initialization methods)和銷毀方法(destruction methods),
從表面上看,application context 如同 bean factory 一樣具有 bean 定義、bean 關聯關系的設定,根據請求分發 bean 的功能,但 applicationcontext 在此基礎上還提供了其他的功能,
- 提供了支持國際化的文本訊息
- 統一的資源檔案讀取方式
- 已在監聽器中注冊的 bean 的事件
以下是三種較常見的 ApplicationContext 實作方式:
1、ClassPathXmlApplicationContext:從 classpath 的 XML 組態檔中讀取背景關系,并生成背景關系定義,應用程式背景關系從程式環境變數中
ApplicationContext context = new
ClassPathXmlApplicationContext(“bean.xml”);
2、FileSystemXmlApplicationContext :由檔案系統中的 XML 組態檔讀取背景關系,
ApplicationContext context = new
FileSystemXmlApplicationContext(“bean.xml”);
3、XmlWebApplicationContext:由 Web 應用的 XML 檔案讀取背景關系,
- AnnotationConfigApplicationContext(基于 Java 配置啟動容器)

Spring 有幾種配置方式
將 Spring 配置到應用開發中有以下三種方式:
-
基于 XML 的配置
-
基于注解的配置
-
基于 Java 的配置
如何用基于 XML 配置的方式配置 Spring ?
在 Spring 框架中,依賴和服務需要在專門的組態檔來實作,我常用的 XML 格式的組態檔,這些組態檔的格式通常用 開頭,然后一系列的 bean 定義和專門的應用配置選項組成,
SpringXML 配置的主要目的時候是使所有的 Spring 組件都可以用 xml 檔案的形式來進行配置,這意味著不會出現其他的 Spring 配置型別(比如宣告的方式或基于 Java Class 的配置方式)Spring 的 XML 配置方式是使用被 Spring 命名空間的所支持的一系列的 XML 標簽來實作的,
Spring 有以下主要的命名空間:context、beans、jdbc、tx、aop、mvc 和 aso,
如:
<beans>
<!-- JSON Support -->
<bean name="viewResolver"
class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
<bean name="jsonTemplate"
class="org.springframework.web.servlet.view.json.MappingJackson2JsonV
iew"/>
<bean id="restTemplate"
class="org.springframework.web.client.RestTemplate"/>
</beans>
下面這個 web.xml 僅僅配置了 DispatcherServlet,這件最簡單的配置便能滿足應用程式配置運行時組件的需求,
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-
class>org.springframework.web.servlet.DispatcherServlet</servlet-
class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
如何用基于 Java 配置的方式配置 Spring ?
Spring 對 Java 配置的支持是由@Configuration 注解和@Bean 注解來實作的,由@Bean 注解
的方法將會實體化、配置和初始化一個 新物件,這個物件將由 Spring 的 IoC 容器來管理,
@Bean 宣告所起到的作用與 元素類似,被 @Configuration 所注解的類則表示這個類
的主要目的是作為 bean 定義的資源,被@Configuration 宣告的類可以通過在同一個類的 內部調
用@bean 方法來設定嵌入 bean 的依賴關系,
最簡單的@Configuration 宣告類請參考下面的代碼:
@Configuration
public class AppConfig{
@Bean
public MyService myService() {
return new MyServiceImpl();
}
}
對于上面的@Beans 組態檔相同的 XML 組態檔如下:
<beans>
<bean id="myService" class="com.somnus.services.MyServiceImpl"/>
</beans>
上述配置方式的實體化方式如下:利用 AnnotationConfigApplicationContext 類進行實體化
public static void main(String[] args) {
ApplicationContext ctx = new
AnnotationConfigApplicationContext(AppConfig.class);
MyService myService = ctx.getBean(MyService.class);
myService.doStuff();
}
要使用組件組建掃描,僅需用@Configuration 進行注解即可:
@Configuration
@ComponentScan(basePackages = "com.somnus")
public class AppConfig {
...
}
在上面的例子中,com.acme 包首先會被掃到,然后再容器內查找被@Component 宣告的類,找到后將這些類按照 Sring bean 定義進行注冊,
如果你要在你的 web 應用開發中選用上述的配置的方式的話,需要用AnnotationConfigWebApplicationContext 類來讀 取組態檔,可以用來配置 Spring 的Servlet 監聽器 ContextLoaderListener 或者 Spring MVC 的 DispatcherServlet,
<web-app>
<!-- Configure ContextLoaderListener to use
AnnotationConfigWebApplicationContext
instead of the default XmlWebApplicationContext -->
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicatio
nContext
</param-value>
</context-param>
<!-- Configuration locations must consist of one or more comma- or
space-delimited
fully-qualified @Configuration classes. Fully-qualified
packages may also be
specified for component-scanning -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.howtodoinjava.AppConfig</param-value>
</context-param>
<!-- Bootstrap the root application context as usual using
ContextLoaderListener -->
<listener>
<listener-
class>org.springframework.web.context.ContextLoaderListener</listener
-class>
</listener>
<!-- Declare a Spring MVC DispatcherServlet as usual -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-
class>org.springframework.web.servlet.DispatcherServlet</servlet-
class>
<!-- Configure DispatcherServlet to use
AnnotationConfigWebApplicationContext
instead of the default XmlWebApplicationContext -->
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicatio
nContext
</param-value>
</init-param>
<!-- Again, config locations must consist of one or more comma-
or space-delimited
and fully-qualified @Configuration classes -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.howtodoinjava.web.MvcConfig</param-
value>
</init-param>
</servlet>
<!-- map all requests for /app/* to the dispatcher servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
</web-app
解釋 Spring Bean 的生命周期
Spring Bean 的生命周期簡單易懂,在一個 bean 實體被初始化時,需要執行一系列的初始化操作
以達到可用的狀態,同樣的,當一個 bean 不在被呼叫時需要進行相關的析構操作,并從 bean 容
器中移除,
Spring bean factory 負責管理在 spring 容器中被創建的 bean 的生命周期,Bean 的生命周期
由兩組回呼(call back)方法組成,
- 初始化之后呼叫的回呼方法,
- 銷毀之前呼叫的回呼方法,
Spring 框架提供了以下四種方式來管理 bean 的生命周期事件:
- InitializingBean 和 DisposableBean 回呼介面
- 針對特殊行為的其他 Aware 介面
- Bean 組態檔中的 Custom init()方法和 destroy()方法
- @PostConstruct 和@PreDestroy 注解方式
使用 customInit() 和 customDestroy()方法管理 bean 生命周期的代碼樣例如
下:
<beans>
<bean id="demoBean" class="com.somnus.task.DemoBean" init-
method="customInit" destroy-method="customDestroy"></bean>
</beans>
最后
針對最近很多人都在面試,我這邊也整理了相當多的面試專題資料,也有其他大廠的面經,希望可以幫助到大家,
有需要的小伙伴可以加群1149778920 暗號:qf


轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/204111.html
標籤:java
