学习笔记-注入各种类型的bean数据

今天学习了如何注入各种类型的bean

先看bean代码

AllKindsService是用来测试注入的各种类型的bean

Employeel是用来创建对象供AllKindsService使用的bean

package it.bean.assemble;
//这个bean的作用是作为对象供下一个使用(因为有的类型可以从存对象属性)

public class Employeel {
private String name;
private int id;


public int getId() {
return id;
}


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


public String getName() {
return name;
}


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

}


package it.bean.assemble;

//这个类来实现各种数据类型bean的注入
import java.util.List;
import java.util.Map;
import java.util.Set;


public class AllKindsService {
private String name;// 字符串类型
private String[] empName;// 数组类型
private List<Employeel> empList;// list类型,存对象
private Set<Employeel> empSet;// set类型,存对象
private Map<String,Employeel> empMaps;// map类型,存对象
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getEmpName() {
return empName;
}
public void setEmpName(String[] empname) {
this.empName = empname;
}
public List<Employeel> getEmpList() {
return empList;
}
public void setEmpList(List<Employeel> empList) {
this.empList = empList;
}
public Set<Employeel> getEmpSet() {
return empSet;
}
public void setEmpSet(Set<Employeel> empSet) {
this.empSet = empSet;
}
public Map<String, Employeel> getEmpMaps() {
return empMaps;
}
public void setEmpMaps(Map<String, Employeel> empMaps) {
this.empMaps = empMaps;
}

}

先贴出实例化AllKindsService的代码

XML文件中:

<bean id="allkindbean" class="it.bean.assemble.AllKindsService">

TEST文件中:

   ApplicationContext ac=new ClassPathXmlApplicationContext("it/bean/assemble/beans.xml");

   AllKindsService emp=(AllKindsService) ac.getBean("allkindbean");

再贴出来Employeel对象的xml配置文件

<bean id="emp1" class="it.bean.assemble.Employeel" >
<property name="name" value="北京" />
<property name="id" value="1" />
</bean>


<bean id="emp2" class="it.bean.assemble.Employeel">
<property name="name" value="天津" />
<property name="id" value="2" />

接下来分开写每一种类型的注入

String类型的注入:

XML文件:

  <!--给基本类型注入值-->

  <property name="name" value="注入基本类型1"/>

TEST类文件:

 System.out.println(emp.getName());

数组类型的注入

XML文件:

<!--给数组注入值-->
  <property name="empName" >
    <list>
      <value>数组值1</value>
      <value>数组值2</value>
    <value>数组值3</value>
    </list>
  </property>

TEST类文件

  //输出注入的数组值
       for(String name:emp.getEmpName())
       {
       System.out.println("这是注入数组的值:"+name);       

       }

List类型的注入

XML文件:  

<!--给list注入对象-->

  <property name="empList">
   <list>
      <ref bean="emp1"/>//获取emp1对象  ,ref表示引用,说明引用这个对象
      <ref bean="emp2"/>
       <ref bean="emp2"/>
        <ref bean="emp1"/>
   </list>
  </property>

TEST类文件:

  //输出注入的list的值
       for(Employeel name:emp.getEmpList())
       {
       System.out.println("这是注入list的对象的name值: "+name.getName());

       }

set对象的注入

XML文件:

<!--给set注入对象,set中不能有重复-->
  <property name="empSet">
    <set>
       <ref bean="emp1"/>
       <ref bean="emp2"/>
       <ref bean="emp1"/>
    </set>
  </property>

TEST类文件:

//输出注入的set的值
for(Employeel name:emp.getEmpSet()) {
System.out.println("这是注入的set的对象的name值"+name.getName());
 

}

map方法

XML文件:

 <!--给map注入键值对,key必须不同,否则覆盖-->
  <property name="empMaps">
     <map>
     <entry key="11" value-ref="emp1"/>
     <entry key="22" value-ref="emp2"/>
     
     </map>
 </property>  

TEST类文件:

//输出注入的map键值对(
//1.使用for循环
for(Entry<String,Employeel> entry:emp.getEmpMaps().entrySet())
{
System.out.println(entry.getKey()+"   "+entry.getValue());
 
}
//利用迭代器
Iterator<Map.Entry<String,Employeel>> it =emp.getEmpMaps().entrySet().iterator();
while(it.hasNext())
{
Entry<String,Employeel> ep=it.next();
System.out.println(ep.getKey()+"   "+ep.getValue()); 
}

tips:这里强调下map的知识 Entry两个参数分别存储了键和值并将Entry封装成一个对象

entryset:返回这个 Set<Map.Entry<K,V>> 类型的set集合

最后贴出总代码

XML:<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">




<bean id="emp1" class="it.bean.assemble.Employeel" >
<property name="name" value="北京" />
<property name="id" value="1" />
</bean>


<bean id="emp2" class="it.bean.assemble.Employeel">
<property name="name" value="天津" />
<property name="id" value="2" />
</bean>


<!--给AllKindsService注入值-->
<bean id="allkindbean" class="it.bean.assemble.AllKindsService">
  <!--给基本类型注入值-->
  <property name="name" value="注入基本类型1"/>
  
  <!--给数组注入值-->
  <property name="empName" >
    <list>
      <value>数组值1</value>
      <value>数组值2</value>
    <value>数组值3</value>
    </list>
  </property>




  <!--给list注入对象-->
  <property name="empList">
   <list>
      <ref bean="emp1"/>
      <ref bean="emp2"/>
       <ref bean="emp2"/>
        <ref bean="emp1"/>
   </list>
  </property>




  <!--给set注入对象,set中不能有重复-->
  <property name="empSet">
    <set>
       <ref bean="emp1"/>
       <ref bean="emp2"/>
       <ref bean="emp1"/>
    </set>
  </property>
  
  <!--给map注入键值对,key必须不同,否则覆盖-->
  <property name="empMaps">
     <map>
     <entry key="11" value-ref="emp1"/>
     <entry key="22" value-ref="emp2"/>
     
     </map>
 </property>  
</bean>


</beans>

Test:package it.bean.assemble;


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


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Test {


public static void main(String[] args) {
   ApplicationContext ac=new ClassPathXmlApplicationContext("it/bean/assemble/beans.xml");
   AllKindsService emp=(AllKindsService) ac.getBean("allkindbean");
  //输出注入的基础类型的值
   System.out.println(emp.getName());
  //输出注入的数组值
       for(String name:emp.getEmpName())
       {
       System.out.println("这是注入数组的值:"+name);       
       }
      //输出注入的list的值
       for(Employeel name:emp.getEmpList())
       {
       System.out.println("这是注入list的对象的name值: "+name.getName());
       }


//输出注入的set的值
for(Employeel name:emp.getEmpSet()) {
System.out.println("这是注入的set的对象的name值"+name.getName());
 
}
 
//输出注入的map键值对(
//1.使用for循环
for(Entry<String,Employeel> entry:emp.getEmpMaps().entrySet())
{
System.out.println(entry.getKey()+"   "+entry.getValue());
 
}
//利用迭代器
Iterator<Map.Entry<String,Employeel>> it =emp.getEmpMaps().entrySet().iterator();
while(it.hasNext())
{
Entry<String,Employeel> ep=it.next();
System.out.println(ep.getKey()+"   "+ep.getValue()); 
}
}
}

猜你喜欢

转载自blog.csdn.net/sunmeok/article/details/80201329
今日推荐