spring学习笔记(9)

使用继承。在beans.xml里配置如下:

<bean abstract="true" id="parent">
<property name="abstractTest" value="XXX"/>
</bean>
将bean为userServiceBean的修改为:
<bean id="userServiceBean" class="com.neusoft.main.object.User" parent="parent">

新建AbstractUser.java
package com.neusoft.main.object;

public abstract class AbstractUser {

    private String abstractTest;

    public String getAbstractTest() {
        return abstractTest;
    }

    public void setAbstractTest(String abstractTest) {
        this.abstractTest = abstractTest;
    }
   
   
}
修改原User.java,让其继承AbstractUser,public class User extends AbstractUser
修改单元测试方法:
@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);
        }
        System.out.println("abstractTest:" + user.getAbstractTest());
    }

执行结果如下:

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 2
map keythree value 3
abstractTest:XXX


属性注入构造器方式:
新建Group.java类:
package com.neusoft.main.object;

import java.util.Set;

import com.neusoft.main.dao.UserDao;

public class Group {

    private String id;
   
    private UserDao userDao;
   
    private Set<String> set;
   
    public Group(String id,UserDao userDao,Set<String> set) {
        this.userDao = userDao;
        this.id = id;
        this.set = set;
    }

    public String getId() {
        return id;
    }

    public UserDao getUserDao() {
        return userDao;
    }

    public Set<String> getSet() {
        return set;
    }
   
   
}
beans.xml里配置:
<bean id="group" class="com.neusoft.main.object.Group">
<constructor-arg index="0" value="1"/>
<constructor-arg index="1" ref="userDaoImpl"/>
<constructor-arg index="2">
<set>
<value>groupSet1</value>
<value>groupSet2</value>
<value>groupSet3</value>
</set>
</constructor-arg>
</bean>

新增单元测试方法:
@Test
    public void baseGroup() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        Group group = (Group) ctx.getBean("group");
        System.out.println("id:" + group.getId());
        if(null != group.getUserDao()) {
            System.out.println("userDao is not null.");
        }
        for(Iterator<String> iter = group.getSet().iterator();iter.hasNext();) {
            System.out.println(iter.next());
        }
    }

执行结果如下:
factory2 born bean.
id:1
userDao is not null.
groupSet1
groupSet2
groupSet3

此方法会覆盖掉默认的构造方法,导致要依赖的配置不可用。因此我们要为Group类提供一个默认的构造方法:

    public Group() {
       
    }

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