Wednesday, October 6, 2010

Spring v3.0.2 Learning Note 6 - Inject Components

注入依赖对象

基本类型对象注入:
<bean id="myId" class="xxx">
       <constructor-arg index="0" type="java.lang.String" value="aaa"/> //构造器注入
       <porperty name="name" value="test"/>  // 属性setter方式注入
</bean>

Ref
<bean id="personDao" class="com.spring.test.dao.PersonDao"/>
<bean id="personManager" class="com.spring.test.manager.PersonManager">
    <property name="personDao" ref="personDao"/>
</bean>

使用内部bean,但该bean不能被其他bean使用
<bean id="personManager" class="com.spring.test.manager.PersonManager">
     <property name="personDao">
           <bean class="com.spring.test.dao.PersonDao"/>
     </property>
</bean>

spring还提供了其他注入方式,比如基于接口的注入等,详见参考手册。

集合类型的注入

public class PersonManager {
     Set<String> sets =  new HashSet<String>();
     List<String> lists = new ArrayList<String>();
     Properties properties = new Properties();
     Map<String,String> maps = new HashMap<String,String>();
     // 省略setter方法,对于spring的组件注入,对应的setter方法是必须的
 }

  • 注入Set
    <bean id="personService" class="com.spring.test.manager.impl.PersonManager">
        <property name="sets"> // name是类中的属性名
            <set>
                <value>AAA</value>
                <value>BBB</value>
                <value>CCC</value>
            </set>
        </property>
    </bean>

测试类:
package com.spring.test.junit;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.test.manager.IPersonService;

public class PersonServiceTest {

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }

    @Test
    public void test() {
        AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(
                "spring.xml");
        IPersonService personService = (IPersonService) ctx
                .getBean("personService");
        for (String value : personService.getSets()) {
            System.out.println(value);
        }
    }
}
运行该测试类,可在控制台看到Set的内容。

  • 注入List
    <property name="lists"> // name是类中的属性名
            <list>
                <value>AAA</value>
                <value>BBB</value>
                <value>CCC</value>
            </list>
     </property>

  • 注入Properties
    <bean id="personService" class="com.spring.test.manager.impl.PersonManager">
        <property name="properties">
            <props>
                <prop key="key1">value1</prop>
                <prop key="key2">value2</prop>
                <prop key="key3">value3</prop>
            </props>
        </property>
    </bean>

测试类:
package com.spring.test.junit;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.test.manager.IPersonService;

public class PersonServiceTest {

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }

    @Test
    public void test() {
        AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(
                "spring.xml");
        IPersonService personService = (IPersonService) ctx
                .getBean("personService");
        for (Object key : personService.getProperties().keySet()) {
            System.out.println(personService.getProperties().getProperty(
                    (String) key));
        }
    }

}
运行该测试类,可在控制台看到注入的内容。

  • 注入Map
     <property name="maps">
            <map>
                <entry key="key1" value="value1"/>
                <entry key="key2" value="value2"/>
                <entry key="key3" value="value3"/>
            </map>
       </property>

No comments:

Post a Comment