SpringFramework核心技术一(IOC容器:实例化一个容器)

实例化一个容器

实例化Spring IoC容器很简单。提供给ApplicationContext构造函数的位置路径实际上是资源字符串,它允许容器从各种外部资源(如本地文件系统,Java等)加载配置元数据CLASSPATH。


一、首先来搞一个例子

实例化一个容器:

services.xml是服务层的配置文件,daos.xml是数据访问文件。
按照下面的配置,就让容器将下面的两个XML文件中的bean配置到容器中。

ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");

1.1

以下示例显示服务层对象(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/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- services -->

    <bean id="petStore" class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl">
        <! -- 加载daos.xml中的bean ref 引用daos.xml中的id -->
        <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>

1.2

以下示例显示数据访问对象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>

1.3

在上面1.1和1.2中,服务层由实现类PetStoreServiceImpl、两个数据访问对象JpaAccountDao和JpaItemDao(基于JPA对象/关系映射标准)。
该property name元素引用JavaBean属性的名称,该ref元素引用另一个bean定义的名称。元素id和ref元素之间的这种联系表达了协作对象之间的依赖关系。


二、编写基于XML的配置元数据

2.1

让bean定义跨越多个XML文件可能很有用。通常,每个单独的XML配置文件都代表了架构中的逻辑层或模块。
您可以使用应用程序上下文构造函数从所有这些XML片段中加载bean定义。这个构造函数有多个Resource位置,如前一节所示。或者,使用一个或多个元素来从另一个或多个文件加载bean定义。例如:

<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

在前面的例子中,外部定义的Bean是从三个文件加载: services.xml,messageSource.xml,和themeSource.xml。所有位置路径都与导入的定义文件相关,因此services.xml必须位于与导入文件相同的目录或类路径位置, messageSource.xml而且themeSource.xml必须位于resources导入文件位置下方的位置。正如你所看到的,一个前导斜线被忽略,但是鉴于这些路径是相对的,最好不要使用斜线。根据Spring架构,正在导入的文件(包括顶层元素)的内容必须是有效的XML bean定义。

import指令是由bean名称空间本身提供的一项功能。除了普通bean定义以外的其他配置特性可以在Spring提供的XML命名空间中选择,例如“context”和“util”命名空间。

注意:

可能但不推荐使用相对的“../”路径引用父目录中的文件。这样做会创建对当前应用程序外部的文件的依赖关系。特别是,不建议将此引用用于“classpath:”URL(例如“classpath:../ services.xml”),其中运行时解析过程选择“最近”的类路径根,然后查看其父目录。类路径配置更改可能会导致选择不同的,不正确的目录。
您始终可以使用完全限定的资源位置而不是相对路径:例如,“file:C:/config/services.xml”或“c​​lasspath:/config/services.xml”。但是,请注意,您将应用程序的配置与特定绝对位置相关联。通常最好保持这种绝对位置的间接性,例如通过在运行时根据JVM系统属性解析的“$ {…}”占位符。

2.3Groovy Bean定义DSL

作为外部化配置元数据的另一个例子,bean的定义也可以在Spring的Groovy Bean Definition DSL中表示,如Grails框架中所知。通常,这样的配置将存在于“.groovy”文件中,结构如下:

beans {
    dataSource(BasicDataSource) {
        driverClassName = "org.hsqldb.jdbcDriver"
        url = "jdbc:hsqldb:mem:grailsDB"
        username = "sa"
        password = ""
        settings = [mynew:"setting"]
    }
    sessionFactory(SessionFactory) {
        dataSource = dataSource
    }
    myService(MyService) {
        nestedBean = { AnotherBean bean ->
            dataSource = dataSource
        }
    }
}

这种配置风格很大程度上等同于XML bean定义,甚至支持Spring的XML配置名称空间。它还允许通过“importBeans”指令导入XML bean定义文件。

扫描二维码关注公众号,回复: 876708 查看本文章

未完待续~~~~

猜你喜欢

转载自blog.csdn.net/wd2014610/article/details/80286575