| 论坛注册| 加入收藏 | 设为首页| RSS
Google
您当前的位置:首页 > Linux频道 > Linux开发区 > WEB开发

Spring中bean的基本xml配置

时间:2007-09-21 12:33:15  来源:Linux联盟收集整理  作者:
在spring容器内拼凑bean叫作装配。装配bean的时候,你是在告诉容器,需要哪些bean,以及容器如何使用依赖注入将它们配合在一起。USVLinux联盟
    理论上,bean装配可以从任何资源获得,包括属性文件,关系数据库等,但xml是最常见的spring 应用系统配置源。Spring中的几种容器都支持使用xml装配bean,包括:USVLinux联盟
    XmlBeanFactory ,USVLinux联盟
    ClassPathXmlApplicationContext ,USVLinux联盟
    FileSystemXmlApplicationContext ,USVLinux联盟
    XmlWebApplicationContext

    基本的xml配置包括如下几个方面:USVLinux联盟
    USVLinux联盟
    1.添加一个beanUSVLinux联盟
    2.设置bean的属性USVLinux联盟
        2.1 手动设置USVLinux联盟
            2.1.1 通过Setter方法USVLinux联盟
            2.1.2 通过构造器USVLinux联盟
        2.2 自动设置USVLinux联盟
    其中bean的属性即为bean里的成员变量,这些成员变量值的获得可以通过setter方法,例如某个属性为name,则setter方法为setName(String name);或者通过构造器在类被实例化时初始化。Setter方法(例如setName方法)或者构造器的调用都可以通过在xml文件里进行配置,从而实现让spring容器来自动进行。
USVLinux联盟

1.添加一个beanUSVLinux联盟
    以下是一个例子:USVLinux联盟
    <bean USVLinux联盟
        id = “mybean”USVLinux联盟
        Class = “blog.spring.MyBean”USVLinux联盟
        Singleton = “false”USVLinux联盟
        init-method = “initMethod”USVLinux联盟
        destroy-method = “destroyMethod”USVLinux联盟
        autowire = “autowire type”USVLinux联盟
    />USVLinux联盟
    下面是对该标签里各个属性的解释:USVLinux联盟
    Id : 标识该bean的名称,通过factory.getBean(“id”)来获得实例。USVLinux联盟
    Class : 该bean的类路径。USVLinux联盟
    Singleton : 默认为true,即单实例模式,每次getBean(“id”)时获取的都是同USVLinux联盟
一个实例,如果设置为false,即原型模式,则每次获取的是新创建USVLinux联盟
的实例。USVLinux联盟
    Init-method : 在bean实例化后要调用的方法(bean里定义好的方法)。USVLinux联盟
    Destroy-method : bean从容器里删除之前要调用的方法。USVLinux联盟
    Autowire : 其属性要通过何种方法进行属性的自动装配。USVLinux联盟
    对于上述的各个属性,id和class是必要的,其他的则可以省略。例如如果设置了autowire的值,则表明需要自动装配,否则是手动装配。
USVLinux联盟

2.通过Setter方法手动设置bean里的属性USVLinux联盟
    Bean里的属性通过<property>标签来标识。有以下几种情况:USVLinux联盟
    ● 简单类型属性USVLinux联盟
        <bean id = “mybean” class = “blog.spring.MyBean”>USVLinux联盟
            <property name = “name”>USVLinux联盟
                <value>springTest</value>USVLinux联盟
            </property>USVLinux联盟
        </bean>USVLinux联盟
    ● 引用其他beanUSVLinux联盟
        <bean id = “mybean” class = “blog.spring.MyBean” />USVLinux联盟
        <bean id = “mybean1” class = “blog.spring.MyBean1”>USVLinux联盟
            <property name = “name”>USVLinux联盟
                <ref bean = “mybean” />USVLinux联盟
            </property>USVLinux联盟
        </bean>USVLinux联盟
也可以将<ref>改为USVLinux联盟
    <bean class = “..”>USVLinux联盟
