spring 05-Spring框架依赖集合注入

Posted 广州富哥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring 05-Spring框架依赖集合注入相关的知识,希望对你有一定的参考价值。

实现普通数组的注入

定义一个Dept类

package cn.liang.vo;
import java.util.List;
public class Dept {
  private Integer deptno ;
  private String dname ; 
  private String loc ;
  private List<String> group;
  
  public List<String> getGroup() {
      return group;
  }

  public void setGroup(List<String> group) {
      this.group = group;
  }

  public Integer getDeptno() {
      return deptno;
  }

  public void setDeptno(Integer deptno) {
      this.deptno = deptno;
  }

  public String getDname() {
      return dname;
  }

  public void setDname(String dname) {
      this.dname = dname;
  }

  public String getLoc() {
      return loc;
  }

  public void setLoc(String loc) {
      this.loc = loc;
  }

  @Override
  public String toString() {
      return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + ", group=" + group + "]";
  }
  
}

修改applicationContext.xml文件,进行Dept类对象的定义

  • 在Spring里面吧数组和List作为同等的对待
  • 使用array标签定义groups
<bean id="dept" class="cn.liang.vo.Dept">
  <property name="deptno" value="10"/>
  <property name="dname" value="Development department"/>
  <property name="loc" value="Guangzhou"/>
  <property name="groups">
      <array value-type="java.lang.String">
          <value>groupOne</value>
          <value>groupTwo</value>
      </array>
  </property>
</bean>
  • 使用list标签定义groups
<bean id="dept" class="cn.liang.vo.Dept">
  <property name="deptno" value="10"/>
  <property name="dname" value="Development department"/>
  <property name="loc" value="Guangzhou"/>
  <property name="groups">
      <list value-type="java.lang.String">
          <value>groupOne</value>
          <value>groupTwo</value>
      </list>
  </property>
</bean>

测试程序

package cn.liang.test;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.liang.service.IMessageService;
import cn.liang.vo.Dept;
import cn.liang.vo.Emp;
import junit.framework.TestCase;
public class TestMessageService2 {
  private static ApplicationContext ctx = null ;
  static {
      ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
  }
  @Test
  public void testDeptConstructor() {
      Dept dept = (Dept) ctx.getBean("dept",Dept.class) ;
      Logger.getLogger(TestMessageService.class).info(dept); 
 Logger.getLogger(TestMessageService.class).info(dept.getGroups().getClass().getName()); 
  }
}

输出结果

2018-11-29 17:01:45,747 INFO [cn.liang.test.TestMessageService] - Dept [deptno=10, dname=Development department, loc=Guangzhou, groups=[groupOne, groupTwo]]
2018-11-29 17:01:45,751 INFO [cn.liang.test.TestMessageService] - java.util.ArrayList

实现set集合的注入

定义一个Dept类

private Set<String> groups;
public Set<String> getGroups() {
  return groups;
}
public void setGroups(Set<String> groups) {
  this.groups = groups;
}

修改applicationContext.xml文件,进行Dept类对象的定义

  • 使用set标签定义groups
<bean id="dept" class="cn.liang.vo.Dept">
  <property name="deptno" value="10"/>
  <property name="dname" value="Development department"/>
  <property name="loc" value="Guangzhou"/>
  <property name="groups">
      <set value-type="java.lang.String">
          <value>groupOne</value>
          <value>groupTwo</value>
      </set>
  </property>
</bean>

测试程序testDeptConstructor,输出结果

2018-11-29 17:03:56,386 INFO [cn.liang.test.TestMessageService] - Dept [deptno=10, dname=Development department, loc=Guangzhou, groups=[groupOne, groupTwo]]
2018-11-29 17:03:56,387 INFO [cn.liang.test.TestMessageService] - java.util.LinkedHashSet

实现Map集合的注入

定义一个Dept类

private Map<Integer, String> groups;
public Map<Integer, String> getGroups() {
  return groups;
}
public void setGroups(Map<Integer, String> groups) {
  this.groups = groups;
}

修改applicationContext.xml文件,进行Dept类对象的定义

  • 使用map标签定义groups
<bean id="dept" class="cn.liang.vo.Dept">
  <property name="deptno" value="10"/>
  <property name="dname" value="Development department"/>
  <property name="loc" value="Guangzhou"/>
  <property name="groups">
      <map>
          <entry key="110" value="GroupsOne"/>
          <entry key="120" value="GroupsTwo"/>
      </map>
  </property>
</bean>

测试程序testDeptConstructor,输出结果

