IOC與DI的理解及使用
控制反轉IOC(Inversion of Control)是一種設計思想,DI(依賴注入)是實作IOC的一種方法,在沒有IOC的程式中,我們使用面向物件編程,物件的創建于物件間的依賴完全硬編碼在程式中,物件的創建有程式自己控制;控制反轉后將物件的創建轉移給第三方;
控制反轉是一種通過描述XML(或注解)并通過第三方去生產或獲取特定物件的方式,在Spring中實作控制反轉的是IOC容器,其實作方法是依賴注入(Dependency Injection,DI)
IOC演示:
-
UserDao介面
public interface UserDao { void getUser(); } -
UserDaoImpl實作類
public class UserDaoImpl implements UserDao{ public void getUser() { System.out.println("默認獲取用戶"); } } -
UserService業務介面
public interface UserService { void getUser(); void setUserDao(UserDao userDao); } -
UserServiceImpl業務實作類
public class UserServiceImpl implements UserService { private UserDao userDao ; //利用set進行動態實作值的注入 public void setUserDao(UserDao userDao){ this.userDao = userDao; } public void getUser() { userDao.getUser(); } }
這里使用一個Set方法實作
private UserDao userDao ;
//利用set進行動態實作值的注入
public void setUserDao(UserDao userDao){
this.userDao = userDao;
}
//代替了原先主動在該類中穿創建物件:
//userDao = new UserDao();
- 之前,程式是主動創建物件,控制權在開發者手上!
- 使用set注入后,程式不再具有主動性,而是變成了被動的接收物件!
- 這種物件的控制權的轉變思想就是IOC(控制反轉)
這種思想,從本質上解決了問題,程式員不用再去管理物件的創建了,系統的耦合性大大降低,可以更加專注在業務的實作上,這就是IOC的原型,
1、第一個spring
Spring的核心就是基于IOC容器,將物件的創建以及依賴的注入全部交由Spring容器來完成,
首先基于如上介面和業務創建兩個介面實作類:
public class UserDaoMysqlImpl implements UserDao{
public void getUser() {
System.out.println("mysql獲取用戶");
}
}
public class UserOracleImpl implements UserDao{
public void getUser() {
System.out.println("oracle獲取用戶");
}
}
創建applicationContext.xml檔案
基于XML的配置元資料的基本結構:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--一個bean就對應一個實體化一個物件,在Spring中管理-->
<bean id="mysqlImpl" class="com.spong.dao.UserDaoMysqlmpl"/>
<bean id="oracleImpl" class="com.spong.dao.UserOracleImpl"/>
<!--userServiceImpl實體注入ioc容器-->
<bean id="userServiceImpl" class="com.spong.service.UserServiceImpl">
<!--
ref:參考Spring容器中創建好的物件
value:具體的值,基本資料型別
-->
<!--給實體化物件中的屬性賦值,自動呼叫類中的setUserDao()方法,沒有或者方法名不規范則會出錯-->
<!--ref傳入哪個實作類的id,就呼叫userServiceImpl的set方法注入這個實作類-->
<property name="userDao" ref="mysqlImpl"/>
</bean>
</beans>
測驗:
public class MyTest {
public static void main(String[] args) {
//獲取Spring的背景關系物件ApplicationContext
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//物件已經在Spring中管理了,使用時直接根據bean id取即可
UserServiceImpl serviceImpl = (UserServiceImpl) context.getBean("userServiceImpl");
serviceImpl.getUser();
}
}
現在,我們徹底不用在程式中去改動了,要實作不同的操作,只需要在XML中進行修改;
這個程序就叫控制反轉:
- 控制:誰來控制物件的創建,傳統的應用程式是由程式本身控制創建的,使用Spring后,物件由Spring來創建;
- 反轉:程式本身不創建物件,而變成被動的接受物件;
- 依賴注入:就是利用set、構造方法等進行屬性的注入;
- 依賴:bean物件的創建依賴于容器!
- 注入:bean物件中的所有屬性,由容器來注入!
IOC是一種編程思想,由主動的編程變為被動的接收,
總結為一句話:物件由Spring來創建,管理,裝配,
2、IOC創建物件的方法
-
默認使用無參構造創建物件
-
自定義使用有參構造創建物件
-
下標賦值
<bean id="exampleBean" class="examples.ExampleBean"> <constructor-arg index="0" value=https://www.cnblogs.com/spang/p/"7500000"/> -
引數名賦值
<bean id="exampleBean" class="examples.ExampleBean"> <constructor-arg name="years" value=https://www.cnblogs.com/spang/p/"7500000"/> -
根據型別賦值(有同型別引數時則會有問題)
<bean id="exampleBean" class="examples.ExampleBean"> <constructor-arg type="int" value=https://www.cnblogs.com/spang/p/"7500000"/>
-
總結:在組態檔加載的時候,容器中管理的物件就已經初始化了,
3、DI(依賴注入)
IOC的一個重點是在系統運行中,動態的向某個物件提供它所需要的其他物件,這一點是通過DI(Dependency Injection,依賴注入)來實作的,
DI的實作原理是反射(reflection),它允許程式在運行的時候動態的生成物件、執行物件的方法、改變物件的屬性,Spring就是通過反射來實作注入的,
理解DI的關鍵是:“誰依賴誰,為什么需要依賴,誰注入誰,注入了什么”,那我們來深入分析一下:
-
誰依賴于誰:是應用程式依賴于IoC容器;
-
為什么需要依賴:應用程式需要IoC容器來提供物件需要的外部資源;
-
誰注入誰:是IOC容器注入應用程式依賴的某個物件;
-
注入了什么:就是注入某個物件所需要的外部資源(包括物件、資源、常量資料),
1、構造器注入
-
默認使用無參構造創建物件
-
自定義使用有參構造創建物件:
- 下標賦值
<bean id="exampleBean" class="examples.ExampleBean"> <constructor-arg index="0" value=https://www.cnblogs.com/spang/p/"7500000"/>- 引數名賦值
<bean id="exampleBean" class="examples.ExampleBean"> <constructor-arg name="years" value=https://www.cnblogs.com/spang/p/"7500000"/>- 根據型別賦值(有同型別引數時則會有問題)
<bean id="exampleBean" class="examples.ExampleBean"> <constructor-arg type="int" value=https://www.cnblogs.com/spang/p/"7500000"/>
*2、set方式注入
【環境搭建】
-
復雜型別
public class Address { private String address; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Address{" + "address='" + address + '\'' + '}'; } } -
測驗物件
@Data public class Student { private String name; private Address address; private String[] books; private List<String> hobbies; private Map<String,String> card; private Set<String> games; private String wife; //null private Properties info; } -
beans.xml
<bean id="address" class="com.spong.pojo.Address"> <property name="address" value=https://www.cnblogs.com/spang/p/"zhejiang"/>西游記 三國演義 紅樓夢 水滸傳 唱歌 打游戲 LOL CS 男 18 -
測驗類
public class MyTest2 { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Student student = (Student) context.getBean("student"); System.out.println(); } }
-
測驗結果
Student{ name='ps', address=Address{address='address'}, books=[西游記, 三國演義, 紅樓夢, 水滸傳], hobbies=[唱歌, 打游戲], card={學生證=147596654, 身份證=33216541236542512}, games=[LOL, CS], wife='null', info={性別=男, 年齡=18} }
總結:在組態檔加載的時候,容器中管理的物件就已經初始化了,
3、拓展
可以使用p命名空間和c命名空間進行注入:
p命名(properties)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--帶可選引數名稱的傳統宣告-->
<bean name="classic" class="com.example.ExampleBean">
<property name="email" value=https://www.cnblogs.com/spang/p/"[email protected]"/>
c命名(constructor)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="beanTwo" class="x.y.ThingTwo"/>
<bean id="beanThree" class="x.y.ThingThree"/>
<!-- 帶可選引數名稱的傳統宣告 -->
<bean id="beanOne" class="x.y.ThingOne">
<constructor-arg name="thingTwo" ref="beanTwo"/>
<constructor-arg name="thingThree" ref="beanThree"/>
<constructor-arg name="email" value=https://www.cnblogs.com/spang/p/"[email protected]"/>
注意:p命名和c命名空間不能直接使用,需要匯入xml約束
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
4、bean的作用域
| Scope | Description |
|---|---|
| singleton | (Default) Scopes a single bean definition to a single object instance for each Spring IoC container. |
| prototype | Scopes a single bean definition to any number of object instances. |
| request | Scopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext. |
| session | Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext. |
| application | Scopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext. |
| websocket | Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext. |
-
單例模式(Spring默認機制)
bean的一個共享實體,并且所有對具有ID或與該bean定義相匹配的ID的bean的請求都會導致該特定的bean實體由Spring容器回傳
<!--scope標簽修改作用域--> <bean id="accountService" class="com.something.DefaultAccountService" scope="singleton"/> -
原型模式:
每次對特定bean提出請求時,bean部署的非單一原型范圍都會創建一個新的bean實體
<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/> -
其余的request、session、application等只能在web開發中使用,對應的就是這三類作用域,
如有錯誤,歡迎大佬指正!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/86672.html
標籤:Java
上一篇:JVM記憶體區域
下一篇:基于注解的DI