这样叫做内部bean,缺点是无法在其他地方重用这个bean的实例。USVLinux联盟
    ● 装配集合USVLinux联盟
        共有以下几种集合的装配:USVLinux联盟
    ****装配List和数组****USVLinux联盟
        <property name = ”nameList”>USVLinux联盟
            <list>USVLinux联盟
                <value>something</value>USVLinux联盟
                <ref bean = “blog.spring.MyBean” />USVLinux联盟
                <value>otherThing</value>USVLinux联盟
            </list>USVLinux联盟
        </property>USVLinux联盟
    ****装配Set****USVLinux联盟
        <property name = ”nameList”>USVLinux联盟
            <set>USVLinux联盟
                <value>something</value>USVLinux联盟
                <ref bean = “blog.spring.MyBean” />USVLinux联盟
                <value>otherThing</value>USVLinux联盟
            </set>USVLinux联盟
        </property>USVLinux联盟
    ****装配Map****USVLinux联盟
        <property name = ”nameList”>USVLinux联盟
            <map>USVLinux联盟
                <entry key = “key1”>USVLinux联盟
                    <value>value1</value>USVLinux联盟
                </entry>USVLinux联盟
                <entry key = “key2”>USVLinux联盟
                    <ref bean = “mybean” />USVLinux联盟
                </entry>USVLinux联盟
            </map>USVLinux联盟
        </property>USVLinux联盟
    ****装配Properties****USVLinux联盟
        <property name = ”nameList”>USVLinux联盟
            <props>USVLinux联盟
                <prop key = “prop1”>value1</prop>USVLinux联盟
                <prop key = “prop2”>value2</prop>USVLinux联盟
            </props>USVLinux联盟
        </property>USVLinux联盟
    ● 设置nullUSVLinux联盟
        要将一个属性null,需要通过<null />标签,如果不设置,则属性为默认值(在实例化时)而不是null。USVLinux联盟
        <property name=”name”> <null /> </property>
USVLinux联盟

3.通过构造器手动设置bean里的属性USVLinux联盟
    假设有如下一个bean:USVLinux联盟
    Public class MyBean {USVLinux联盟
        Public MyBean( String arg1, MyBean1 arg2, String arg3 )USVLinux联盟
    }USVLinux联盟
则可以在xml里这样配置该bean:USVLinux联盟
<bean id = “mybean” class = “blog.spring.MyBean”>USVLinux联盟
        <constructor-arg index = “1”>USVLinux联盟
            <value>springTest</value>USVLinux联盟
        <constructor-arg>USVLinux联盟
        <constructor-arg index = “0”>USVLinux联盟
            <ref bean = “mybean1” />USVLinux联盟
        <constructor-arg>USVLinux联盟
</bean>USVLinux联盟
其中的index是用来标识该参数在构造函数里的位置的,并从0开始。
USVLinux联盟

4.让spring完成自动装配USVLinux联盟
    例如:USVLinux联盟
<bean USVLinux联盟
id = “mybean” USVLinux联盟
class = “blog.spring.MyBean”USVLinux联盟
autowire = “autowire type”USVLinux联盟
/>USVLinux联盟
下面是几种autowire type的说明:USVLinux联盟
● byname : 试图在容器中寻找和需要自动装配的属性名相同的bean或id,如果没有找到相应的bean,则这个属性未被装配上。USVLinux联盟
● byType : 试图在容器中寻找一个与需要自动装配的属性类型相同的bean或id,如果没有找到,则该属性未被装配上。USVLinux联盟
● constructor : 试图在容器中寻找与需要自动装配的bean的构造函数参数一致的一个或多个bean,如果没找到则抛出异常。USVLinux联盟
● autodetect : 首先尝试使用constructor来自动装配,然后再使用byType方式。USVLinux联盟
从上面可以看出,如果某个bean不手动设置autowire属性,则默认为手动装配。如果需要将所有bean都设置为自动装配时,可以通过在<beans>标签中设置default-autowire属性。<beans>标签是整个xml文档的根,在它下面就是一个个的<bean>。USVLinux联盟
其中default-autowire的值也有byName,byType,constructor,autodetect四种。USVLinux联盟
例如配置如下:USVLinux联盟
<beans default-autowire = “byName”>USVLinux联盟
    ...USVLinux联盟
</beans>
USVLinux联盟

    自动装配可能带来不确定性问题。例如使用byType时可能同时发现两个相同的类型,则不知道该采用哪一个。所以可能混合采用自动和手动装配。例如,对某个bean设置为自动装配,而对其某个属性则手动明确的设置其值,例如:USVLinux联盟
<bean id = “mybean” class = “blog.spring.MyBean”USVLinux联盟
    Autowire = “byType”USVLinux联盟
>USVLinux联盟
    <property name = “name”>USVLinux联盟
        <ref bean = “myBean1”>USVLinux联盟
    </property>USVLinux联盟
</bean>USVLinux联盟
通过这样的配置,对mybean里的name属性进行手动装配,而对除name外的其他属性就进行自动装配。
USVLinux联盟

USVLinux联盟
USVLinux联盟

来顶一下
近回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
相关文章
栏目更新
栏目热门