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>
时间: 2024-07-29 03:22:12

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

Spring集合 (List,Set,Map,Properties) 实例

下面例子向您展示Spring如何注入值到集合类型(List, Set, Map, and Properties). 支持4个主要的集合类型: List – <list/> Set – <set/> Map – <map/> Properties – <props/> Spring beans 一个Customer对象,有四个集合属性. package com.yiibai.common; import java.util.List; import java.

【spring set注入 注入集合】 使用set注入的方式注入List集合和Map集合/将一个bean注入另一个Bean

Dao层代码: 1 package com.it.dao; 2 3 public interface SayHell { 4 public void sayHello(); 5 } Dao的Impl实现层: 1 package com.it.dao.impl; 2 3 import java.util.List; 4 import java.util.Map; 5 6 import com.it.dao.SayHell; 7 8 /** 9 * Spring如何知道setter方法?如何将值注入

[原创]java WEB学习笔记98:Spring学习---Spring Bean配置及相关细节:如何在配置bean,Spring容器(BeanFactory,ApplicationContext),如何获取bean,属性赋值(属性注入,构造器注入),配置bean细节(字面值,包含特殊字符,引用bean,null值,集合属性list map propert),util 和p 命名空间

本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱好者,互联网技术发烧友 微博:伊直都在0221 QQ:951226918 -----------------------------------------------------------------------------------------------------------------

【Spring】Construcotrer注入和setter注入不同的XML写法方式

林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 本文主要讲解了Spring中constructor注入的4种不同写法和sette的3种不同写法 一.constructor注入4种不同写法 通过构造方法注入,就相当于给构造方法的参数传值set注入的缺点是无法清晰表达哪些属性是必须的,哪些是可选的,构造注入的优势是通过构造强制依赖关系,不可能实例化不完全的或无法使用的bean. 第1种方法:直接传值 <!-- constructor方式注入写

[Spring实战系列](8)Spring注入方式之setter注入

通常,JavaBean 的属性是私有的,同时拥有一组存取器方法,以setXXX() 和getXXX() 形式存在.Spring 可以借助属性的set方法来配置属性的值,以实现setter方式的注入. 1. 注入简单值 在Spring 中我们可以使用<property> 元素配置Bean 的属性.<property>在许多方面都与<constructor-arg> 类似,只不过一个是通过构造参数来注入值,另一个是通过调用属性的setter 方法来注入值. 举例说明,让我们

Spring依赖注入的Setter注入(通过get和set方法注入)

导入必要的jar包(Spring.jar和commonslogging.jar) 在src目录下建立applicationContext.xml   (Spring 管理 bean的配置文件) <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPEING//DTD BEAN//EN" "http://www.springframewor

spring之setter注入

setter注入分为2种 第一:普通属性注入 <bean id="userAction" class="com.xx.action.UserAction"><!--第一种--> <property name="age" value="13"></property><!--第二种--><property name="name"> <

Spring笔记②--各种属性注入

Ioc 反转控制 反转资源获取的方向 分离接口与实现 采用工厂模式 采用反转控制 ? Di 依赖注入 依赖容器把资源注入 ? 配置bean 通过全类名(反射) 配置形式:基于xml方式 Ioc容器的beanFactory&ApplicationContext 依赖注入的方式:属性注入,构造器注入 ? ? Bean必须要有一个无参的构造函数 Class:bean的全类名,通过反射的方式在IOC容器中创建bean,所以要求bean中必须有无参的构造函数 id :bean 的标示,id唯一 ? app

Spring 中设置依赖注入

package com.ysq.vo; public class User { private int uid; private String uname; private String pwd; private Date birth; private String[] likes; //声明一个数组 private List<String> list; //声明一个集合 private Map<String, String> map; //声明一个map public User(