五、Spring容器中Bean的属性值的注入

来源:互联网 发布:软件用途 编辑:程序博客网 时间:2024/06/09 17:06

对于容器中的bean,属性多种多样包括如下情况:

1、普通属性值:如int String 等等基本数据类型,对于普通属性值的注入有两种方式:set注入以及构造器注入。

2、普通bean:这里说的普通bean是与容器中其他的bean的区分。

3、容器中其他的bean

4:集合类型:包括List   Map     Properties    数组等类型。

以下是对上面的4中情况分别进行详细说明。

一、普通属性值

     注意一点:对于构造器注入必须要相应的bean有参数个数对应的构造方法。

<!-- 使用set方法设置实体bean的参数 --><bean id="book" class="org.meify.bean.BookBean" scope="prototype" autowire="byName">  <!-- 自动注入属性bean --><property name="id" value="1001"></property><property name="name" value="张三"></property><!-- <property name="author" ref="author"></property> --> <!-- 注意这里参数author也是一个bean --><property name="intro" value="这是一本好书"></property></bean><!-- 通过构造器进行参数赋值 --><bean id="author" class="org.meify.bean.AuthorBean" lazy-init="true"><!-- 懒加载,仅当用到的时候才去实例化该bean --><constructor-arg value="2001"></constructor-arg><constructor-arg value="李新华"></constructor-arg><constructor-arg value="32"></constructor-arg><constructor-arg value="湖北省武穴市"></constructor-arg></bean>

二、普通bean的注入

<!-- 这里属性的注入 既有普通属性,也有普通bean作为属性注入 --><bean id="student" class="org.meify.bean.StudentBean"><property name="id" value="1001"></property><property name="name" value="王二小"></property><property name="family"><bean class="org.meify.bean.FamilyBean"></bean></property></bean>

三、容器其他bean的注入

<!-- 使用set方法设置实体bean的参数 --><bean id="book" class="org.meify.bean.BookBean" scope="prototype" autowire="byName">  <!-- 自动注入属性bean --><property name="id" value="1001"></property><property name="name" value="张三"></property><property name="author" ref="author"></property>  <!-- 注意这里参数author也是一个bean --><property name="intro" value="这是一本好书"></property></bean>


四、集合属性的配置

   对于集合属性的多种情况,我们先编写一个ChineseBean

package org.meify.bean;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Set;/** * 属性为集合的时候 * @description  * @version 1.0 * @author meify  2014-1-2 下午5:14:07 */public class ChineseBean {    private List<String> schools;    private Map scores;    private Map<String,BookBean> books;    private Properties health;    private Set  AuthorBean;    private String[] addresses;        public List<String> getSchools() {return schools;}public void setSchools(List<String> schools) {this.schools = schools;}public Map getScores() {return scores;}public void setScores(Map scores) {this.scores = scores;}public Map<String, BookBean> getBooks() {return books;}public void setBooks(Map<String, BookBean> books) {this.books = books;}public Properties getHealth() {return health;}public void setHealth(Properties health) {this.health = health;}public Set getAuthorBean() {return AuthorBean;}public void setAuthorBean(Set authorBean) {AuthorBean = authorBean;}public String[] getAddresses() {return addresses;}public void setAddresses(String[] addresses) {this.addresses = addresses;}}

对应属性注入的配置如下:

<!-- 这里对属性为集合的bean进行配置 --><bean id="chinese" class="org.meify.bean.ChineseBean"><property name="schools"><!-- 对List属性配置属性值 --><list><value>小学</value><value>中学</value><value>大学</value></list></property><property name="scores"><!-- 对Map属性配置属性值 --><map><entry key="语文" value="94"></entry><entry key="数学" value="95"></entry><entry key="英语" value="96"></entry></map></property><property name="books"><!-- 对Map<String,Object>属性配置属性值 --><map><entry key="语文书" value-ref="book"></entry><entry key="数学书" value-ref="book"></entry><entry key="英语书" value-ref="book"></entry></map></property><property name="health"><!-- 对Properties属性配置属性值 --><props>   <prop key="血压">正常</prop>   <prop key="身高">170</prop></props></property><property name="AuthorBean"><!-- 对Set属性配置属性值 --><set>   <value>普通字符串</value>   <bean class="org.meify.bean.AuthorBean"></bean>   <ref local="author"/></set></property><property name="addresses"><!-- 对数组属性配置属性值 --><list>   <value>湖北省</value>   <value>湖南省</value>   <value>辽宁省</value></list></property></bean>

编写测试程序对上面范例进行测试:

package org.meify.test;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Set;import org.meify.bean.ChineseBean;import org.meify.bean.StudentBean;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * 设置bean的属性值 * 包括: * 1、普通属性值(两种注入方式:1、set  2、构造器)此处省略 * 2、普通bean * 3、容器中的bean * 4、list、set、map、props * @description  * @version 1.0 * @author meify  2014-1-2 下午2:33:48 */public class Test02 {public static void main(String[] args) {//ApplicationContext的实例即Spring容器,也称之为Spring上下文ApplicationContext ctx=new ClassPathXmlApplicationContext("spring-config.xml");System.out.println(ctx);//测试属性为普通bean的注入StudentBean student=ctx.getBean("student",StudentBean.class);System.out.println(student.getName()+"==="+student.getFamily());//测试属性为集合的注入ChineseBean chinese=ctx.getBean("chinese",ChineseBean.class);//获取chinses对象中的list集合List  schools=chinese.getSchools();for(Object obj:schools){System.out.println(obj.toString());}//获取chinese中的map属性值Map scores=chinese.getScores();System.out.println("语文分数:"+scores.get("语文"));System.out.println("数学分数:"+scores.get("数学"));System.out.println("英语分数:"+scores.get("英语"));//获取chinese中的map<String,Object>属性值Map books=chinese.getBooks();System.out.println("语文书:"+books.get("语文书"));System.out.println("数学书:"+books.get("数学书"));System.out.println("英语书:"+books.get("英语书"));//获取数组属性值String[] addresses=chinese.getAddresses();for(int i=0;i<addresses.length;i++){System.out.println(addresses[i]);}//获取set集合属性值Set set=chinese.getAuthorBean();Iterator it = set.iterator();while(it.hasNext()){System.out.println(it.next());}Properties health=chinese.getHealth();System.out.println("血压:"+health.getProperty("血压"));System.out.println("身高:"+health.getProperty("身高"));}}

控制台输入如下:

org.springframework.context.support.ClassPathXmlApplicationContext@1cb25f1: startup date [Fri Jan 03 10:59:07 CST 2014]; root of context hierarchy王二小===org.meify.bean.FamilyBean@26d607小学中学大学语文分数:94数学分数:95英语分数:96语文书:id==1001;name==张三;author==作者编号:2001;作者姓名:李新华;作者年龄:32;作者住址:湖北省武穴市;简介==这是一本好书数学书:id==1001;name==张三;author==作者编号:2001;作者姓名:李新华;作者年龄:32;作者住址:湖北省武穴市;简介==这是一本好书英语书:id==1001;name==张三;author==作者编号:2001;作者姓名:李新华;作者年龄:32;作者住址:湖北省武穴市;简介==这是一本好书湖北省湖南省辽宁省普通字符串作者编号:null;作者姓名:null;作者年龄:0;作者住址:null作者编号:2001;作者姓名:李新华;作者年龄:32;作者住址:湖北省武穴市血压:正常身高:170





0 0
原创粉丝点击