
正文
峰Spring4学习(3)注入参数的几种类型
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
People.java model类:
package com.cy.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 List<String> hobbies = new ArrayList<String>();
private Set<String> loves = new HashSet<String>();
private Map<String,String> works = new HashMap<String,String>();
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 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;
} public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public List<String> getHobbies() {
return hobbies;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public Set<String> getLoves() {
return loves;
}
public void setLoves(Set<String> loves) {
this.loves = loves;
}
public Map<String, String> getWorks() {
return works;
}
public void setWorks(Map<String, String> works) {
this.works = works;
}
public Properties getAddresses() {
return addresses;
}
public void setAddresses(Properties addresses) {
this.addresses = addresses;
} @Override
public String toString() {
return "People [id=" + id + ", name=" + name + ", age=" + age
+ ", dog=" + dog.getName() + ", hobbies=" + hobbies + ", loves=" + loves
+ ", works=" + works + ", addresses=" + addresses + "]";
} }
com.cy.entity.Dog: Model类:
package com.cy.entity;
public class Dog {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
spring管理bean配置文件:
beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 基本类型值 -->
<bean id="people1" class="com.cy.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
</bean> <bean id="dog1" class="com.cy.entity.Dog">
<property name="name" value="Jack"></property>
</bean> <!-- 注入bean -->
<bean id="people2" class="com.cy.entity.People">
<property name="id" value="2"></property>
<property name="name" value="李四"></property>
<property name="age" value="14"></property>
<property name="dog" ref="dog1"></property>
</bean> <!-- 内部bean -->
<bean id="people3" class="com.cy.entity.People">
<property name="id" value="3"></property>
<property name="name" value="王五"></property>
<property name="age" value="13"></property>
<property name="dog">
<bean class="com.cy.entity.Dog">
<property name="name" value="Tom"></property>
</bean>
</property>
</bean> <!-- null值 -->
<bean id="people4" class="com.cy.entity.People">
<property name="id" value="4"></property>
<property name="name" value="赵六"></property>
<property name="age" value="14"></property>
<property name="dog">
<null></null>
</property>
</bean> <!-- 级联属性 直接这么写,但是people中的dog为null值,会报错,在People dog属性初始化时,直接Dog dog = new Dog() -->
<!--
<bean id="people5" class="com.cy.entity.People">
<property name="id" value="5"></property>
<property name="name" value="孙七"></property>
<property name="age" value="14"></property>
<property name="dog.name" value="jack2"></property>
</bean> --> <!-- 集合属性 -->
<bean id="people6" class="com.cy.entity.People">
<property name="id" value="6"></property>
<property name="name" value="P6"></property>
<property name="age" value="16"></property>
<property name="dog" ref="dog1"></property>
<property name="hobbies">
<list>
<value>唱歌</value>
<value>跳舞</value>
</list>
</property>
<property name="loves">
<set>
<value>唱歌2</value>
<value>跳舞2</value>
</set>
</property>
<property name="works">
<map>
<entry>
<key><value>上午</value></key>
<value>写代码</value>
</entry>
<entry>
<key><value>下午</value></key>
<value>测试代码</value>
</entry>
</map>
</property>
<property name="addresses">
<props>
<prop key="address1">aaaa</prop>
<prop key="address2">bbbb</prop>
</props>
</property>
</bean>
</beans>
测试代码:
package com.cy.test; import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.cy.entity.People; public class T { private ApplicationContext ac; @Before
public void setUp() throws Exception {
ac=new ClassPathXmlApplicationContext("beans.xml");
} //基本类型值
@Test
public void test() {
People people1 = (People) ac.getBean("people1");
System.out.println(people1);
} //注入bean
@Test
public void test2() {
People people2 = (People) ac.getBean("people2");
System.out.println(people2);
} //内部bean
@Test
public void test3() {
People people3 = (People) ac.getBean("people3");
System.out.println(people3);
} //注入null值
@Test
public void test4() {
People people4 = (People) ac.getBean("people4");
System.out.println(people4);
} //级联属性 测试代码直接在People类下面初始化dog,Dog dog = new Dog()
@Test
public void test5() {
People people5 = (People) ac.getBean("people5");
System.out.println(people5);
} //注入集合属性
@Test
public void test6() {
People people6 = (People) ac.getBean("people6");
System.out.println(people6);
//People [id=6, name=P6, age=16, dog=Jack, hobbies=[唱歌, 跳舞], loves=[唱歌2, 跳舞2], works={上午=写代码, 下午=测试代码}, addresses={address2=bbbb, address1=aaaa}] } }






