码迷,mamicode.com
首页 > 编程语言 > 详细

Spring配置文件浅析

时间:2016-11-16 03:20:02      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:spring配置文件详解

Spring的配置文件概述

Spring的配置文件是用于指导Spring工厂进行Bean的生产、依赖关系注入及Bean实例分发的"图纸",它是一个或多个标准的XML文档,J2EE程序员必须学会并灵活应用这份“图纸”,准确表达自己的"生产意图"

    Spring配置文件示例

   Spring配置文件的一般结构如下:       

 <beans>
     <import resource="resource1.xml"/>
     <import resource="resource2.xml"/>
     <bean id="bean1" class="**"></bean>
     <bean name="bean2" class=""></bean>
     <bean alias="bean3" name="bean2"/>
 </beans>


   Spring容器高层视图

    Spring容器启动基本条件

    Spring的框架类包

    Bean的配置信息

        xml bean,java configuration, autowire

    Bean的实现类


    Bean的元数据信息

        Bean的实现类

        Bean的属性信息

        Bean的依赖关系

        Bean的行为配置

        Bean的创建方式


  基于XMl的配置



SpringBean的命名


SpringBean的实例化

Spring Ioc容器如何实例化Bean呢?传统应用程序可以通过new和反射方式进行实例化Bean,而Spring IoC容器则需要根据Bean定义里的配置元数据使用反射机制来创建Bean.在Spring IoC容器中主要有以下几种创建Bean实例的方式:

    使用构造器实例化Bean

    构造器实例化Bean是最简单的方式,Spring IoC容器既能使用默认空构造器也能使用有构造器两种方式创建Bean,如下所示:

    1、空构造器实例化:

        <bean id="helloServiceNoWidthArgs" class="com.hello"/>

    2、有参数构造器实例化

        <bean id="helloServiceWithArgs" class="com.hello">

            <constructor-args index="0" value="hello Spring">

        </bean>


   使用静态工程方式实例化Bean

   使用静态工厂的方式除了指定必须的class属性,还要指定factory-method属性来指定实例化Bean的方法,而且使用静态工厂方法也允许指定方法参数,Spring IoC容器将调用此属指定的方法来获取Bean,配置如下:

    <bean id="helloService" class="com.hello" factory-method="newInstance">

        <constructor-args index="0" value="hello factory">

    </bean>


   使用实例工厂方法实例化Bean

    使用实例工厂方式不能指定class属性,此时必须使用factory-bean属性来指定工厂Bean,factory-method属性指定实例化Bean的方法,而且使用实例工厂方法允许指定方法参数,方式和使用构造器方式一样,

    定义实例工厂bean

    <bean id="beanInstanceFactory" class="com.helloService" />

    <bean id="helloOne" factory-bean="beanInstanceFactory" factory-method="newInstance">

        <constructor-arg index="0" value="hello"></constructor-arg>

    </bean>


SpringBean的作用域

Spring Bean中所说的作用域,在配置文件中即是"scope"。在面向对象程序设计中一般指对象或变量之间的可见范围。而在Spring容器中是指其创建的Bean对象相对于其他Bean对象的请求可见范围:

Bean的作用域类型与配置

    在Spring容器当中,一共提供了5种作用域类型,在配置文件中,通过属性scope来设置Bean的作用域范围。

        singleton: <bean id="userInfo" class="com.UserInfo" scope="singleton"></bean>

        prototype:<bean id="userinfo" class="com.Userinfo scope="prototype""></bean>

        request:<bean id="userinfo" class="com.Userinfo" scope="request"></bean>

        session:<bean id="userInfo" class="com.Userinfo" scope="session"></bean>

        global session: <bean id="userinfo" class="com.userinfo" scope="globalSession"></bean>

    

    singleton作用域是指在Spring IoC容器中仅存在一个Bean示例,Bean以单实例的方式存在,单实例模式是最重要的设计模式之一,在Spring中对此实现了超越,可以对那些非线程安全的对象采用单实例模式

    singleton配置方式

        <bean id="car" class="com.car" scope="singleton"></bean>

        <bean id="boss1" class="com.boss" p:car-ref="car"></bean>

        <bean id="boss2" class="com.boss" p:car-ref="car"></bean>

        <bean id="boss3" class="com.boss" p:car-ref="car"></bean>

    prototype作用域是指每次从容器中调用bean时,都返回一个新的实例,即每次调用getBean()时,相当于执行new Bean()的操作。在默认情况下,Spring容器在启动时不实例化prototype的Bean。

    prototype的配置方式

    <bean id="car" class="com.car" scope="prototype"></bean>

     <bean id="boss1" class="com.boss" p:car-ref="car"></bean>

     <bean id="boss2" class="com.boss" p:car-ref="car"></bean>

     <bean id="boss3" class="com.boss" p:car-ref="car"></bean>


    当用户使用Spring的webApplicationContext时,还可以使用另外3种Bean的作用域,即request,session和global session.在使用web应用环境相关的bean作用域时,必须在web容器中进行一些额外的配置:

        低版本web容器配置

        <filter>

                <filter-name></filter-name>

                <filter-class></filter-class>

        </filter>

        <filter-mapping>

                <filter-name></filter-name>

                <servlet-name></servlet-name>

        </filter-mapping>

    高版本web容器

        <listenner>

            <listener-class></listenner-class>

        </listener>

    web应用环境的作用域

        request作用域

        session作用域

        globalsession作用域

配置文件的整合

多个配置文件

    spring-common.xml位于 common文件夹下

    spring-connection.xml位于connection文件夹下

    spring-module.xml位于module文件夹下

传统加载方式:

    ApplicaitonContext context=new ClassPathXmlApplicationContext(new String[]{"Spring-common.xml","Spring-connection.xml","spring-module.xml"})

 配置整合文件

    spring-all-module.xml

    <beans>

        <import resource ="common/spring-common.xml"/>

        <import resource="connection/spring-connection.xml"/>

        <import resource="module/spring-module.xml"/>

    </beans>

    整合后加载方式:

        applicationCongtext context = new ClassPathXmlApplicationContext("spring-all-module.xml")


    






Spring配置文件浅析

标签:spring配置文件详解

原文地址:http://hanchengen.blog.51cto.com/8340379/1873242

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!