注入参数:
1,基本类型值;
2,注入bean;3,内部bean;4,null 值;5,级联属性;6,集合类型属性;package entity;public class Dog { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
package entity;import java.util.ArrayList;import java.util.HashMap;import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Set;public class People { private int id; private String name; private int age; private Dog dog; //级联属性 private Listhobbies=new ArrayList (); private Set loves=new HashSet (); private Map works=new HashMap (); private Properties addresses=new Properties(); public People() { super(); // TODO Auto-generated constructor stub } public People(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } public Set getLoves() { return loves; } public void setLoves(Set loves) { this.loves = loves; } public List getHobbies() { return hobbies; } public void setHobbies(List hobbies) { this.hobbies = hobbies; } public Map getWorks() { return works; } public void setWorks(Map works) { this.works = works; } public Properties getAddresses() { return addresses; } public void setAddresses(Properties addresses) { this.addresses = addresses; } public Dog getDog() { return dog; } public void setDog(Dog dog) { this.dog = dog; } 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; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "People [id=" + id + ", name=" + name + ", age=" + age + ", dog=" + dog + ", hobbies=" + hobbies + ", loves=" + loves + ", works=" + works + ", addresses=" + addresses + "]"; } }
package test;import org.junit.Before;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import entity.People;public class T { private ApplicationContext ac; @Before public void setUp() throws Exception { ac=new ClassPathXmlApplicationContext("beans.xml"); } // 基本类型值 @Test public void test1() { People people=(People)ac.getBean("people1"); System.out.println(people); } // 注入bean @Test public void test2() { People people=(People)ac.getBean("people2"); System.out.println(people); } // 内部bean @Test public void test3() { People people=(People)ac.getBean("people3"); System.out.println(people); } // 注入null @Test public void test4() { People people=(People)ac.getBean("people4"); System.out.println(people); } // 级联属性 @Test public void test5() { People people=(People)ac.getBean("people5"); System.out.println(people); } // 注入集合 @Test public void test6() { People people=(People)ac.getBean("people6"); System.out.println(people); }}
唱歌 跳舞 唱歌2 跳舞2 aaaaa bbbbb