二、IOC容器基本原理
IOC容器就是具有依賴注入功能的容器,IOC容器負責實例化、定位、配置應用程序中的對象及建立這些對象間的依賴。應用程序無需在代碼中new相關的對象,應用程序由IOC容器進行組裝。
spring IOC管理的對象,我們稱為bean。bean就是spring容器初始化,裝配,及管理的對象,除此之外,bean就與應用程序中的其他對象沒有什么區(qū)別。那IOC如何實例化bean、管理bean之間的依賴關系?這些就需要配置元數(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,注意此處完全“面向接口編程,而不是面向實現(xiàn)”
helloInter helloApi = context.getBean("hello", helloInter.class);
//3、執(zhí)行業(yè)務邏輯
helloApi.sayHello();
}
}
運行,可以看出輸出一個hello World。
在spring IOC容器的代表就是org.springframework.beans包中的BeanFactory接口,BeanFactory接口提供了IOC容器最基本功能,而org.springframework.context包下的ApplicationContext接口擴展了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,客戶端需要自己進行類型轉換;
? T getBean(String name, Class<T> requiredType) 根據(jù)名稱和指定的類型返回一個Bean,客戶端無需自己進行類型轉換,如果類型轉換失敗,容器拋出異常;
? T getBean(Class<T> requiredType) 根據(jù)指定的類型返回一個Bean,客戶端無需自己進行類型轉換,如果沒有或有
多于一個Bean存在容器將拋出異常;
? Map<String, T> getBeansOfType(Class<T> type) 根據(jù)指定的類型返回一個鍵值為名字和值為Bean對象的 Map,
如果沒有Bean對象存在則返回空的Map。
讓我們來看下IoC容器到底是如何工作。在此我們以xml配置方式來分析一下:
一、準備配置文件:就像前邊Hello World配置文件一樣,在配置文件中聲明Bean定義也就是為Bean配置元數(shù)據(jù)。
二、由IoC容器進行解析元數(shù)據(jù): IoC容器的Bean Reader讀取并解析配置文件,根據(jù)定義生成BeanDefinition配置元數(shù)據(jù)對象,IoC容器根據(jù)BeanDefinition進行實例化、配置及組裝Bean。
三、實例化IoC容器:由客戶端實例化容器,獲取需要的Bean。
轉自:http://jinnianshilongnian.iteye.com/blog/1413851
總結
以上是生活随笔為你收集整理的二、IOC容器基本原理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dml语句和ddl语句 区别
- 下一篇: 电脑还原系统方法步骤详解如何使电脑系统还