Spring_08_DI之Setter注入

DI

Deoendence inject,依赖注入。

把对象的依赖关系全部交给Spring容器处理。

DI之Setter注入

BeanObject

@Setter@ToString
public class BeanObject implements IBeanObject {
    /* 简单值 */
    private long sn;
    private String name;

    /* 依赖的bean对象 */
    private IOtherBean other;

    /* 集合 */
    private List list;
    private Set set;
    private Map map;

    /* Properties对象*/
    private Properties properties;
    
    public BeanObject(){
        System.out.println("new BeanObject");
    }
}

 

· 简单值注入

在<bean>中,配置<property>的name属性和value属性。

name为对象属性名,value为属性值。(Spring会自动转型)

 <bean id="beanObject" class="com.hanaii.di.setter.BeanObject">
        <property name="sn" value="1"/>
        <property name="name" value="hanaii"/>
    <!-- 其余无关代码略 -->
</bean>

 

· 依赖bean对象的注入

先对当前bean对象的所依赖的其它bean对象进行配置。

在当前bean对象的<bean>中,配置<property>的name属性和ref属性。

ref为依赖对象的id值或name值。

<bean id="otherBean" name="other" class="com.hanaii.di.setter.OtherBean"/>
<bean id="beanObject" class="com.hanaii.di.setter.BeanObject">
     <!-- 其余无关代码略 -->
    <property name="other" ref="otherBean"/>
</bean>

· 集合注入

1、List和Set

a、配置<bean>中<property>的name为集合属性名。

b、对<property>中<list>或<set>进行配置。

当值为bean对象时,配置<ref>标签。简单值对<value>标签进行配置。

另集合中还可配置集合,使用对应集合标签即可。

 <bean id="otherBean" name="other" class="com.hanaii.di.setter.OtherBean"/>
 <bean id="beanObject" class="com.hanaii.di.setter.BeanObject">
    <!--省略其他无关代码 -->
        <!-- List集合的配置 -->
        <property name="list">
            <list>
                <!-- 允许重复 -->
                <value>1</value>
                <value>1</value>
                <ref bean="otherBean"/>
            </list>
        </property>
        <!-- Set集合的配置 -->
        <property name="set">
            <set>
                <!-- 不允许重复 -->
                <value>1</value>
                <value>1</value>
                <value>a</value>
                <ref bean="other"/>
            </set>
        </property>
 </bean>

2、Map

a、配置<bean>中<property>的name为集合属性名。

b、对<property>的<map>中的<entry>或进行配置。

其中<entry>的key-ref属性和value-ref属性的值,为bean对象。

 <bean id="otherBean" name="other" class="com.hanaii.di.setter.OtherBean"/>
 <bean id="beanObject" class="com.hanaii.di.setter.BeanObject">
          <!--省略其余无关代码-->
          <property name="map">
            <map>       
                <entry key="1" value="a"/>
                <entry key-ref="otherBean" value="other"/>
                <entry key="3" value-ref="otherBean"/>
            </map>
        </property>
 </bean>

· Properties对象的注入

存在两种配置方式。

第一种是使用<pros>标签进行配置。该方式能正常识别中文。如下:

<bean id="beanObject" class="com.hanaii.di.setter.BeanObject">
    <!-- 省略其余无关代码 -->
     <property name="properties">
            <props>
                <prop key="show_sql" >true</prop>
                <prop key="system_language" >简体中文</prop>
            </props>
    </property>
</bean>

另一种是使用<value>注入,每一条property占一行,格式为key=value。

该方式不能正常识别中文。如下:

<bean id="beanObject" class="com.hanaii.di.setter.BeanObject">
    <property name="properties">
            <value>
                show_sql=true
                system_language=简体中文
                <!--结果查看最后测试-->
            </value>
    </property>
</bean>

按照上述配置进行测试

· 测试代码

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class SetterTest {

    @Autowired
    ApplicationContext ctx;

    @Test
    public void test1(){
        BeanObject obj = ctx.getBean("beanObject", BeanObject.class);
        System.out.println(obj);
    }
}

(其中properties的配置方式为value注入)

· 测试结果

BeanObject(sn=1, name=hanaii, other=com.hanaii.di.setter.OtherBean@5606c0b,
list=[1, 1, com.hanaii.di.setter.OtherBean@5606c0b], 
set=[1, a, com.hanaii.di.setter.OtherBean@5606c0b],
map={1=a,com.hanaii.di.setter.OtherBean@5606c0b=other,
3=com.hanaii.di.setter.OtherBean@5606c0b}, 
properties={system_language=????, show_sql=true})

· 补充

在必须依赖的属性的setter方法上,打上@Required标签,

若配置文件中没有对该属性的配置,在容器启动时则会报错。

猜你喜欢

转载自blog.csdn.net/hanaii/article/details/82527047