日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

二、IOC容器基本原理

發(fā)布時間:2024/8/26 综合教程 30 生活家
生活随笔 收集整理的這篇文章主要介紹了 二、IOC容器基本原理 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

IOC容器就是具有依賴注入功能的容器,IOC容器負(fù)責(zé)實例化、定位、配置應(yīng)用程序中的對象及建立這些對象間的依賴。應(yīng)用程序無需在代碼中new相關(guān)的對象,應(yīng)用程序由IOC容器進(jìn)行組裝。

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

寫個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,注意此處完全“面向接口編程,而不是面向?qū)崿F(xiàn)”  
                 helloInter helloApi = context.getBean("hello", helloInter.class);  
                  //3、執(zhí)行業(yè)務(wù)邏輯  
                  helloApi.sayHello();  
           }  

}

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

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

容器實現(xiàn)一覽:

XmlBeanFactory:BeanFactory實現(xiàn),提供基本的IoC容器功能,可以從classpath或文件系統(tǒng)等獲取資源;
(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實現(xiàn),從classpath獲取配置文件;
BeanFactory beanFactory = new ClassPathXmlApplicationContext("classpath.xml");

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

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

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

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

一、準(zhǔn)備配置文件:就像前邊Hello World配置文件一樣,在配置文件中聲明Bean定義也就是為Bean配置元數(shù)據(jù)。

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

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

轉(zhuǎn)自:http://jinnianshilongnian.iteye.com/blog/1413851

總結(jié)

以上是生活随笔為你收集整理的二、IOC容器基本原理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。