文章目錄
what is IOC
控制反轉(Inversion of Control,縮寫爲IoC),是面向對象編程中的一種設計原則,可以用來減低計算機代碼之間的耦合度。其中最常見的方式叫做依賴注入(Dependency Injection,簡稱DI),還有一種方式叫“依賴查找”(Dependency Lookup)
Dependency Injection
依賴注入
關於什麼是依賴
關於注入和查找以及拖拽
爲什麼要使用spring IOC
spring體系結構----IOC的位置 自己看官網
在日常程序開發過程當中,我們推薦面向抽象編程,面向抽象編程會產生類的依賴,當然如果你夠強大可以自己寫一個管理的容器,但是既然spring以及實現了,並且spring如此優秀,我們僅僅需要學習spring框架便可。
當我們有了一個管理對象的容器之後,類的產生過程也交給了容器,至於我們自己的app則可以不需要去關係這些對象的產生了。
spring實現IOC的思路和方法
spring實現IOC的思路是提供一些配置信息用來描述類之間的依賴關係,然後由容器去解析這些配置信息,繼而維護好對象之間的依賴關係,前提是對象之間的依賴關係必須在類中定義好,比如A.class中有一個B.class的屬性,那麼我們可以理解爲A依賴了B。既然我們在類中已經定義了他們之間的依賴關係那麼爲什麼還需要在配置文件中去描述和定義呢?
spring實現IOC的思路大致可以拆分成3點
-
應用程序中提供類,提供依賴關係(屬性或者構造方法)
-
把需要交給容器管理的對象通過配置信息告訴容器(xml、annotation,javaconfig)
-
把各個類之間的依賴關係通過配置信息告訴容器
配置這些信息的方法有三種分別是xml,annotation和javaconfig
維護的過程稱爲自動注入,自動注入的方法有兩種構造方法和setter
自動注入的值可以是對象,數組,map,list和常量比如字符串整形等
spring編程的風格
schemal-based-------xml
annotation-based-----annotation
java-based----java Configuration
注入的兩種方法
spring注入詳細配置(字符串、數組等)參考文檔:
https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-factory-properties-detailed
Constructor-based Dependency Injection
構造方法注入參考文檔:
https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-constructor-injection
Setter-based Dependency Injection
setter參考文檔:
https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-setter-injection
自動裝配
上面說過,IOC的注入有兩個地方需要提供依賴關係,一是類的定義中,二是在spring的配置中需要去描述。自動裝配則把第二個取消了,即我們僅僅需要在類中提供依賴,繼而把對象交給容器管理即可完成注入。
在實際開發中,描述類之間的依賴關係通常是大篇幅的,如果使用自動裝配則省去了很多配置,並且如果對象的依賴發生更新我們可以不需要去更新配置,但是也帶來了一定的缺點
自動裝配的優點參考文檔:
https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-factory-autowire
缺點參考文檔:
https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-autowired-exceptions
作爲我來講,我覺得以上缺點都不是缺點
自動裝配的方法
no
byName
byType
constructor
自動裝配的方式參考文檔:
https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-factory-autowire
spring懶加載
官網已經解釋的非常清楚了:
https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-factory-lazy-init
值得提醒的是,如果你想爲所有的對都實現懶加載可以使用官網的配置
springbean的作用域
文檔參考:
https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-factory-scopes
xml定義方式
<bean id="accountService" class="com.something.DefaultAccountService" scope="singleton"/>
annotation的定義方式
Singleton Beans with Prototype-bean Dependencies
意思是在Singleton 當中引用了一個Prototype的bean的時候引發的問題
官網引導我們參考
https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-factory-method-injection
spring聲明週期和回調
參考文檔:
https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-factory-lifecycle
1、Methods annotated with @PostConstruct
2、afterPropertiesSet() as defined by the InitializingBean callback interface
3、A custom configured init() method
Generating an Index of Candidate Components
加上這個依賴,可以在spring大型應用啓動的時候更快
他會在編譯的時候創建一個索引,啓動的時候直接根據索引去找相應的類,提高啓動速度