javascript
Spring核心技术之IOC容器(一):IOC容器与Bean简介
最近開始研究Spring框架,今天學(xué)習(xí)Spring的核心內(nèi)容IOC 與 Bean
1. Spring IOC 與 Bean 簡介
?Inversion of Control?(IoC)即控制反轉(zhuǎn),也叫dependency injection?(DI)依賴注入,Spring實現(xiàn)了一個基于配置文件的復(fù)雜工廠模式來提供實現(xiàn)控制反轉(zhuǎn)。
? org.springframework.beans?和org.springframework.context包是Spring中實現(xiàn)IOC的基礎(chǔ)包,其中BeanFactory接口提供一套基于配置文件來管理對象類型的機制,ApplicationContext接口繼承自BeanFactory接口,除了包含BeanFactory的所有功能之外,在國際化支持、資源訪問(如URL和文件)、事件傳播等方面進行了良好的支持,而WebApplicationContext是專門針對Web應(yīng)用。
由Spring IOC管理、實例化、組裝的應(yīng)用程序?qū)ο?#xff0c;稱為Bean,Bean與Bean之間的依賴關(guān)系存放于Spring配置元數(shù)據(jù)中。
2. Spring IOC容器
org.springframework.context.ApplicationContex接口代表IOC容器,根據(jù)配置元數(shù)據(jù)來實現(xiàn)Bean的初始化、配置和裝配,配置數(shù)據(jù)可以是XMl格式、Java注解格式或者Java代碼格式。
常見的實現(xiàn)ApplicationContext接口的類是ClassPathXMLApplicationContext和FileSystemXmlApplicationContext。
2.1 Spring IOC 配置實例
根據(jù)配置文件創(chuàng)建ApplicationContext:
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});services.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!-- services --><bean id="petStore"class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl"><property name="accountDao" ref="accountDao"/><property name="itemDao" ref="itemDao"/><!-- additional collaborators and configuration for this bean go here --></bean><!-- more bean definitions for services go here --></beans>daos.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="accountDao"class="org.springframework.samples.jpetstore.dao.ibatis.SqlMapAccountDao"><!-- additional collaborators and configuration for this bean go here --></bean><bean id="itemDao" class="org.springframework.samples.jpetstore.dao.ibatis.SqlMapItemDao"><!-- additional collaborators and configuration for this bean go here --></bean><!-- more bean definitions for data access objects go here --></beans>也可以用<import>標簽把多個配置文件引用到一個配置文件中,如下
<beans><import resource="services.xml"/><import resource="resources/messageSource.xml"/><import resource="/resources/themeSource.xml"/><bean id="bean1" class="..."/><bean id="bean2" class="..."/></beans>2.2.使用IOC容器創(chuàng)建對象
使用ApplicationContext接口中的 T getBean(String name, Class<T> requiredType)方法可以創(chuàng)建你想要的對象實例
// create and configure beans ApplicationContext context =new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});// retrieve configured instance PetStoreServiceImpl service = context.getBean("petStore", PetStoreServiceImpl.class);// use configured instance List userList = service.getUsernameList();事實上,我們的應(yīng)用應(yīng)該不要直接調(diào)用getBean方法,因此可以完全不依賴于Sping的API,比如Spring和Web集成框架可以直接將對象注入到web框架的Controller中。
3.Bean?
Spring IOC容器管理著很多的Bean,這些Bean是根據(jù)配置數(shù)據(jù)而創(chuàng)建的,但是對于Spring IOC容器本身來說,這些Bean的配置數(shù)據(jù)最終使用BeanDefinition對象來表示,一個BeanDefinition包含如下一些數(shù)據(jù):bean對應(yīng)的類的完全限定名、Bean的行為(范圍、生命周期等)、與其他Bean的依賴關(guān)系、其他配置信息。
Spring也允許手動加載外部對象,通過ApplicationContext的getBeanFactory方法獲取DefaultListableBeanFactory, 然后通過DefaultListableBeanFactory中的registerSingleton(...)或registerBeanDefinition(...)方法加載外部的對象。
每個Bean通常有一個標識符,所有的標識符在同一個容器內(nèi)不能重復(fù),xml配置中,可以用id或name類標識一個bean,id屬性只能設(shè)置一個確定的標識,而name屬性中可以有多個標識符,用逗號分好或空格隔開。你也可以不填id或name,那么容器會自動生成一個標識符給bean。有時候,各個子系統(tǒng)希望用不同的名字訪問同一個bean,那么你也可以使用<alias>標簽在bean的外部為他取一個別名,格式如下:
<alias name="subsystemA-dataSource" alias="subsystemB-dataSource"/> <alias name="subsystemA-dataSource" alias="myApp-dataSource" />Spring可以管理任何形式的Bean,不一定非要是標準的JavaBean。class屬性用來指定bean對應(yīng)的class,如下:
<bean id="exampleBean" class="examples.ExampleBean"/> <bean name="anotherExample" class="examples.ExampleBeanTwo"/>3.1根據(jù)靜態(tài)工廠方法來創(chuàng)建bean
如果你的bean需要根據(jù)特定的靜態(tài)工廠方法來創(chuàng)建,那么你可以這樣配置,其中class值的工廠方法所在的class,而不是工廠方法返回值的class,factory-method指定靜態(tài)工廠方法名
<bean id="clientService" class="examples.ClientService" factory-method="createInstance"/> public class ClientService {private static ClientService clientService = new ClientService();private ClientService() {}public static ClientService createInstance() {return clientService;} }3.2根據(jù)實例工廠方法來創(chuàng)建bean
根據(jù)實例工廠方法創(chuàng)建bean的配置,它根據(jù)另一個bean來創(chuàng)建工廠方法對應(yīng)的實例,使用factory-bean屬性來指定工廠方法所在的bean的名稱,如下:
<!-- the factory bean, which contains a method called createInstance() --> <bean id="serviceLocator" class="examples.DefaultServiceLocator"><!-- inject any dependencies required by this locator bean --> </bean> <!-- the bean to be created via the factory bean --> <bean id="clientService"factory-bean="serviceLocator"factory-method="createClientServiceInstance"/> public class DefaultServiceLocator {private static ClientService clientService = new ClientServiceImpl();private DefaultServiceLocator() {}public ClientService createClientServiceInstance() {return clientService;} }?
轉(zhuǎn)載于:https://www.cnblogs.com/ArtofDesign/p/5605442.html
總結(jié)
以上是生活随笔為你收集整理的Spring核心技术之IOC容器(一):IOC容器与Bean简介的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenStack镜像制作笔记 --以
- 下一篇: 线性表实例分析