Spring_总结_03_装配Bean之XML配置
Posted shirui
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring_总结_03_装配Bean之XML配置相关的知识,希望对你有一定的参考价值。
一、前言
本文承接上一节:Spring_总结_03_装配Bean(二)之Java配置
上一节说到,当需要显示配置时,首选类型安全并且比XML更强大Java配置。
那什么时候使用XML配置呢?
(1)维护已有XML配置
(2)想使用便利的XML命名空间,并且在JavaConfig中没有同样的实现。
二、引入schema
<?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"> </beans>
用来装配bean的最基本的XML元素包含在spring-beans模式中,在上面这个XML文件中,它被定义为根命名空间。
<beans>是该模式中的一个元素,它是所有Spring配置文件的根元素。
三、声明Bean
<bean id="compactDisc" class="soundsystem.SgtPeppers"></bean>
id :指定bean的名字,以便于被其他bean引用。
class : 全类名,指定创建bean的Class
注:
(1)通过xml配置的方式,我们不再直接负责创SgtPeppers,而在JavaConfig中,我们却是需要这样做的。当Spring发现 <bean>元素时,他会调用默认构造器来创建bean。
(2)将bean的类型以字符串的形式设置在class属性中,不便于重构。(如修改类名时)
四、构造器注入
注入bean有两种方式:
(1)构造器注入
(2)属性注入
这两种方式如何选择:强依赖选择构造器注入,可选性依赖使用属性注入。
1. constructor-arg
<?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="compactDisc" class="soundsystem.SgtPeppers"></bean> <bean id="cdPlayer" class="soundsystem.CDPlayer"> <constructor-arg ref="compactDisc" /> </bean> </beans>
2. c-命名空间
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:c="http://www.springframework.org/schema/c" 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="compactDisc" class="soundsystem.SgtPeppers"></bean> <bean id="cdPlayer" class="soundsystem.CDPlayer" c:cd-ref="compactDisc"/> <bean id="cdPlayer2" class="soundsystem.CDPlayer" c:_0-ref="compactDisc"/> </beans>
(1)构造器参数名
"c:"为命名空间
“-ref”表示注入引用
“cd”为构造器参数名
(2)构造器参数索引
“_i”为第i个参数。
表示为第i个参数注入bean
3. 字面量
将字面量注入到构造器中
(1)constructor-arg
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:c="http://www.springframework.org/schema/c" 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="blankDisc" class="soundsystem.BlankDisc"> <constructor-arg value="字面量"/> </bean> </beans>
(2)构造器参数名
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:c="http://www.springframework.org/schema/c" 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="blankDisc" class="soundsystem.BlankDisc" c:_title="标题" c:_author="作者" /> </beans>
(3)构造器参数索引
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:c="http://www.springframework.org/schema/c" 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="blankDisc" class="soundsystem.BlankDisc" c:_0="标题" c:_1="作者" /> </beans>
4. 集合
BlankDisc
public class BlankDisc implements CompactDisc{ private String title; private String author; private List<String> tracks; public BlankDisc(String title, String author, List<String> tracks) { this.title = title; this.author = author; this.tracks = tracks; } public void play(){ System.out.print("aaaaaaaa"); } }
beans.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:c="http://www.springframework.org/schema/c" 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="compactDisc" class="soundsystem.BlankDisc"> <constructor-arg value="标题"/> <constructor-arg value="作者"/> <constructor-arg> <list> <value>111111111</value> <value>222222222</value> <value>333333333</value> <value>444444444</value> <value>555555555</value> </list> </constructor-arg> </bean> <bean id="compactDisc2" class="soundsystem.BlankDisc"> <constructor-arg value="标题"/> <constructor-arg value="作者"/> <constructor-arg> <list> <ref bean="compactDisc" /> <ref bean="compactDisc" /> <ref bean="compactDisc" /> <ref bean="compactDisc" /> </list> </constructor-arg> </bean> <bean id="compactDisc3" class="soundsystem.BlankDisc"> <constructor-arg value="标题"/> <constructor-arg value="作者"/> <constructor-arg> <set> <value>111111111</value> <value>222222222</value> <value>333333333</value> <value>444444444</value> <value>555555555</value> </set> </constructor-arg> </bean> </beans>
五、属性注入
1.注入引用
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:c="http://www.springframework.org/schema/c" xmlns:p="http://www.springframework.org/schema/p" 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="compactDisc" class="soundsystem.CompactDisc" /> <bean id="cdPlayer" class="soundsystem.CDPalyer" > <property name="compactDisc" ref="compactDisc" /> </bean> <bean id="cdPlayer2" class="soundsystem.CDPalyer" p:compactDisc-ref="compactDisc"/> </beans>
2.注入字面量
BlankDisc
public class BlankDisc implements CompactDisc{ private String title; private String author; private List<String> tracks; public void setTitle(String title) { this.title = title; } public void setAuthor(String author) { this.author = author; } public void setTracks(List<String> tracks) { this.tracks = tracks; } public void play(){ System.out.print("aaaaaaaa"); } }
beans.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:c="http://www.springframework.org/schema/c" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/plugin" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/plugin http://www.springframework.org/schema/plugin/spring-plugin.xsd"> <!--1.空属性--> <bean id="reallyBlankDisc" class="soundsystem.BlankDisc" /> <!--2.注入list--> <bean id="compactBlankDisc" class="soundsystem.BlankDisc" > <property name="title" value="标题"/> <property name="author" value="作者"/> <property name="tracks" > <list> <value>11111111</value> <value>11111111</value> <value>11111111</value> <value>11111111</value> </list> </property> </bean> <!--3.p命名空间装配引用与装配字面量的唯一区别在于是否带有 -ref p命名空间不能用来装配集合 --> <bean id="compactBlankDisc2" class="soundsystem.BlankDisc" p:title="标题" p:author="作者"> <property name="tracks" > <list> <value>11111111</value> <value>11111111</value> <value>11111111</value> <value>11111111</value> </list> </property> </bean> <!--4.<util:list>会创建一个列表bean --> <util:list id="tracklist"> <value>1111111111</value> <value>1111111111</value> <value>1111111111</value> <value>1111111111</value> </util:list> <bean id="compactBlankDisc3" class="soundsystem.BlankDisc" p:title="标题" p:author="作者" p:tracks-ref="tracklist" /> </beans>
以上是关于Spring_总结_03_装配Bean之XML配置的主要内容,如果未能解决你的问题,请参考以下文章