javascript
Spring核心技术之IOC容器(一):IOC容器与Bean简介
最近開始研究Spring框架,今天學習Spring的核心內容IOC 與 Bean
1. Spring IOC 與 Bean 簡介
?Inversion of Control?(IoC)即控制反轉,也叫dependency injection?(DI)依賴注入,Spring實現了一個基于配置文件的復雜工廠模式來提供實現控制反轉。
? org.springframework.beans?和org.springframework.context包是Spring中實現IOC的基礎包,其中BeanFactory接口提供一套基于配置文件來管理對象類型的機制,ApplicationContext接口繼承自BeanFactory接口,除了包含BeanFactory的所有功能之外,在國際化支持、資源訪問(如URL和文件)、事件傳播等方面進行了良好的支持,而WebApplicationContext是專門針對Web應用。
由Spring IOC管理、實例化、組裝的應用程序對象,稱為Bean,Bean與Bean之間的依賴關系存放于Spring配置元數據中。
2. Spring IOC容器
org.springframework.context.ApplicationContex接口代表IOC容器,根據配置元數據來實現Bean的初始化、配置和裝配,配置數據可以是XMl格式、Java注解格式或者Java代碼格式。
常見的實現ApplicationContext接口的類是ClassPathXMLApplicationContext和FileSystemXmlApplicationContext。
2.1 Spring IOC 配置實例
根據配置文件創建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容器創建對象
使用ApplicationContext接口中的 T getBean(String name, Class<T> requiredType)方法可以創建你想要的對象實例
// 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();事實上,我們的應用應該不要直接調用getBean方法,因此可以完全不依賴于Sping的API,比如Spring和Web集成框架可以直接將對象注入到web框架的Controller中。
3.Bean?
Spring IOC容器管理著很多的Bean,這些Bean是根據配置數據而創建的,但是對于Spring IOC容器本身來說,這些Bean的配置數據最終使用BeanDefinition對象來表示,一個BeanDefinition包含如下一些數據:bean對應的類的完全限定名、Bean的行為(范圍、生命周期等)、與其他Bean的依賴關系、其他配置信息。
Spring也允許手動加載外部對象,通過ApplicationContext的getBeanFactory方法獲取DefaultListableBeanFactory, 然后通過DefaultListableBeanFactory中的registerSingleton(...)或registerBeanDefinition(...)方法加載外部的對象。
每個Bean通常有一個標識符,所有的標識符在同一個容器內不能重復,xml配置中,可以用id或name類標識一個bean,id屬性只能設置一個確定的標識,而name屬性中可以有多個標識符,用逗號分好或空格隔開。你也可以不填id或name,那么容器會自動生成一個標識符給bean。有時候,各個子系統希望用不同的名字訪問同一個bean,那么你也可以使用<alias>標簽在bean的外部為他取一個別名,格式如下:
<alias name="subsystemA-dataSource" alias="subsystemB-dataSource"/> <alias name="subsystemA-dataSource" alias="myApp-dataSource" />Spring可以管理任何形式的Bean,不一定非要是標準的JavaBean。class屬性用來指定bean對應的class,如下:
<bean id="exampleBean" class="examples.ExampleBean"/> <bean name="anotherExample" class="examples.ExampleBeanTwo"/>3.1根據靜態工廠方法來創建bean
如果你的bean需要根據特定的靜態工廠方法來創建,那么你可以這樣配置,其中class值的工廠方法所在的class,而不是工廠方法返回值的class,factory-method指定靜態工廠方法名
<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根據實例工廠方法來創建bean
根據實例工廠方法創建bean的配置,它根據另一個bean來創建工廠方法對應的實例,使用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;} }?
轉載于:https://www.cnblogs.com/ArtofDesign/p/5605442.html
總結
以上是生活随笔為你收集整理的Spring核心技术之IOC容器(一):IOC容器与Bean简介的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenStack镜像制作笔记 --以
- 下一篇: 线性表实例分析