spring学习笔记(8)

属性注入方式

1。settter注入
1)简单属性:
beans.xml配置如下:
<bean id="userServiceBean" class="com.neusoft.main.object.User">
<property name="id" value="100"/>
<property name="name" value="test"/>
</bean>

新增User.java:

package com.neusoft.main.object;

/**
* @author <a href="mailto:">hongchq </a>
* @version $Revision 1.1 $ 2010-6-1 上午11:07:01
*/
public class User {
   
    private String id;
   
    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
   
   

}
单元测试类中添加如下方法:
@Test
    public void baseObject() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        User user = (User) ctx.getBean("userServiceBean");
        System.out.println("id: "+user.getId());
        System.out.println("name:" + user.getName());
    }

执行结果如下:
2010-6-1 13:23:52 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@169e11: display name [org.springframework.context.support.ClassPathXmlApplicationContext@169e11]; startup date [Tue Jun 01 13:23:52 CST 2010]; root of context hierarchy
2010-6-1 13:23:52 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2010-6-1 13:23:52 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@169e11]: org.springframework.beans.factory.support.DefaultListableBeanFactory@fec107
2010-6-1 13:23:52 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@fec107: defining beans [userDaoImpl,factory,userDaoImpl3,userServiceBean]; root of factory hierarchy
factory2 born bean.
id: 100
name:test

2)对象属性-外部bean注入:在上面的bean中修改如下配置:
<bean id="userServiceBean" class="com.neusoft.main.object.User">
<property name="id" value="100"/>
<property name="name" value="test"/>
<property name="userDao" ref="userDaoImpl"/>
</bean>

修改User.java,增加UserDao属性:

package com.neusoft.main.object;

import com.neusoft.main.dao.UserDao;

/**
* @author <a href="mailto">hongchq </a>
* @version $Revision 1.1 $ 2010-6-1 上午11:07:01
*/
public class User {
   
    private String id;
   
    private String name;
   
    private UserDao userDao;

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
   
   

}

修改单元测试方法如下:

@Test
    public void baseObject() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        User user = (User) ctx.getBean("userServiceBean");
        System.out.println("id: "+user.getId());
        System.out.println("name:" + user.getName());
        if(null != user.getUserDao() ) {
            System.out.println("userdao is not null.");
        }
    }
执行结果如下:
2010-6-1 13:33:52 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@169e11: display name [org.springframework.context.support.ClassPathXmlApplicationContext@169e11]; startup date [Tue Jun 01 13:33:52 CST 2010]; root of context hierarchy
2010-6-1 13:33:52 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2010-6-1 13:33:52 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@169e11]: org.springframework.beans.factory.support.DefaultListableBeanFactory@132e13d
2010-6-1 13:33:52 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@132e13d: defining beans [userDaoImpl,factory,userDaoImpl3,userServiceBean]; root of factory hierarchy
factory2 born bean.
id: 100
name:test
userdao is not null.

3)对象属性-内部bean注入:
修改beans.xml
<property name="userDao1">
<bean id="temp" class="com.neusoft.main.dao.impl.UserDaoImpl" />
</property>
修改User.java:加入如下片段:
private UserDao userDao1;

    public UserDao getUserDao1() {
        return userDao1;
    }

    public void setUserDao1(UserDao userDao1) {
        this.userDao1 = userDao1;
    }

修改单元测试方法:

@Test
    public void baseObject() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        User user = (User) ctx.getBean("userServiceBean");
        System.out.println("id: "+user.getId());
        System.out.println("name:" + user.getName());
        if(null != user.getUserDao() ) {
            System.out.println("userdao is not null.");
        }
        if(null != user.getUserDao1()) {
            System.out.println("userdao1 is not null.");
        }
    }
执行单元测试方法结果如下:
2010-6-1 13:48:49 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@169e11: display name [org.springframework.context.support.ClassPathXmlApplicationContext@169e11]; startup date [Tue Jun 01 13:48:49 CST 2010]; root of context hierarchy
2010-6-1 13:48:50 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2010-6-1 13:48:50 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@169e11]: org.springframework.beans.factory.support.DefaultListableBeanFactory@132e13d
2010-6-1 13:48:50 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@132e13d: defining beans [userDaoImpl,factory,userDaoImpl3,userServiceBean]; root of factory hierarchy
factory2 born bean.
id: 100
name:test
userdao is not null.
userdao1 is not null.

集合属性注入:
修改User.java如下:
package com.neusoft.main.object;

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

import com.neusoft.main.dao.UserDao;

/**
* @author <a href="mailto:">hongchq </a>
* @version $Revision 1.1 $ 2010-6-1 上午11:07:01
*/
public class User {

    private String id;

    private String name;

    private UserDao userDao;

    private UserDao userDao1;

    private List<String> telephone;

    private Set<String> set;

    private Map<String, String> map;

    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    public Set<String> getSet() {
        return set;
    }

    public void setSet(Set<String> set) {
        this.set = set;
    }

    public List<String> getTelephone() {
        return telephone;
    }

    public void setTelephone(List<String> telephone) {
        this.telephone = telephone;
    }

    public UserDao getUserDao1() {
        return userDao1;
    }

    public void setUserDao1(UserDao userDao1) {
        this.userDao1 = userDao1;
    }

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

修改beans.xml如下:
<property name="telephone">
<list>
<value>02488796789</value>
<value>18678905656</value>
<value>04344456651</value>
</list>
</property>
<property name="set">
<set>
<value>set1</value>
<value>set2</value>
<value>set3</value>
</set>
</property>
<property name="map">
<map>
<entry key="one" value="1"/>
<entry key="two" value="3"/>
<entry key="three" value="3"/>
</map>
</property>

修改单元测试方法:
@Test
    public void baseObject() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        User user = (User) ctx.getBean("userServiceBean");
        System.out.println("id: "+user.getId());
        System.out.println("name:" + user.getName());
        if(null != user.getUserDao() ) {
            System.out.println("userdao is not null.");
        }
        if(null != user.getUserDao1()) {
            System.out.println("userdao1 is not null.");
        }
        for(int i = 0;i < user.getTelephone().size();i++) {
            System.out.println("List " +i + ":" + user.getTelephone().get(i));
        }
        for(Iterator<String> iter = user.getSet().iterator();iter.hasNext();) {
            System.out.println("set: " + iter.next());
        }
        for(Iterator<String> iter = user.getMap().keySet().iterator();iter.hasNext();) {
            String key = iter.next();
            String value = user.getMap().get(key);
            System.out.println("map key" + key + " value " + value);
        }
    }

执行结果如下
factory2 born bean.
id: 100
name:test
userdao is not null.
userdao1 is not null.
List 0:02488796789
List 1:18678905656
List 2:04344456651
set: set1
set: set2
set: set3
map keyone value 1
map keytwo value 3
map keythree value 3

你可能感兴趣的:(DAO,spring,bean,xml,单元测试)