Spring——依赖注入类型
以例子说明:
一、新建Student类,声明各属性,创建成JavaBean:
1 package spring; 2 3 import java.util.List; 4 import java.util.Map; 5 import java.util.Properties; 6 import java.util.Set; 7 8 public class Student { 9 private int sid; 10 private String sname; 11 private String sex; 12 private StuClass stuClass; 13 14 private Set course; 15 private Set<StuClass> classSet; 16 17 private Map C2S; 18 19 private List score; 20 private List<StuClass> classList; 21 22 private Properties properties; 23 24 public int getSid() { 25 return sid; 26 } 27 28 public void setSid(int sid) { 29 this.sid = sid; 30 } 31 32 public String getSname() { 33 return sname; 34 } 35 36 public void setSname(String sname) { 37 this.sname = sname; 38 } 39 40 public String getSex() { 41 return sex; 42 } 43 44 public void setSex(String sex) { 45 this.sex = sex; 46 } 47 48 public StuClass getStuClass() { 49 return stuClass; 50 } 51 52 public void setStuClass(StuClass stuClass) { 53 this.stuClass = stuClass; 54 } 55 56 public Set getCourse() { 57 return course; 58 } 59 60 public void setCourse(Set course) { 61 this.course = course; 62 } 63 64 public Set<StuClass> getClassSet() { 65 return classSet; 66 } 67 68 public void setClassSet(Set<StuClass> classSet) { 69 this.classSet = classSet; 70 } 71 72 public Map getC2S() { 73 return C2S; 74 } 75 76 public void setC2S(Map c2s) { 77 C2S = c2s; 78 } 79 80 public List getScore() { 81 return score; 82 } 83 84 public void setScore(List score) { 85 this.score = score; 86 } 87 88 public List<StuClass> getClassList() { 89 return classList; 90 } 91 92 public void setClassList(List<StuClass> classList) { 93 this.classList = classList; 94 } 95 96 public Properties getProperties() { 97 98 Set<Object> keys = properties.keySet(); 99 for (Object key : keys) { 100 String value = properties.getProperty((String)key); 101 System.out.println("getProperties():" + key + " = "+value); 102 } 103 104 return properties; 105 } 106 107 public void setProperties(Properties properties) { 108 this.properties = properties; 109 } 110 111 @Override 112 public String toString() { 113 return "Student [sid=" + sid + ", sname=" + sname + ", sex=" + sex + ", stuClass=" + stuClass + ", course=" 114 + course + ", classSet=" + classSet + ", C2S=" + C2S + ", score=" + score + ", classList=" + classList 115 + ", properties=" + properties + "]"; 116 } 117 }
二、在xml文件中采取依赖注入的方式动态赋值:
1 <!-- 班级bean............开始 --> 2 <bean id="class1" class="spring.StuClass"> 3 <property name="cid" value="1000"></property> 4 <property name="cname" value="JAVA"></property> 5 </bean> 6 7 <bean id="class2" class="spring.StuClass"> 8 <property name="cid" value="2000"></property> 9 <property name="cname" value="C#"></property> 10 </bean> 11 12 <bean id="class3" class="spring.StuClass"> 13 <property name="cid" value="3000"></property> 14 <property name="cname" value="C++"></property> 15 </bean> 16 <!-- 班级bean............结束 --> 17 18 <!-- 学生bean --> 19 <bean id="zhangsan" class="spring.Student"> 20 <!-- 基本类型注入 --> 21 <property name="sid" value="001"></property> 22 <!-- <property name="sid"><value>001</value></property> --> 23 24 <!-- null类型注入 --> 25 <property name="sname"> 26 <!-- <value>null</value> 字符串 --> 27 <null></null><!-- null类型 --> 28 </property> 29 30 <!-- ""空字符串注入 --> 31 <property name="sex"> 32 <value></value> 33 </property> 34 35 <!-- 引用类型注入 --> 36 <property name="stuClass"> 37 <ref bean="class1"></ref> 38 </property> 39 40 <!-- set集合注入:无序无重复 --> 41 <property name="course"> 42 <set> 43 <value>java</value> 44 <value>php</value> 45 <value>c#</value> 46 <value>vb</value> 47 <value>c++</value> 48 <value>java</value><!-- 重复值 --> 49 </set> 50 </property> 51 <property name="classSet"> 52 <set> 53 <ref bean="class1"/> 54 <ref bean="class2"/> 55 <ref bean="class3"/> 56 </set> 57 </property> 58 <!-- Map集合注入 --> 59 <property name="C2S"> 60 <map> 61 <entry key="java" value="90"></entry> 62 <entry key="C#" value="99"></entry> 63 <entry key="C++" value="100"></entry> 64 </map> 65 </property> 66 <!-- list集合注入:有序有重复 --> 67 <property name="score"> 68 <list> 69 <value>77</value> 70 <value>88</value> 71 <value>99</value> 72 </list> 73 </property> 74 <property name="classList"> 75 <list> 76 <ref bean="class1"/> 77 <ref bean="class2"/> 78 <ref bean="class3"/> 79 </list> 80 </property> 81 <!-- Properties注入 --> 82 <property name="properties"> 83 <props> 84 <prop key="username">tomcat</prop> 85 <prop key="password">123456</prop> 86 </props> 87 </property> 88 </bean>