spring心得4--setter注入集合(set、list、map、properties等多种集合,配有案例解析)@基本装(引用)

spring心得4--setter注入集合(set、list、map、properties等多种集合,配有案例解析)@基本装

  

1. 基本装配

    在spring容器内拼凑bean叫做装配。装配bean的时候,需要告诉容器哪些bean以及容器如何使用依赖注入将它们配合在一起。

   使用XML装配(xml是最常见的spring应用系统配置源。)

   几种spring容器都支持使用xml装配bean,包括:

   1).XmlBeanFactory:调用InputStream载入上下文定义文件。

   2).ClassPathXmlApplicationContext:从类路径载入上下文定义文件。

   3).XmlWenApplicationContext:从web应用上下文中载入定义文件。

   上下文定义文件的根元素是<beans>.<beans>有多个<bean>子元素。每个<bean>元素定义了一个bean如何被装配到spring容器中。对bean的最基本的配置包括bean的ID和他的全称类名

基本装配-scope

   scope属性的值有以下五种:prototype、singleton、request session、global-session。

   spring中的bean缺省情况下是单例模式。始终返回一个实例。若想返回不同的实例的话需要定义成原型模式。

 2. 实例化与销毁

    spring实例化bean或销毁bean时,有时需要作一些处理工作,因此spring可以在创建和拆卸bean的时候调用bean的两个生命周期方法(bean的声明周期在上篇博客有重墨讲解)。

  <bean class="Foo" init-method destory-method>

  <bean class="...CommonAnnotationBeanPostProcessor">

   spring也提供了两个接口来实现相同的功能:

  InitializingBean和DisposableBean.InitializingBean接口提供了一个afterPropertiesSet()方法。DisposableBean接口提供了destroy().不推荐使用该接口,它将你的bean和springAPI邦定在一起。

3.一些注意事项

    继承配置(继承在bean标签加属性parent属性加以指明,该属性值为继承父类bean的id),覆盖父 Bean配置。

   可以设置 <bean> 的abstract 属性为 true, Spring 不会实例化该Bean有些属性不会被继承. 比如: autowire, abstract 等.子Bean 指定自己的class. 但此时 abstract 必须设为 true

   通过set方法依赖注入

   <bean>元素的< property >子元素指明了使用它们的set方法来注入。可以注入任何东西,从基本类型到集合类,甚至是应用系统的bean

   配置bean的简单属性,基本数据类型和string。

   在对应bean实例的property子标签中设置一个bean类型的属性;这种方式的缺点是你无法在其它地方重用这个bar实例,原因是它是专门为foo而用。

4.setter注入集合

  装配List和数组:

  <property name="barlist">

       <list>

           <value>bar1</value>

           <ref bean="bar2"/>

       </list>

  </property>

  装配set:

  <property name="barlist">

       <set>

           <value>bar1</value>

           <ref bean="bar2"/>

       </set>

  </property>

     set使用方法和list一样,不同的是对象被装配到set中,而list是装配到List或数组中装配

  装配map:

  <property name="barlist">

       <map>

           <entry key="key1" value="bar1" />

           <entry key="key2 value-ref="xxx" />

      </map>

  </property>

   key值必须是string的,key-ref可以是其他bean。

  设置null:

  <property name="barlist">

         <null/>

  </property>

  注入集合的案例分析

     以下类中的属性命名方式和访问权限修饰符都是为了做测试,比如下面属性都是public类型的。实际开发中都是private类型,通过get方法来访问属性,这里只是为了简单测试 。

 集合bean  CollectionBean类

package www.csdn.spring.collection.set;

 

import java.util.List;

import java.util.Map;

import java.util.Properties;

import java.util.Set;

 

publicclass CollectionBean {

   

   //set集合

   public  Set<String> sets;

 

   publicvoid setSets(Set<String> sets) {

      this.sets = sets;

   }

 

   public CollectionBean() {

      System.out.println("初始化。。。。。");

      

   }

   

   //list集合

   public List<User> users;

 

   publicvoid setUsers(List<User> users) {

      this.users = users;

   }

   

   //map集合

   public Map<Integer,User> map;  

 

   publicvoid setMap(Map<Integer, User> map) {

      this.map = map;

   }

   

   //properties集合

   public Properties props;

 

   publicvoid setProps(Properties props) {

      this.props = props;

   }

   

}

  辅助类 user

