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

来源:互联网 发布:淘宝上买iphone7 编辑:程序博客网 时间:2024/04/27 01:39

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;   }   }  辅助类 userpackage 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;   }      }测试类 TestBeanpackage 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>


 

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 华为手机无法访问移动网络怎么办 晒课上传课堂实录太大怎么办 手机酷狗音乐下载要钱怎么办 手机酷狗下载要钱怎么办 酷我音乐没有声音怎么办 手机酷我音乐没有声音怎么办 酷我音乐歌曲下载收费怎么办 网易云下载超过每日上限怎么办 全民k歌领不了花怎么办 安卓全民k歌延迟怎么办 全民k歌唱歌延迟怎么办 全民k歌耳机延迟怎么办 word文档打开是乱码怎么办 全民k歌不能录音怎么办 全民k歌登录不上怎么办 平果手机迅雷闪退怎么办 电脑打开央视影音死机怎么办 先锋影音二级网页打不开怎么办 手机qq音乐登录失效怎么办 酷狗账号忘记了怎么办 手机qq音乐听不了歌怎么办 第一试用网密码忘了怎么办 玩h1z1画面卡顿怎么办 uu跑腿抢不到单怎么办 比特币加密忘了怎么办 路虎发现cd卡死怎么办 苹果手机帐号被锁定怎么办 苹果手机帐号锁定了怎么办 微博帐号被锁定怎么办 微博显示帐号被锁定怎么办 uc屏蔽了一个网站怎么办 uu跑腿送货遇到不方便收货怎么办 雷神加速器忘记暂停怎么办 obs直播开摄像头吃鸡掉帧怎么办 陌陌收到的礼物怎么办 吃了油腻的东西恶心怎么办 主播工资不发怎么办 主播工资被欠怎么办 直播平台不发工资坑主播怎么办 主播公司不发工资怎么办 梦幻月卡用完了怎么办