2018-11-29 17:13:21,272 INFO [cn.liang.test.TestMessageService] - Dept [deptno=10, dname=Development department, loc=Guangzhou, groups={110=GroupsOne, 120=GroupsTwo}]
2018-11-29 17:13:21,272 INFO [cn.liang.test.TestMessageService] - java.util.LinkedHashMap

实现Properties的注入

定义一个Dept类

private Properties groups;
public Properties getGroups() {
  return groups;
}
public void setGroups(Properties groups) {
  this.groups = groups;
}

修改applicationContext.xml文件,进行Dept类对象的定义

  • 使用props标签定义groups
<bean id="dept" class="cn.liang.vo.Dept">
  <property name="deptno" value="10"/>
  <property name="dname" value="Development department"/>
  <property name="loc" value="Guangzhou"/>
  <property name="groups">
      <props>
          <prop key="110">GroupsOne</prop>
          <prop key="120">GroupsTwo</prop>
      </props>
  </property>
</bean>

测试程序testDeptConstructor,输出结果

2018-11-29 17:17:18,534 INFO [cn.liang.test.TestMessageService] - Dept [deptno=10, dname=Development department, loc=Guangzhou, groups={110=GroupsOne, 120=GroupsTwo}]
2018-11-29 17:17:18,535 INFO [cn.liang.test.TestMessageService] - java.util.Properties

实现集合之间的引用注入

定义一个Dept类

package cn.liang.vo;
import java.util.List;
public class Dept {
  private Integer deptno ;
  private String dname ; 
  private String loc ;
  private List<Emp> emps ;
  public void setEmps(List<Emp> emps) {
      this.emps = emps;
  }
  public List<Emp> getEmps() {
      return emps;
  }

  public Integer getDeptno() {
      return deptno;
  }

  public void setDeptno(Integer deptno) {
      this.deptno = deptno;
  }

  public String getDname() {
      return dname;
  }

  public void setDname(String dname) {
      this.dname = dname;
  }

  public String getLoc() {
      return loc;
  }

  public void setLoc(String loc) {
      this.loc = loc;
  }
  @Override
  public String toString() {
      return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + ", emps=" + emps + "]";
  }
}

修改applicationContext.xml文件,进行Dept类对象的定义

<bean id="emp" class="cn.liang.vo.Emp">
  <property name="empno" value="5678"/>
  <property name="ename" value="SMITH"/>
  <property name="sal" value="800.00"/>
</bean>
<bean id="dept" class="cn.liang.vo.Dept">
  <property name="deptno" value="10"/>
  <property name="dname" value="Development department"/>
  <property name="loc" value="Guangzhou"/>
  <property name="emps">
      <list>
          <ref bean="emp"/>   <!-- 表示引用一个其它定义的bean -->
          <!-- 描述的是一个内部的Bean,这个Bean只能够被这个Dept类定义使用 -->
          <bean class="cn.liang.vo.Emp">
              <property name="empno" value="1234"/>
              <property name="ename" value="Liang"/>
              <property name="sal" value="18000.00"/>
          </bean>
      </list>
  </property>
</bean>

测试程序

package cn.liang.test;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.liang.vo.Dept;
import cn.liang.vo.Emp;
import junit.framework.TestCase;
public class TestMessageService2 {
  private static ApplicationContext ctx = null ;
  static {
      ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
  }
  @Test
  public void testDeptConstructor() {
      Dept dept = (Dept) ctx.getBean("dept",Dept.class) ;
      Logger.getLogger(TestMessageService.class).info(dept);
      Logger.getLogger(TestMessageService.class).info(dept.getEmps().getClass().getName()); 
  }
}

输出结果

2018-11-29 17:29:11,596 INFO [cn.liang.test.TestMessageService] - Dept [deptno=10, dname=Development department, loc=Guangzhou, emps=[Emp [empno=5678, ename=SMITH, sal=800.0, dept=null], Emp [empno=1234, ename=Liang, sal=18000.0, dept=null]]]
2018-11-29 17:29:11,599 INFO [cn.liang.test.TestMessageService] - java.util.ArrayList

以上是关于spring 05-Spring框架依赖集合注入的主要内容,如果未能解决你的问题,请参考以下文章

05-spring-构造注入

框架----Spring中依赖注入

框架学习 Spring之依赖注入DI

如何正确使用 Spring 注入集合类型?

使用SMM框架开发企业级应用-----Spring集合注入和域属性自动注入byName和byType

Java面试官:如何正确使用 Spring 注入集合类型?