Spring之Chaper 4, The IoC container-4.2 Container view(容器总述)

来源:互联网 发布:windows选择哪个 编辑:程序博客网 时间:2024/04/28 19:30

<?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/beans        http://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>

4.2  Container View(容器概述)

       org.springframework.context.ApplicationContext接口代表Spring的IoC容器并且负责上面提及beans的实例化,配置和装配。容器通过读取配置元数据获取要实例化,配置和装配的对象的指令。可以通过XML,注解或者Java代码表示配置元数据。其允许你表达这样的对象来构成应用程序和这样对象之间的相互依赖。

      

        ApplicationContext接口的几个实现由Spring提供开箱即用。在独立的应用程序中,通常是创建一个ClassPathXmlApplicationContext或者ClassPathXmlApplicationContext的实例。其中XML是定义配置元数据的传统格式,你可以命令容器使用Java注解或者代码作为元数据格式,通过提供少量的XML配置来申明支持额外的元数据格式。

 

        在大多数应用程序场景中,显示的用户代码不要求实例化一个或者多个Spring IoC容器。例如,在一个web应用程序场景中,应用程序的web.xml文件中仅仅8行样板化的web描述符就能满足要求。如果使用SpringSource Tool Suite工具,容器生成样板化的配置。


         下面的图表是Spring如何工作的高级视图。你的应用程序类通过配置元数据联合,这样在创建和初始化ApplicationContext之后,你有一个全配置和可执行的系统或者应用程序。

         图 4-1 The Spring IoC Container

          



        

4.2.1 Configuration metadata

如同上图显示所示,IoC容器消费了配置元数据。这个配置元数据标示作为应用程序开发人员如何告诉IoC容器在你的应用程序中实例化,配置和装配对象。


配置元数据支持一个简单直观的XML格式,是这一章节传达了Spring IoC容器的关键概念和功能。
        注意:
                  基于XML的元数据不只是配置元数据的唯一形式。Spring IoC容器以基于XML的元数据配置是完全解耦的。如今,许多开发人员为其java应用程序选择基于Java的配置。

 另外,还有关于使用其他格式的元数据的信息:
        基于注解的配置:Spring 2.5 支持基于注解配置元数据
        基于Java配置:Spring 3.0 支持基于Java的配置。详情请见:@Configuration,@Bean,@Import,@DependsOn注解

Spring配置包含一个并且至少一个容器必须管理的bean定义。基于XML配置元数据显示了在元素<beans/>下的<bean/>中配置的bean。Java配置使用在一个以@Configuration注解的类内,以@Bean注解的方法上。


这些bean定义对应相应的实际对象,构建了你的应用程序。典型地,你定义了业务层对象,数据访问层(DAOs),表达对象,比如Struts Action实例,基础结构对象比如Hibernate的SessionFactoryies,JMS队列等等。一个在容器中不配置细粒度的领域对象,因为它通常由DAOs和业务逻辑来创建和加载领域对象。然而,你可以使用Spring集成AspectJ来配置对象,这些对象已经在IoC容器的控制外创建了。

下面的代码显示了基于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/beans           http://www.springframework.org/schema/beans/spring-beans.xsd">           <bean id="..." class="...">                      <!-- collaborators and configuration for this bean go here -->           <bean/>                       <bean id="..." class="...">                     <!-- collaborators and configuration for this bean go here -->           </bean>           <!-- more bean definitions go here --></beans>   

       
id属性是一个字符串,你用来标示唯一的bean定义。class属性定义了bean的类型并使用全限定类名。id属性值涉及到合作的对象。涉及到合作对象的XML没有在例子中显示:详情请看Dependencies。




4.2.2 Instantiaing a container(实例化一个容器)

实例化一个Spring IoC容器很简单。传递给ApplicationContext构造器参数的位置路径是资源字符串,允许容器从不同的资源中加载配置元数据,比如本地文件系统,从Java的CLASSPATH中,等等。

ApplicationContext context =     new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});

注意:在你了解了Spring的IoC容器后,你可能想更多了解Spring的资源抽象,其提供了方便的机制从指定文件路径中读取流。特别地,资源路径用于构造应用程序上下文。


下面的例子显示了业务层对象(servives.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/beans        http://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/beans        http://www.springframework.org/schema/beans/spring-beans.xsd">    <bean id="accountDao"        class="org.springframework.samples.jpetstore.dao.jpa.JpaAccountDao">        <!-- additional collaborators and configuration for this bean go here -->    </bean>    <bean id="itemDao" class="org.springframework.samples.jpetstore.dao.jpa.JpaItemDao">        <!-- additional collaborators and configuration for this bean go here -->    </bean>    <!-- more bean definitions for data access objects go here --></beans>


在前面的例子中,业务层由PetStoreServiceImpl类,和两个JpaAccountDao 和JpaItemDao 类型的数据访问对象组成。property name 元素与JavaBean属性的名字相关,ref元素与另一个bean定义的名字相关。id和ref元素这样的联合表达了合作对象之间的依赖。


Composing XML-based configuration metadata(基于XML配置的元数据)


定义跨越多个xml文件是很用的。一般每个独立的xml配置文件在你的结构中表示一个逻辑层或者模块。


你可以使用application Context构造器从xml片段中加载定义的beans。这个构造器需要多个Resource位置,如同前面显示的那样。作为可选地,使用一个或者多个<import/>元素从其他文件中加载定义的beans。例如:


<beans>    <import resource="services.xml"/>    <import resource="resources/messageSource.xml"/>    <import resource="/resources/themeSource.xml"/>    <bean id="bean1" class="..."/>    <bean id="bean2" class="..."/></beans>


在前面的例子中,多个定义的bean从这三个文件中加载:services.xml,messageSource.xml, 和themeSource.xml。所有的位置路径与导入的定义文件是相对的,这样service.xml必须与importing的文件在同一个目录或者类位置下,messageSource.xml, 和themeSource.xml必须位于resources所在的目录下。如你所见,忽略了斜线,所给的路径都是相对的,不用斜线是一中较好的形式。文件内容就被导入了,包括顶层元素<beans/>必须通过Spring语法校验。


        注意:


4.2.3 Using the container(使用容器)


ApplicationContext是一个高级工厂的接口,能够维护注册不同beans和其依赖。使用方法T getBean(String name, Class<T> requiredType) 你可以重新获得beans的实例。


ApplicationContext能使你读取bean和访问它们,如下所示:

// create and configure beansApplicationContext context =    new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});// retrieve configured instancePetStoreService service = context.getBean("petStore", PetStoreService.class);// use configured instanceList<String> userList = service.getUsernameList();


使用getBean()重新获得beans的实例。ApplicationContext接口还有其他的方法重新获取这些beans,但是理想情况下,你的应用程序代码应该从不会使用它们。

确实,你的应用程序代码应该从不调用getBean()方法,这样就与Spring的API没有依赖。例如,Spring与web框架的集成,对不同的web框架类提供了依赖注入,比如控制器和JSF-管理的bean。









0 0
原创粉丝点击