二、IOC容器基本原理


IOC容器就是具有依賴注入功能的容器,IOC容器負責實例化、定位、配置應用程序中的對象及建立這些對象間的依賴。應用程序無需在代碼中new相關的對象,應用程序由IOC容器進行組裝。

spring IOC管理的對象,我們稱為bean。bean就是spring容器初始化,裝配,及管理的對象,除此之外,bean就與應用程序中的其他對象沒有什么區別。那IOC如何實例化bean、管理bean之間的依賴關系?這些就需要配置元數據。

寫個hello world(我用的是maven)

pxm.xml

<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>jdom</groupId>
    <artifactId>jdom</artifactId>
    <version>1.1</version>
</dependency>

helloInter.java

public interface helloInter {
           public void sayHello();  
}

helloImpl.java

public class helloImpl implements helloInter{

    public void sayHello() {
        // TODO Auto-generated method stub
        System.out.println("Hello World!!!");
    }

}

hello.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" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
    http://www.springframework.org/schema/context                http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <!-- id 表示你這個組件的名字,class表示組件類 -->
    <bean id="hello" class="com.lj.testspring.chapter.helloImpl"></bean>
</beans>  

helloTest.java

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class helloTest {
           @Test
           public void testHelloWorld() {  
                 //1、讀取配置文件實例化一個IoC容器  
                 ApplicationContext context = new ClassPathXmlApplicationContext("hello.xml");  
                 //2、從容器中獲取Bean,注意此處完全“面向接口編程,而不是面向實現”  
                 helloInter helloApi = context.getBean("hello", helloInter.class);  
                  //3、執行業務邏輯  
                  helloApi.sayHello();  
           }  

}

運行,可以看出輸出一個hello World。

在spring IOC容器的代表就是org.springframework.beans包中的BeanFactory接口,BeanFactory接口提供了IOC容器最基本功能,而org.springframework.context包下的ApplicationContext接口擴展了BeanFactory,還提供了與Spring AOP集成,國際化處理,事件傳播及提供不同層次的context實現。簡單說,BeanFactory提供了IOC容器最基本功能,而ApplicationContext則增加了更多支持企業級功能支持。ApplicationContext完全繼承BeanFactory,因而BeanFactory所具有的語義也適用於ApplicationContext。

容器實現一覽:

XmlBeanFactory:BeanFactory實現,提供基本的IoC容器功能,可以從classpath或文件系統等獲取資源;
(1) File file = new File("fileSystemConfig.xml");
Resource resource = new FileSystemResource(file);
BeanFactory beanFactory = new XmlBeanFactory(resource);
(2)
Resource resource = new ClassPathResource("classpath.xml");
BeanFactory beanFactory = new XmlBeanFactory(resource);

ClassPathXmlApplicationContext:ApplicationContext實現,從classpath獲取配置文件;
BeanFactory beanFactory = new ClassPathXmlApplicationContext("classpath.xml");

FileSystemXmlApplicationContext:ApplicationContext實現,從文件系統獲取配置文件。
BeanFactory beanFactory = new FileSystemXmlApplicationContext("fileSystemConfig.xml");

 

ApplicationContext接口獲取Bean方法簡介:
• Object getBean(String name) 根據名稱返回一個Bean,客戶端需要自己進行類型轉換;
• T getBean(String name, Class<T> requiredType) 根據名稱和指定的類型返回一個Bean,客戶端無需自己進行類型轉換,如果類型轉換失敗,容器拋出異常;

• T getBean(Class<T> requiredType) 根據指定的類型返回一個Bean,客戶端無需自己進行類型轉換,如果沒有或有
多於一個Bean存在容器將拋出異常;
• Map<String, T> getBeansOfType(Class<T> type) 根據指定的類型返回一個鍵值為名字和值為Bean對象的 Map,
如果沒有Bean對象存在則返回空的Map。

 

讓我們來看下IoC容器到底是如何工作。在此我們以xml配置方式來分析一下:

一、准備配置文件:就像前邊Hello World配置文件一樣,在配置文件中聲明Bean定義也就是為Bean配置元數據。

二、由IoC容器進行解析元數據: IoC容器的Bean Reader讀取並解析配置文件,根據定義生成BeanDefinition配置元數據對象,IoC容器根據BeanDefinition進行實例化、配置及組裝Bean。

三、實例化IoC容器:由客戶端實例化容器,獲取需要的Bean。

 

轉自:http://jinnianshilongnian.iteye.com/blog/1413851


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM