Study Notes - Injecting various types of bean data

Learned today how to inject various types of beans

Look at the bean code first

AllKindsService is used to test the various types of beans injected

Employeel is the bean used to create objects for use by AllKindsService

package it.bean.assemble;
// This bean is used as an object for the next use (because some types can store object properties)

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;

//This class implements the injection of beans of various data types
import java.util.List;
import java.util.Map;
import java.util.Set;


public class AllKindsService {
private String name;// String type
private String[ ] empName;// array type
private List<Employeel> empList;// list type, save object
private Set<Employeel> empSet;// set type, save object
private Map<String,Employeel> empMaps;// map type, save object
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;
}

}

First post the code to instantiate AllKindsService

In the XML file :

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

In the TEST file :

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

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

Then paste the xml configuration file of the Employeel object

<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" />

Next, write each type of injection separately

Injection of String type:

XML file:

  <!--Inject values ​​into basic types-->

  <property name="name" value="Inject Basic Type 1"/>

TEST class file:

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

Array type injection

XML file:

<!--Inject values ​​into the array-->
  <property name="empName" >
    <list>
      <value>Array value 1</value>
      <value>Array value 2</value>
    <value>Array value 3</value> value>
    </list>
  </property>

TEST class file :

  //Output the injected array value
       for(String name:emp.getEmpName())
       {
       System.out.println("This is the value of the injected array: "+name);       

       }

List type injection

XML file:  

<!--Inject objects into list-->

  <property name="empList">
   <list>
      <ref bean="emp1"/>//Get the emp1 object, ref means reference, indicating that this object is referenced
      <ref bean="emp2"/>
       <ref bean="emp2" />
        <ref bean="emp1"/>
   </list>
  </property>

TEST class file:

  //Output the value of the injected list
       for(Employeel name:emp.getEmpList())
       {
       System.out.println("This is the name value of the object injected into the list: "+name.getName());

       }

Injection of set objects

XML file:

<!--Inject objects into the set, no repetition in the set-->
  <property name="empSet">
    <set>
       <ref bean="emp1"/>
       <ref bean="emp2"/>
       <ref bean= "emp1"/>
    </set>
  </property>

TEST class file:

//Output the value of the injected set
for(Employeel name:emp.getEmpSet()) {
System.out.println("This is the name value of the object of the injected set"+name.getName());
 

}

map method

XML file:

 <!--Inject key-value pairs into the map, the keys must be different, otherwise overwrite-->
  <property name="empMaps">
     <map>
     <entry key="11" value-ref="emp1"/>
     <entry key ="22" value-ref="emp2"/>
     
     </map>
 </property>  

TEST class file:

//输出注入的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 : Emphasize the knowledge of map here. The two parameters of Entry store the key and value respectively and encapsulate the Entry into an object

entryset: returns this 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()); 
}
}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325602185&siteId=291194637