阅读:5841次   评论:0条   更新时间:2011-06-01    

1、新建一个java工程名叫DIConfig,加入commons-loggin.jar和spring.jar。

2、简单的语法就不演示,具体代码的含义请看注释,这里不再作解释

3、内部bean的配置

/**  
 * 内部bean的引用演示  
 */  
package com.iwtxokhtd.bean.config.inner;   
  
/**  
 * @author Administrator  
 *  
 */  
//外部bean   
public class OuterBean {   
  
    private String outer;   
    private InnerBean target;   
    //外部访问   
    public InnerBean getTarget() {   
        return target;   
    }   
    //setter注入内部bean   
    public void setTarget(InnerBean target) {   
        this.target = target;   
    }   
    public String getOuter() {   
        return outer;   
    }   
    public void setOuter(String outer) {   
        this.outer = outer;   
    }   
       
       
}  

 

/**  
 * 作为其它bean的内部bean来使用  
 */  
package com.iwtxokhtd.bean.config.inner;   
  
/**  
 * @author Administrator  
 *  
 */  
public class InnerBean {   
  
//  用setter注入两个属性   
    private String name;   
    private int age;   
    public int getAge() {   
        return age;   
    }   
    public void setAge(int age) {   
        this.age = age;   
    }   
    public String getName() {   
        return name;   
    }   
    public void setName(String name) {   
        this.name = name;   
    }   
}  

 bean集合的配置:

/**  
 * 演示bean中常见集合配置  
 */  
package com.iwtxokhtd.bean.config.collection;   
  
import java.util.List;   
import java.util.Map;   
import java.util.Properties;   
import java.util.Set;   
  
/**  
 * @author Administrator  
 *  
 */  
public class CollectionBean {   
  
    //1:Properties   
    private Properties propertys;   
    //2:List   
    private List list;   
    //3:Map   
    private Map maps;   
    //4:Set   
    private Set set;   
    public List getList() {   
        return list;   
    }   
    public void setList(List list) {   
        this.list = list;   
    }   
    public Map getMaps() {   
        return maps;   
    }   
    public void setMaps(Map maps) {   
        this.maps = maps;   
    }   
    public Properties getPropertys() {   
        return propertys;   
    }   
    public void setPropertys(Properties propertys) {   
        this.propertys = propertys;   
    }   
    public Set getSet() {   
        return set;   
    }   
    public void setSet(Set set) {   
        this.set = set;   
    }   
       
}  

 

/**  
 * 在同一容器内被引用的bean  
 */  
package com.iwtxokhtd.bean.config.collection;   
  
/**  
 * @author Administrator  
 *  
 */  
public class ReferenceBean {   
    @Override  
    public String toString(){   
        return "引用的bean";   
    }   
}  

 集合的合并:

 

/**  
 * 测试集合合并用到的父类  
 */  
package com.iwtxokhtd.bean.config.collection;   
  
import java.util.Properties;   
  
/**  
 * @author Administrator  
 *  
 */  
public  class ParentMergeBean {   
  
    private Properties pro;   
  
    public Properties getPro() {   
        return pro;   
    }   
  
    public void setPro(Properties pro) {   
        this.pro = pro;   
    }   
}  

 

 

/**  
 * 测试bean集合的合并(先取并后去年交的部分)  
 */  
package com.iwtxokhtd.bean.config.collection;   
  
  
/**  
 * @author Administrator  
 *  
 */  
public class MergeBean extends ParentMergeBean{   
  
       
}  

 

spring配置文件beans.xml代码总汇:

