1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Spring 在xml文件中配置Bean

Spring 在xml文件中配置Bean

时间:2019-02-07 01:58:46

相关推荐

Spring 在xml文件中配置Bean

Spring容器是一个大工厂,负责创建、管理所有的Bean。

Spring容器支持2种格式的配置文件:xml文件、properties文件,最常用的是xml文件。

Bean在xml文件中的配置

<beans>

根元素,可包含多个<bean>元素,一个<bean>即一个Bean的配置。

<bean>

一个<bean>即一个Bean对象。原来是new出来该类的一个对象,Spring中是一个<bean>创建一个对象。

<bean name="" class="" scope="" />

name指定对象的名称,class指定该Bean的类,scope指定该对象的作用域。class属性是必须的,其它可选。

对象的名称可通过name或id指定,id只能指定一个名称,name可指定一个或多个名称,用逗号或分号隔开即可。示例:name="grade,score"。未指定id或name时,默认取class属性的值。

Bean的作用域

示例:singleton作用域

<bean name="student" class="my_package.Student" scope="singleton" />

1 ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");2 //获取到的这两个对象是同一个对象。3 Student student1=applicationContext.getBean("student",Student.class); 4 Student student2=applicationContext.getBean("student",Student.class);5 //输出相同6 System.out.println(student1); 7 System.out.println(student2);

缺省scope属性时,默认就是singleton作用域。

示例:prototype作用域

<bean name="student" class="my_package.Student" scope="prototype" />

1 ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");2 //获取到的这两个对象是不同的。调用getBean()一次,就创建一个新的对象。3 Student student1=applicationContext.getBean("student",Student.class);4 Student student2=applicationContext.getBean("student",Student.class);5 //输出不同6 System.out.println(student1);7 System.out.println(student2);

说明:

在xml配置文件中,一个<bean>配置的是一个Bean,配置的是一个类,不是该类的一个实例(对象)。

在调用getBean()时获取/引用容器中Bean实例时,Spring容器根据id/name找到这个Bean对应的配置,查看作用域,该重新新建这个Bean的实例就重新新建,该返回已存在的实例就返回已存在的实例。

<bean>的子元素——<constructor-arg>

<bean name="" class=""><constructor-arg index/name="" value/ref="" /><constructor-arg index/name="" value/ref="" /></bean>

<constructor-arg>用于向该bean的构造函数传递参数。一个<constructor-arg>传递一个参数,一个<bean>可带有多个<constructor-arg>子元素,根据<constructor-arg>的个数调用相应的构造函数。

name或index指定形参,name是用形参名指定,index是用形参列表的下标指定(从0开始)。缺省时,默认从第一个参数开始,依次传值。

value或ref指定实参值,value只能指定基础类型(Spring容器会自动转换为对应的数据类型),ref只能指定为其它的Bean(指定为其它Bean的name或id),根据需要选用。也可以用<constructor-arg>的子元素<ref>或<value>来指定。type属性可指定数据类型,这个属性很鸡肋,基本不用。

可以写成这种形式:

<bean name="" class=""><constructor-arg index/name=""><value></value></constructor-arg><constructor-arg index/name=""><ref bean="" /></constructor-arg></bean>

依然是一个<constructor-arg>传递一个实参,index/name可缺省。

<value>元素中,如果实参是String、char,不加引号。比如<value>张三</value>,会自动识别类型,2个字符的String。<value>"张三"</value>也是String,但实参是4个字符。

<ref />只能是单标签形式。

参数可以是数组类型

<constructor-arg><array><value></value><value></value></array></constructor-arg>

<value>、<ref />根据情况选用。

参数可以是List、Map、Set类型

private List<String> list;public Student(List<String> list){this.list=list;}

<bean name="" class=""><constructor-arg><util:list><value></value><value></value></util:list></constructor-arg></bean>

一个<util:list>传递一个List,一个<value>表示一个列表项,<value>只能传递Java基础类型。如果是其它的Bean,要用<ref />:

<constructor-arg><util:list><ref bean="" /><ref bean="" /></util:list></constructor-arg>

<util:list>和<list>的效果一样,使用哪个都行。

Set:用法和List一样。

Map:

<bean name="zhangsan" class="my_package.Student"><constructor-arg><util:map><entry key="name" value="张三" /><entry key="age" value="20" /></util:map></constructor-arg></bean>

一个<entry>表示一个键值对,key、value表示的是基础类型,如果是其它的Bean,用key-ref、value-ref。

说明:

因为<list>元素对应得数据类型是List,<set>对应Set,<map>对应Map,所以形参只能是List/Set/Map类型,不能是ArrayList/HashSet/HashMap等子类的类型,但可以使用泛型。<list>和<util:list>效果一样,<set>和<util:set>效果一样,<map>、<util:map>效果一样。如果参数是基础数据类型或是其它的Bean,可以写成<constructor-arg index/name="" value="" />单标签的形式,如果参数是数组、集合这种有多项的数据类型,就需要写成<constructor-arg></constructor-arg>双标签的形式。

<bean>的子元素——<property>

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

给setter方法传递参数,一个setter方法设置一个属性,一个<property>给一个setter方法传递参数(传递一个参数)。

如果Bean有多个setter方法,可使用多个<property>传递参数。

name指定形参名。value指定值(只能为Java基础类型),或者用ref指定其它Bean。当然也可以用对应的子元素。

同<constructor-arg>一样,<property>可以使用数组、集合,用法相同。

注意:

和<constructor-arg>不同,<property>只能用name,不能用index。

因为<constructor-arg>是向构造函数传递一个参数,构造函数的形参表是有序的,可用index指定,也可用name指定。而Bean的多个setter方法是无序的,只能通过name指定。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。