我们对类中的参数注入的时候,有的时候,注入的是我们自己定义的类,而不是java基本的类型。
如下
Home.java是我们自己定义的类
package com.endual.bean;
public class Home {
private String address ;
private int phone ;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getPhone() {
return phone;
}
public void setPhone(int phone) {
this.phone = phone;
}
}
People.java
package com.endual.bean;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class People {
private Home home;
private Home[] homes;
private List<Home> lists;
private Map<Integer,Home> maps;
private Set<Home> sets;
public Home getHome() {
return home;
}
public void setHome(Home home) {
this.home = home;
}
public Home[] getHomes() {
return homes;
}
public void setHomes(Home[] homes) {
this.homes = homes;
}
public List<Home> getLists() {
return lists;
}
public void setLists(List<Home> lists) {
this.lists = lists;
}
public Map<Integer, Home> getMaps() {
return maps;
}
public void setMaps(Map<Integer, Home> maps) {
this.maps = maps;
}
public Set<Home> getSets() {
return sets;
}
public void setSets(Set<Home> sets) {
this.sets = sets;
}
}
配置文件的配置
<?xml version="1.0" encoding="UTF-8"?> <!-- - Middle tier application context definition for the image database. --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="people" class="com.endual.bean.People"> <property name="home"> <bean id="people_home" class="com.endual.bean.Home"> <property name="address" value="浙江工商大学people_home" /> <property name="phone" value="123123123" /> </bean> </property> <property name="homes"> <list> <bean id="people_homes1" class="com.endual.bean.Home"> <property name="address" value="浙江工商大学people_homes1" /> <property name="phone" value="123123123" /> </bean> <bean id="people_homes2" class="com.endual.bean.Home"> <property name="address" value="浙江工商大学people_homes2" /> <property name="phone" value="123123123" /> </bean> <bean id="people_homes3" class="com.endual.bean.Home"> <property name="address" value="浙江工商大学people_homes3" /> <property name="phone" value="123123123" /> </bean> </list> </property> <property name="lists"> <list> <bean id="lists1" class="com.endual.bean.Home"> <property name="address" value="浙江工商大学lists1" /> <property name="phone" value="123123123" /> </bean> <bean id="lists2" class="com.endual.bean.Home"> <property name="address" value="浙江工商大学lists2" /> <property name="phone" value="123123123" /> </bean> <bean id="lists3" class="com.endual.bean.Home"> <property name="address" value="浙江工商大学lists3" /> <property name="phone" value="123123123" /> </bean> </list> </property> <property name="maps"> <map key-type="int"> <entry key="1"> <bean id="maps1" class="com.endual.bean.Home"> <property name="address" value="浙江工商大学maps1" /> <property name="phone" value="123123123" /> </bean> </entry> <entry key="2"> <bean id="maps2" class="com.endual.bean.Home"> <property name="address" value="浙江工商大学maps1" /> <property name="phone" value="123123123" /> </bean> </entry> <entry key="3"> <bean id="maps3" class="com.endual.bean.Home"> <property name="address" value="浙江工商大学maps1" /> <property name="phone" value="123123123" /> </bean> </entry> </map> </property> <property name="sets"> <set> <bean id="set1" class="com.endual.bean.Home"> <property name="address" value="浙江工商大学set1" /> <property name="phone" value="123123123" /> </bean> <bean id="set2" class="com.endual.bean.Home"> <property name="address" value="浙江工商大学set2" /> <property name="phone" value="123123123" /> </bean> <bean id="set3" class="com.endual.bean.Home"> <property name="address" value="浙江工商大学set3" /> <property name="phone" value="123123123" /> </bean> </set> </property> </bean> </beans>
测试
package com.endual.main;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.endual.bean.Home;
import com.endual.bean.People;
public class MainRun {
public static void main(String[] args) {
// ClassPathXmlApplicationContext factory=new ClassPathXmlApplicationContext("bean.xml");
ApplicationContext context=new FileSystemXmlApplicationContext("src/applicationContext.xml");
People hello = (People)context.getBean("people");
Home home = hello.getHome() ;
System.out.println(home.getAddress());
Home[] homes = hello.getHomes() ;
System.out.println(homes[1].getAddress());
List<Home> lists = hello.getLists() ;
System.out.println(lists.get(0).getAddress());
Map<Integer,Home> maps = hello.getMaps() ;
System.out.println(maps.get(1).getAddress());
Set<Home> sets = hello.getSets() ;
System.out.println(sets.iterator().next().getAddress());
}
}
打印结果:
浙江工商大学people_home
浙江工商大学people_homes2
浙江工商大学lists1
浙江工商大学maps1
浙江工商大学set1
感受:
感觉到,其他自定义类的书写和我们一般的是一样的,就是用bean作为我们的参数的一个整体