package www.csdn.spring.collection.set;

 

publicclass User {

 

   public String name;

   public Integer age;

   publicvoid setName(String name) {

      this.name = name;

   }

   publicvoid setAge(Integer age) {

      this.age = age;

   }

   

   

}

测试类 TestBean

package www.csdn.spring.collection.set;

 

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import java.util.Map.Entry;

import java.util.Properties;

import java.util.Set;

 

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

publicclass TestBean {

 

   @Test

   publicvoid test() {

      ApplicationContext context = new ClassPathXmlApplicationContext("spring-collection.xml");

      CollectionBean bean = context.getBean("collectionBean",CollectionBean.class);

      

      //set集合

      Set<String> sets = bean.sets;

       //得到迭代器

       Iterator<String> it = sets.iterator();

       while(it.hasNext()){

          System.out.println(it.next());

       }

       

       System.out.println("-------------------------list集合------------------------");

     

       //list集合

       List<User> users = bean.users;

       for(User user : users){

          System.out.println(user.name+"---"+user.age);

       }

       

       System.out.println("--------------------------map集合------------------------");

        

       //map集合

       //方法一:

       Map<Integer,User> map = bean.map;

       //得到map的key键的set集合

       Set<Integer> setKeys = map.keySet();

      //得到key键的迭代器

       Iterator<Integer> itKeys = setKeys.iterator();

      //迭代键值

       while(itKeys.hasNext()){

          //得到一个具体键值

          Integer key = itKeys.next();

          //通过get(key)方法获取到key值对应的value

          User user = map.get(key);

          System.out.println(key+"--"+user.name+"="+user.age);

       }

       System.out.println("========================");

       //方法二:

       Set<Entry<Integer,User>> setEntry = map.entrySet();

       Iterator<Entry<Integer,User>> itEntry = setEntry.iterator();

       while(itEntry.hasNext()){

          Entry<Integer,User> entry = itEntry.next();

          User user = entry.getValue();

          System.out.println(entry.getKey()+"---"+user.name+"="+user.age);

       }

          

       System.out.println("-------------------------properties集合------------------------");

        

       //properties集合

       Properties props = bean.props;

       Set<String> setProps = props.stringPropertyNames();

       Iterator<String> keyStr = setProps.iterator();

       while(keyStr.hasNext()){

          String key = keyStr.next();

          //通过getProperty(key)方法来获取key对应的value值

          System.out.println(key+"----"+props.getProperty(key));

       }

   } 

 

}

  spring配置文件

<?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="collectionBean" class="www.csdn.spring.collection.set.CollectionBean">

      <!-- set集合 -->

      <property name="sets">

         <set>

            <value>陈红军</value>

            <value>杨凯</value>

            <value>李伟</value>

            <value>小胖</value>

            <value>潇洒</value>

         </set>

      </property>

 

      <!-- list集合 -->

      <property name="users">

         <array>

            <ref bean="u1" />

            <ref bean="u2" />

            <ref bean="u3" />

         </array>

         <!-- <list> <ref bean="u1"/> <ref bean="u2"/> <ref bean="u3"/> </list> -->

      </property>

 

      <!-- map集合 -->

      <property name="map">

         <map>

            <entry key="1" value-ref="u1" />

            <entry key="2">

                <ref bean="u2" />

            </entry>

            <entry key="3" value-ref="u3" />

         </map>

      </property>

 

      <!--properties集合 -->

      <property name="props">

         <props>

            <prop key="1">jdbc:oracle</prop>

            <prop key="2">jdbc:mysql</prop>

            <prop key="3">jdbc:access</prop>

         </props>

      </property>

 

   </bean>

 

   <bean id="u1" class="www.csdn.spring.collection.set.User">

      <property name="name" value="杨凯" />

      <property name="age" value="22" />

   </bean>

   <bean id="u2" class="www.csdn.spring.collection.set.User">

      <property name="name" value="潇洒" />

      <property name="age" value="22" />

   </bean>

   <bean id="u3" class="www.csdn.spring.collection.set.User">

      <property name="name" value="红军" />

      <property name="age" value="28" />

   </bean>

 

</beans>
原文地址:https://www.cnblogs.com/gongyu/p/4128318.html