Spring依赖注入的方式

晨曦之光 发布于 2012/04/25 16:18
阅读 1K+
收藏 0

Spring通过property(<property/>)和构造函数(<constructor-arg/>)对bean的依赖注入进行配置.
对String以及基本类型的注入:通过name/value的形式,如果是数字类型,boolean类型,value指定的值会被自动转换成期望的类型.
<constructor-arg/>与之类似,只是没有name指定参数名称,可以用index指定序号(从0开始).
<property name="XXX" value="YYY"/>
<constructor-arg index="0" value="1"/>
对对象类类型的注入:通过ref或者内部bean的形式.

<property name="refBean" ref="RefBean" />
<property name="target">
    <bean class="com.example.Person"> <!-- this is the inner bean -->
        <property name="name" value="Fiona Apple"/>
        <property name="age" value="25"/>
    </bean>
</property>
还可以用"P:"简写方式设置注入:注意必须放在<bean>内作为属性定义.
<bean name="TestBean" class="com.test.spring.di.cfg.TestBean" p:strVal="string value">
 <property name="refBean" ref="RefBean" /> == p:refBean-ref="RefBean">
对集合类型的注入:对应JAVA的List, Set, Map, and Properties.

<property name="listVal">
            <list>
                <value>a list element followed by a
                    reference</value>
                <ref bean="RefBean" />
            </list>
        </property>

        <property name="setVal">
            <set>
                <value>just some string</value>
                <ref bean="RefBean" />
            </set>
        </property>

        <property name="mapVal">
            <map>
                <entry key="an entry" value="just some string" />
                <entry key="a ref" value-ref="RefBean" />
            </map>
        </property>

        <property name="propVal">
            <props>
                <prop key="administrator">administrator@example.org</prop>
                <prop key="support">support@example.org</prop>
                <prop key="development">development@example.org</prop>
            </props>
        </property>
设置空字符串和null值:
<property name="email" value=""/> equals to XXX.setEmail("");
<property name="email"><null/></property> equals to XXX.setEmail(null);
一个完整的例子:

package com.test.spring.di.cfg;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class TestBean {
    private String strVal;
    private int intVal;
    private RefBean refBean;
    private List listVal;
    private Set setVal;
    private Map mapVal;
    private Properties propVal;

    public TestBean(List listVal) {
        this.listVal = listVal;
    }
    public TestBean(String strVal) {
        this.strVal = strVal;
    }
   
    public String getStrVal() {
        return strVal;
    }
    public void setStrVal(String strVal) {
        this.strVal = strVal;
    }
   
    public int getIntVal() {
        return intVal;
    }
    public void setIntVal(int intVal) {
        this.intVal = intVal;
    }
    public RefBean getRefBean() {
        return refBean;
    }
    public void setRefBean(RefBean refBean) {
        this.refBean = refBean;
    }
    public List getListVal() {
        return listVal;
    }
    public void setListVal(List listVal) {
        this.listVal = listVal;
    }
    public Set getSetVal() {
        return setVal;
    }
    public void setSetVal(Set setVal) {
        this.setVal = setVal;
    }
    public Map getMapVal() {
        return mapVal;
    }
    public void setMapVal(Map mapVal) {
        this.mapVal = mapVal;
    }
    public Properties getPropVal() {
        return propVal;
    }
    public void setPropVal(Properties propVal) {
        this.propVal = propVal;
    }
}

public class RefBean {
    public String testFunc(){
        return "Test Function";
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="RefBean" class="com.test.spring.di.cfg.RefBean" />

    <bean name="TestBean" class="com.test.spring.di.cfg.TestBean"p:strVal="string value">
        <constructor-arg index="0">
            <list>
                <value>a list element followed by a reference</value>
                <ref bean="RefBean" />
            </list>
        </constructor-arg>

        <property name="intVal" value="100" />
        <property name="refBean" ref="RefBean" />
        <property name="setVal">
            <set>
                <value>just some string</value>
                <ref bean="RefBean" />
            </set>
        </property>

        <property name="mapVal">
            <map>
                <entry key="an entry" value="just some string" />
                <entry key="a ref" value-ref="RefBean" />
            </map>
        </property>

        <property name="propVal">
            <props>
                <prop key="administrator">administrator@example.org
                </prop>
                <prop key="support">support@example.org</prop>
                <prop key="development">development@example.org</prop>
            </props>
        </property>
    </bean>

</beans>

Spring支持自动注入(autowiring),减少XML配置.
通过指定<beans>的default-autowire="xxx">其适用范围为该配置文件内的所有bean,注意不是整个context.
也可以对单个bean设定:<bean .... autowire="byName" />,如果是byName方式,property的名称和需要reference的bean的ID必须相同,
并且该property必须要有set方法.
使用@Autowired注解的默认是byType,注意一般来将整个context中必须有一个也只能有一个符合这种type的bean,如果context中有多个符合该类型的bean,Spring会看有没有bean的名称和该属性的名称相同,如果能匹配上,则用这个有着相同名称的bean赋值给该属性,否则抛异常.@Autowired注解的成员变量不需要set方法.
@Autowired注解还可以用在方法上,根据方法里面的参数进行匹配.常用的做法是在beans设定default-autowire="byName",把一些常要用的bean按默认名配置,如<bean id="dataSource",<bean id="transactionManager"等.对不经常变化的配置通过注解配置,@Component,@Transactional配合<context:component-scan base-package="com.test.spring.tx.annotation" />和<tx:annotation-driven/>来简化XML配置.
被依赖的bean将在依赖bean之前被适当的初始化。


原文链接:http://blog.csdn.net/kkdelta/article/details/5467127
加载中
返回顶部
顶部