使用非字符串Map的构造函数注入示例(包含依赖对象)

在这个示例中,我们使用map作为包含Answer和User的答案。在这里,我们同时使用键值对作为对象。Answer有它自己的信息,比如answerId、answer和postedDate,User有它自己的信息,比如userId、username和emailId。

和之前的示例一样,这是一个论坛的示例,其中一个问题可以有多个答案

Question.java

这个类包含三个属性,两个构造函数和displayInfo()方法来显示信息。

package cn.javatiku;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

public class Question {
    private int id;
    private String name;
    private Map<Answer, User> answers;

    public Question() {}

    public Question(int id, String name, Map<Answer, User> answers) {
        super();
        this.id = id;
        this.name = name;
        this.answers = answers;
    }

    public void displayInfo() {
        System.out.println("question id: " + id);
        System.out.println("question name: " + name);
        System.out.println("Answers....");
        Set<Entry<Answer, User>> set = answers.entrySet();
        Iterator<Entry<Answer, User>> itr = set.iterator();
        while (itr.hasNext()) {
            Entry<Answer, User> entry = itr.next();
            Answer ans = entry.getKey();
            User user = entry.getValue();
            System.out.println("Answer Information:");
            System.out.println(ans);
            System.out.println("Posted By:");
            System.out.println(user);
        }
    }
}

Answer.java

package cn.javatiku;

import java.util.Date;

public class Answer {
    private int id;
    private String answer;
    private Date postedDate;

    public Answer() {}

    public Answer(int id, String answer, Date postedDate) {
        super();
        this.id = id;
        this.answer = answer;
        this.postedDate = postedDate;
    }

    public String toString() {
        return "Id: " + id + " Answer: " + answer + " Posted Date: " + postedDate;
    }
}

User.java

package cn.javatiku;

public class User {
    private int id;
    private String name, email;

    public User() {}

    public User(int id, String name, String email) {
        super();
        this.id = id;
        this.name = name;
        this.email = email;
    }

    public String toString() {
        return "Id: " + id + " Name: " + name + " Email Id: " + email;
    }
}

applicationContext.xml

在这个XML文件中,我们使用entry元素的key-refvalue-ref属性来定义map中bean的引用。

<?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="answer1" class="cn.javatiku.Answer">
        <constructor-arg value="1"></constructor-arg>
        <constructor-arg value="Java is a Programming Language"></constructor-arg>
        <constructor-arg value="12/12/2001"></constructor-arg>
    </bean>
    <bean id="answer2" class="cn.javatiku.Answer">
        <constructor-arg value="2"></constructor-arg>
        <constructor-arg value="Java is a Platform"></constructor-arg>
        <constructor-arg value="12/12/2003"></constructor-arg>
    </bean>

    <bean id="user1" class="cn.javatiku.User">
        <constructor-arg value="1"></constructor-arg>
        <constructor-arg value="Arun Kumar"></constructor-arg>
        <constructor-arg value="arun@gmail.com"></constructor-arg>
    </bean>
    <bean id="user2" class="cn.javatiku.User">
        <constructor-arg value="2"></constructor-arg>
        <constructor-arg value="Varun Kumar"></constructor-arg>
        <constructor-arg value="Varun@gmail.com"></constructor-arg>
    </bean>

    <bean id="q" class="cn.javatiku.Question">
        <constructor-arg value="1"></constructor-arg>
        <constructor-arg value="What is

Test.java

这个类从applicationContext.xml文件中获取bean,并调用displayInfo()方法来显示信息。

package cn.javatiku;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class Test {
    public static void main(String[] args) {
        Resource r = new ClassPathResource("applicationContext.xml");
        BeanFactory factory = new XmlBeanFactory(r);

        Question q = (Question) factory.getBean("q");
        q.displayInfo();
    }
}

标签: spring, Spring教程, Spring技术, Spring语言学习, Spring学习教程, Spring下载, Spring框架, Spring框架入门, Spring框架教程, Spring框架高级教程, Spring面试题, Spring笔试题, Spring编程思想