set,list,Map,数组的注入

来源:互联网 发布:java怎么以分数形式 编辑:程序博客网 时间:2024/05/19 22:52

bean car和bean customer  对应关系:一个customer对应多个car

在bean customer中除了定义Car car 外还要定义 
   List carlist;
Car[] cars;
 Set carset;
Map carmap;的get/set方法

在bean.xml中一个bean对应一个<bean></bean>所以有customer和car两个

在其中各自写上对应的<property> property 的name是什么取决于bean中的属性是什么
在是set/list/数组时,<list>等中的<ref bean="">引出
MAP:
 <property name="carmap">
         <map>           <entry key="1">           <ref bean="car1"/>           </entry>           <entry key="2">            <ref bean="car2"/>            </entry>         </map>         </property>
准则:注入方式为:建一个bean配置一个bean, 配置的property属性名为建的bean中的属性名,配置set等内值时可以运用内部对象的方式,但是这样外部不能引用!在测试类中通过建bean的对象引用对应的get方法得到值。
<bean id="customer" class="spring.Customer">        <property name="name" value="zhangsan"/>        <property name="salary" value="4000"/>        <property name="carlist">          <list>            <ref bean="car1"/>            <ref bean="car2"/>                          <bean  class="spring.Car">                    <property name="brand" value="mazida"/>                    <property name="cost" value="180000"/>             </bean>           </list>          </property>       
map.get("1")注意有引号   

1.--User类

package org.stelcomtech;

import java.util.List;import java.util.Map;import java.util.Set;

public class User { private String username;

 private int age;

 private Home myHome;

 private Home[] homes;

 private List homesList;

 private Set homesSet;

 private Map homesMap;

 public Home[] getHomes() {  return homes; }

 public void setHomes(Home[] homes) {  this.homes = homes; }

 public List getHomesList() {  return homesList; }

 public void setHomesList(List homesList) {  this.homesList = homesList; }

 public Map getHomesMap() {  return homesMap; }

 public void setHomesMap(Map homesMap) {  this.homesMap = homesMap; }

 public Set getHomesSet() {  return homesSet; }

 public void setHomesSet(Set homesSet) {  this.homesSet = homesSet; }

 public Home getMyHome() {  return myHome; }

 public void setMyHome(Home myHome) {  this.myHome = myHome; }

 public int getAge() {  return age; }

 public void setAge(int age) {  this.age = age; }

 public String getUsername() {  return username; }

 public void setUsername(String username) {  this.username = username; }

}

2.---Home类

package org.stelcomtech;

public class Home { private String homeAddr;

 public String getHomeAddr() {  return homeAddr; }

 public void setHomeAddr(String homeAddr) {  this.homeAddr = homeAddr; }

}

3.--测试类 MainTest

package org.stelcomtech;

import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;

import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;

public class MainTest { public static void main(String[]args){  ApplicationContext ac=new FileSystemXmlApplicationContext("org/stelcomtech/applicationContext.xml");  User user=(User)ac.getBean("userid");  Home[] homes=user.getHomes();  System.out.println("************数组注入***************");  for(int i=0;i<homes.length;i++){   System.out.println("家庭地址:"+homes[i].getHomeAddr());   System.out.println("************List注入********************");  List list=user.getHomesList();  Iterator ir=list.iterator();  while(ir.hasNext()){   //Home home1=(Home)ir.next();   System.out.println("家庭地址:"+ir.next().toString());   System.out.println("***********Set注入******************");  Set set=user.getHomesSet();  Iterator ir1=set.iterator();  while(ir1.hasNext()){   System.out.println("家庭地址:"+ir1.next().toString());   System.out.println("************Map注入**************");  Map map=user.getHomesMap();     Home home11=(Home)map.get("1");   System.out.println("家庭地址:"+home11.getHomeAddr().toString());   Home home111=(Home)map.get("2");   System.out.println("家庭地址:"+home111.getHomeAddr().toString());   }

 }

 

4.配置文件 applicationContext.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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

 <bean id="myHome1" class="org.stelcomtech.Home" abstract="false"  lazy-init="default" dependency-check="default">  <property name="homeAddr">   <value>wuhan</value>  </property> </bean> <bean id="myHome2" class="org.stelcomtech.Home" abstract="false"  lazy-init="default" dependency-check="default">  <property name="homeAddr">   <value>shanghai</value>  </property> </bean>

    <!-- autowire 选择 byName时,按照Bean id 是否和类中的属性名来查找  -->    <!-- autowire 选择 byType时,按照 类的类型 来查找 --> <bean id="userid" class="org.stelcomtech.User" abstract="false"    lazy-init="default" dependency-check="default">  <property name="username">   <value>haiyang</value>  </property>  <property name="age">   <value>22</value>  </property>   <!-- 数组注入方式 -->  <property name="homes">  <list>   <ref bean="myHome1" />   <ref bean="myHome2" />  </list>  </property>  <!-- List注入方式 -->   <property name="homesList">   <list>    <ref bean="myHome1" />    <ref bean="myHome2" />    <value>haha</value>       </list>  </property>  <!-- Set注入方式 -->   <property name="homesSet">   <set>    <value>hello</value>    <ref bean="myHome2" />    <ref bean="myHome1" />   </set>  </property>  <!-- Map注入方式 -->  <property name="homesMap">   <map>    <entry key="1">     <ref bean="myHome1" />    </entry>    <entry key="2">    <ref bean="myHome2" />    </entry>   </map>  </property>

</bean> </beans>

 

5.运行结果

log4j:WARN No appenders could be found for logger (org.springframework.context.support.FileSystemXmlApplicationContext).log4j:WARN Please initialize the log4j system properly.************数组注入***************家庭地址:wuhan家庭地址:shanghai************List注入********************家庭地址:org.stelcomtech.Home@1d7ad1c家庭地址:org.stelcomtech.Home@65a77f家庭地址:haha***********Set注入******************家庭地址:hello家庭地址:org.stelcomtech.Home@65a77f家庭地址:org.stelcomtech.Home@1d7ad1c************Map注入**************家庭地址:wuhan家庭地址:shanghai

 

 

 

 

 

阅读全文
0 0
原创粉丝点击