<?xml version="1.0" encoding="gbk"?>  
<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-2.5.xsd">  
   <!--引用其它bean   
    idref:引用容器内其它bean的id给<constructor-arg/>和<property/>  
    与ref local="xxxx"的配置具有同等功能   
    ref bean="xxxx"可以引用父容器或本容器内的其它bean   
    ref parent="xxxx"只能引用父容器内的其它bean,可以与自身bean名相同   
   -->  
   <!--演示内部bean的使用-->  
   <bean id="outer" class="com.iwtxokhtd.bean.config.inner.OuterBean">  
     <!--注入一个简单类型的属性-->  
     <property name="target">  
     <!--开始配置内部bean,内部bean在java代码中不一定是内部类-->  
     <!--不用配置其id和name,配了也会过忽略-->  
         <bean class="com.iwtxokhtd.bean.config.inner.InnerBean">  
            <!--setter注入了两个属性-->  
            <property name="name" value="内部bean的name属性"/>  
            <property name="age" value="27"/>  
         </bean>  
     </property>  
     <!--外部bean的简单类型属性-->  
     <property name="outer">  
        <value>外部bean的属性值</value>  
     </property>  
   </bean>  
   <!--集合元素引用的bean-->  
   <bean id="refbean" class="com.iwtxokhtd.bean.config.collection.ReferenceBean"/>  
   <!--bean集合类的配置演示-->  
   <bean id="col" class="com.iwtxokhtd.bean.config.collection.CollectionBean">  
      <!--配置Properties-->  
      <property name="propertys">  
          <props>  
            <!--设置所有的prop,为键值对-->  
            <prop key="163">iwtxokhtd@163.com</prop>  
            <prop key="sina">iwtxokhtd@sina.com.cn</prop>  
            <prop key="yahoo">iwtxokhtd1@yahoo.cn</prop>  
          </props>  
      </property>  
      <!--配置List-->  
      <property name="list">  
         <list>  
            <value>有序元素一</value>  
            <value>有序元素二</value>  
            <value>有序元素三</value>  
         </list>  
      </property>  
      <!--配置Map-->  
      <property name="maps">  
        <map>  
            <entry>  
                <!--这里没有<key value="key1"/>的写法-->  
                <key>  
                    <value>key1</value>  
                </key>  
                <value>value1</value>  
            </entry>  
            <!--等价形式   
            <entry key="key1" value="value1"/>  
            -->  
            <entry>  
                <key>  
                    <value>key2</value>  
                </key>  
                <value>value2</value>  
            </entry>  
            <entry>  
                <key>  
                    <value>key3</value>  
                </key>  
                <ref bean="refbean"/>  
            </entry>  
        </map>  
      </property>  
      <!--配置Set-->  
      <property name="set">  
        <set>  
            <value>无序不重复元素1</value>  
            <value>无序不重复元素1</value>  
            <value>无序不重复元素2</value>  
            <ref local="refbean"/>  
        </set>  
      </property>  
   </bean>  
   <!--测试集合合并-->  
   <bean id="parentbean" class="com.iwtxokhtd.bean.config.collection.ParentMergeBean">  
       <property name="pro">  
          <props>  
            <!--设置所有的prop,为键值对-->  
            <prop key="163">iwtxokhtd@163.com</prop>  
            <prop key="sina">iwtxokhtd@sina.com.cn</prop>  
            <prop key="yahoo">iwtxokhtd1@yahoo.cn</prop>  
          </props>  
      </property>  
   </bean>  
   <bean id="merge" parent="parentbean">  
     <!--配置Properties-->  
      <property name="pro">  
        <!--指定了合并-->  
          <props merge="true">  
            <!--设置所有的prop,为键值对-->  
            <prop key="qq">iwtxokhtd@qq.com</prop>  
            <prop key="sina">iwtxokhtd@sina.com.cn</prop>  
            <prop key="126">iwtxokhtd@126.com</prop>  
          </props>  
      </property>  
   </bean>  
   <!--   
   强类型集合就不用演示了,只是支持泛型而已   
   自动装配也不讲解,虽然它用起来很方便但不安全,并时不推荐使用自动装配   
   对于bean的其它元素和属性配置这里不讲解,在以后的实例中用到再说   
   -->  
</beans>  

 

客户端测试:

/**  
 * bean配置详解测试  
 */  
package com.iwtxokhtd.bean.config.test;   
  
import java.util.ArrayList;   
import java.util.Iterator;   
import java.util.List;   
import java.util.Map;   
import java.util.Properties;   
import java.util.Set;   
  
import org.springframework.context.ApplicationContext;   
import org.springframework.context.support.ClassPathXmlApplicationContext;   
  
