spring(二)
阿新 • • 發佈:2017-05-11
per 取出 keys str ans 繼承 分享 sta void
盡量使用 scope="singleton" ,不要使用prototype,因為對性能的影響較大
給集合類型註入值
Java中主要的集合有:map set list 數組
department類
1 package com.hsp.collection; 2 3 import java.util.List; 4 import java.util.Map; 5 import java.util.Set; 6 7 public class Department { 8 9 private String name; 10 11 12 privateString [] empName; 13 private List<Employee> empList; 14 private Set<Employee> empsets; 15 private Map<String,Employee> empMaps; 16 17 public Map<String, Employee> getEmpMaps() { 18 return empMaps; 19 } 20 public void setEmpMaps(Map<String, Employee> empMaps) {21 this.empMaps = empMaps; 22 } 23 public Set<Employee> getEmpsets() { 24 return empsets; 25 } 26 public void setEmpsets(Set<Employee> empsets) { 27 this.empsets = empsets; 28 } 29 public List<Employee> getEmpList() { 30 returnempList; 31 } 32 public void setEmpList(List<Employee> empList) { 33 this.empList = empList; 34 } 35 public String getName() { 36 return name; 37 } 38 public void setName(String name) { 39 this.name = name; 40 } 41 42 public String[] getEmpName() { 43 return empName; 44 } 45 public void setEmpName(String[] empName) { 46 this.empName = empName; 47 } 48 49 }
employee類
1 package com.hsp.collection; 2 3 public class Employee { 4 5 private String name; 6 private int id; 7 8 public int getId() { 9 return id; 10 } 11 12 public void setId(int id) { 13 this.id = id; 14 } 15 16 public String getName() { 17 return name; 18 } 19 20 public void setName(String name) { 21 this.name = name; 22 } 23 24 }
測試類
1 package com.hsp.collection; 2 3 import java.util.Iterator; 4 import java.util.Map; 5 import java.util.Map.Entry; 6 7 import javax.swing.text.html.parser.Entity; 8 9 import org.springframework.context.ApplicationContext; 10 import org.springframework.context.support.ClassPathXmlApplicationContext; 11 12 public class App1 { 13 14 /** 15 * @param args 16 */ 17 public static void main(String[] args) { 18 // TODO Auto-generated method stub 19 ApplicationContext ac = new ClassPathXmlApplicationContext("com/hsp/collection/beans.xml"); 20 Department department = (Department) ac.getBean("department"); 21 System.out.println(department.getName()); 22 for (String emName:department.getEmpName()){ 23 System.out.println(emName); 24 } 25 26 System.out.println("********通過list集合取出數據*********"); 27 for(Employee e: department.getEmpList()){ 28 System.out.println("name"+e.getName()+" "+e.getId()); 29 } 30 31 System.out.println("********通過set集合取出數據*********"); 32 for(Employee e: department.getEmpsets()){ 33 System.out.println("name"+e.getName()+" "+e.getId()); 34 } 35 36 37 System.out.println("********通過map集合取出數據 叠代器方法*********"); 38 Map<String, Employee> empmaps=department.getEmpMaps(); 39 Iterator it = empmaps.keySet().iterator(); 40 while (it.hasNext()) { 41 String key = (String) it.next(); 42 Employee emp = empmaps.get(key); 43 System.out.println("key="+key+" "+emp.getName()); 44 45 } 46 47 System.out.println("********通過map集合取出數據 簡潔方法*********"); 48 for(Entry <String,Employee> entry1:department.getEmpMaps().entrySet()){ 49 System.out.println(entry1.getKey()+" "+entry1.getValue().getName()); 50 } 51 } 52 53 }
beans.xml文件
1 <bean id="department" class="com.hsp.collection.Department"> 2 3 4 <property name="name" value="醫務室" /> 5 6 <!-- 對數組註入值 --> 7 <property name="empName"> 8 <list> 9 <value>張三</value> 10 <value>李四</value> 11 <value>王五</value> 12 </list> 13 </property> 14 15 <!-- 給list註入值 --> 16 <property name="empList"> 17 <list> 18 <ref bean="emp1"/> 19 <ref bean="emp2"/> 20 </list> 21 </property> 22 23 <!-- 給set註入值 --> 24 <property name="empsets"> 25 <set> 26 <ref bean="emp1"/> 27 <ref bean="emp2"/> 28 </set> 29 </property> 30 31 <!-- 給map註入值 --> 32 <property name="empMaps"> 33 <map> 34 <entry key="1" value-ref="emp1" /> 35 <entry key="2" value-ref="emp2" /> 36 </map> 37 </property> 38 </bean> 39 40 41 <bean id="emp1" class="com.hsp.collection.Employee"> 42 <property name="name" value="揚州"></property> 43 <property name="id" value="1"></property> 44 </bean> 45 <bean id="emp2" class="com.hsp.collection.Employee"> 46 <property name="name" value="武漢"></property> 47 <property name="id" value="2"></property> 48 </bean>
內部bean
<bean id="foo" class="…Foo">
<property name="屬性">
<!--第一方法引用-->
<ref bean ="bean 對象名"/>
<!--內部bean-->
<bean>
<property></property>
</bean>
</property>
</bean>
4、繼承配置
student類
1 public class Student { 2 3 protected String name; 4 protected int age; 5 6 7 public String getName() { 8 return name; 9 } 10 public void setName(String name) { 11 this.name = name; 12 } 13 public int getAge() { 14 return age; 15 } 16 public void setAge(int age) { 17 this.age = age; 18 } 19 20 }
Gradate類
1 public class Gradate extends Student { 2 3 private String degree; 4 5 public String getDegree() { 6 return degree; 7 } 8 9 public void setDegree(String degree) { 10 this.degree = degree; 11 } 12 13 }
測試類
1 public class App1 { 2 3 public static void main(String[] args){ 4 ApplicationContext ac = new ClassPathXmlApplicationContext("com/hsp/inherit/beans.xml"); 5 6 Gradate gradate = (Gradate) ac.getBean("gradate"); 7 System.out.println(gradate.getName()+" "+gradate.getAge()+" "+gradate.getDegree()); 8 } 9 10 }
beans.xml文件
<!-- 配置學生對象 --> <bean id="student" class="com.hsp.inherit.Student"> <property name="name" value="李世民"/> <property name="age" value="21"/> </bean> <!-- 配置Gradate --> <bean id="gradate" parent="student" class="com.hsp.inherit.Gradate"> <!-- 如果自身配置屬性那麽,age,則會替換父對象的數據 --> <property name="name" value="李淵"/> <property name="age" value="63"/> <property name="degree" value="小學畢業"/> </bean>
spring(二)