Spring中Ioc的兩種方式


1. 概述

控制反轉(Inversion of Control,Ioc)是指將創建對象的權利交給框架,它是框架的重要特徵,並非面向對象編程的專用術語。它主要包含依賴注入(Dependency Injection)和依賴查找(Dependency Lookup)。
在這裏插入圖片描述
其中依賴注入是被動的接收其依賴的其它組件被Ioc容器注入;而依賴查找是主動的去某個服務註冊地查找其依賴的那些服務。它們之間的關係如下所示:
在這裏插入圖片描述


2. Ioc依賴注入

Ioc的依賴注入可以分爲兩個流程:

  • 收集和註冊
  • 分析和組裝

2.1 收集和註冊

在這個階段中,Spring可以通過XML配置文件或是註解代碼的方式來定義一些Bean,然後通過手動組裝或者讓Ioc容器基於某些機制自動掃描的方式,將定義好的Bean收集到Ioc容器中。

例如,通過XML文件的形式配置<bean></bean>標籤來手動的收集並註冊Bean:

<?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
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="accountService" class="dyliang.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
    </bean>

    <bean id="accountDao" class="dyliang.dao.impl.IAccountDaoImpl">
        <property name="runner" ref="runner"></property>
    </bean>
</beans>

​ 通過@Component、@Repository、@Service、@Controller註解來讓Spring幫程序收集並註冊定義的Bean

@Service("accountService")
public class AccountServiceImpl implements IAccountService {}
@Repository("accountDao1")
public class AccountDaoImpl implements IAccountDao {}

如果嫌逐個收集 bean 定義麻煩,想批量地收集並註冊到 IoC 容器中,我們也可以通過 XML Schema 形式的配置進行批量掃描並採集和註冊:

<context:component-scan base-package="xxxx">

2.2 分析和組裝

經過收集和註冊階段得到的Ioc容器中的Bean,它們之間此時並沒有任何關係,但實際中不同Bean之間會存在某些依賴,這就需要分析和組裝階段來完成。

如果 IoC 容器發現某個 bean 依賴另一個 bean,它就會將這另一個 bean 注入給依賴它的那個 bean,直到所有 bean 的依賴都注入完成。當所有的bean都組裝結束,整個 IoC 容器的工作即算完成。

例如,XML文件中通過<bean>標籤內部的<property>標籤的ref屬性來指向另一個Bean,表示兩者之間的依賴關係。或者使用@Autowired、@Inject等註解在Bean對應的類內部定義依賴類的對象,讓Spring幫我們自動注入,完成依賴關係的綁定。

<bean id="accountService" class="dyliang.service.impl.AccountServiceImpl">
    <property name="accountDao" ref="accountDao"></property>
</bean>

<bean id="accountDao" class="dyliang.dao.impl.IAccountDaoImpl">
    <property name="runner" ref="runner"></property>
</bean>
@Service("accountService")
public class AccountServiceImpl implements IAccountService {

    @Autowired
    @Qualifier("accountDao")
    private IAccountDao accountDao;
}

3. Ioc依賴查找

在Spring項目的測試類中,可以通過如下代碼獲取XML配置文件形式對應的Ioc容器:

ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");

得到Ioc容器後通過getBean()方法來獲取對應的Bean的過程就是依賴查找,其中Bean()可以接受三種類型的參數

  • Bean_ID.class

    AccountService service = ac.getBean(AccountService.class);
    
  • Bean name

    AccountService service = ac.getBean("accountService");
    
  • Bean_ID.class, Bean name

    AccountService service = ac.getBean(AccountService.class, "accountService");
    

或者通過@ContextConfiguration註解來通過Spring自動獲取容器,然後在類中定義要使用的Bean對象,並通過@Autowired自動注入來獲取Bean,最後只需要使用Bean中相應的方法即可。

@ContextConfiguration(locations = "classpath:bean.xml")
public class AccountTest {
    
    @Autowired
    private IAccountService as;

    @Test
    public void testFindAll(){
        List<Account> all = as.findAll();
        for (Account account : all) {
            System.out.println(account);
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章