import com.iwtxokhtd.bean.config.collection.CollectionBean;   
import com.iwtxokhtd.bean.config.collection.ParentMergeBean;   
import com.iwtxokhtd.bean.config.inner.OuterBean;   
  
/**  
 * @author Administrator  
 *  
 */  
public class BeanConfigTest {   
  
    @SuppressWarnings("unchecked")   
    public static void main(String []args){   
        ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");   
        System.out.println("获取外部bean");   
        //获取外部bean   
        OuterBean ob=(OuterBean)ac.getBean("outer");   
        //得到外部bean的属性值   
        System.out.println("得到外部bean的属性值:"+ob.getOuter());   
        //访问内部bean的属性值   
        System.out.println("得到内部bean的两个属性值:name="+ob.getTarget().getName()+", age="+ob.getTarget().getAge());   
        //测试bean中的集合配置   
        System.out.println("测试bean中的集合配置");   
        //先是Properties   
        System.out.println("先看下Properties");   
        CollectionBean cb=(CollectionBean)ac.getBean("col");   
        Properties props=cb.getPropertys();   
        System.out.println("Properties里的key-value");   
        Set<Object> set=props.keySet();   
        for(Iterator<Object> iter=set.iterator();iter.hasNext();){   
            String key=(String) iter.next();   
            //不能写成System.out.println(iter.next()+"=>"+props.getProperty(iter.next()));   
            //这样循环一次iter会迭代两次,最终会找不到元素   
            System.out.println(key+"=>"+props.getProperty(key));   
        }   
        System.out.println("再看下List");   
        List<String> list=new ArrayList<String>();   
        list=cb.getList();   
        for(Iterator<String> iter=list.iterator();iter.hasNext();){   
            System.out.println(iter.next());   
        }   
        System.out.println("下面看下Map");   
        Map map=cb.getMaps();   
        Set entries=map.keySet();   
        //若用Set entries=map.entrySet();   
        //则下面的for里直接用System.out.println(iter.next())可得结果如下:   
        //key1=value1   
        //key2=value2   
        for(Iterator<Object> iter=entries.iterator();iter.hasNext();){   
            String key=(String)iter.next();   
            System.out.println(key+"=>"+map.get(key));   
        }   
        System.out.println("看再一个Set");   
        Set sets=cb.getSet();   
        for(Iterator<Object> iter=sets.iterator();iter.hasNext();){   
            //由于Set里有一个重复的元素,所以这里打印出来的是不重复的元素   
            System.out.println(iter.next());   
        }   
        //测试集合的合并   
        System.out.println("测试集合的合并");   
        //此处必须是父类   
        ParentMergeBean mb=(ParentMergeBean) ac.getBean("merge");   
        props=mb.getPro();   
        set=props.keySet();   
        for(Iterator<Object> iter=set.iterator();iter.hasNext();){   
            String key=(String) iter.next();   
            System.out.println(key+"=>"+props.getProperty(key));   
        }   
    }   
}  

 

测试结果:

获取外部bean
得到外部bean的属性值:外部bean的属性值
得到内部bean的两个属性值:name=内部bean的name属性, age=27
测试bean中的集合配置
先看下Properties
Properties里的key-value
163=>iwtxokhtd@163.com
sina=>iwtxokhtd@sina.com.cn
yahoo=>iwtxokhtd1@yahoo.cn
再看下List
有序元素一
有序元素二
有序元素三
下面看下Map
key1=>value1
key2=>value2
key3=>引用的bean
看再一个Set
无序不重复元素1
无序不重复元素2
引用的bean
测试集合的合并
qq=>iwtxokhtd@qq.com
163=>iwtxokhtd@163.com
126=>iwtxokhtd@126.com
sina=>iwtxokhtd@sina.com.cn
yahoo=>iwtxokhtd1@yahoo.cn

评论 共 0 条 请登录后发表评论

发表评论

您还没有登录,请您登录后再发表评论

文章信息

  • iwtxokhtd在2009-04-10创建
  • iwtxokhtd在2011-06-01更新
  • 标签: spring2.5, ioc, bean, 配置
Global site tag (gtag.js) - Google Analytics