尽量使用 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 private String [] empName;13 private ListempList;14 private Set empsets;15 private Map empMaps;16 17 public Map getEmpMaps() {18 return empMaps;19 }20 public void setEmpMaps(Map empMaps) {21 this.empMaps = empMaps;22 }23 public Set getEmpsets() {24 return empsets;25 }26 public void setEmpsets(Set empsets) {27 this.empsets = empsets;28 }29 public List getEmpList() {30 return empList;31 }32 public void setEmpList(List 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 args16 */17 public static void main(String[] args) {18 // TODO Auto-generated method stub19 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 Mapempmaps=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 entry1:department.getEmpMaps().entrySet()){49 System.out.println(entry1.getKey()+" "+entry1.getValue().getName());50 }51 }52 53 }
beans.xml文件
12 3 4 39 40 415 6 7 8 14 15 169
13张三 10李四 11王五 1217 21 22 23 2425 30 31 3226 27 28 2933 37 3842 4543 44 46 47 48
内部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文件