Spring属性注入实例讲解Word格式.docx

上传人:b****5 文档编号:16634249 上传时间:2022-11-25 格式:DOCX 页数:15 大小:19.42KB
下载 相关 举报
Spring属性注入实例讲解Word格式.docx_第1页
第1页 / 共15页
Spring属性注入实例讲解Word格式.docx_第2页
第2页 / 共15页
Spring属性注入实例讲解Word格式.docx_第3页
第3页 / 共15页
Spring属性注入实例讲解Word格式.docx_第4页
第4页 / 共15页
Spring属性注入实例讲解Word格式.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

Spring属性注入实例讲解Word格式.docx

《Spring属性注入实例讲解Word格式.docx》由会员分享,可在线阅读,更多相关《Spring属性注入实例讲解Word格式.docx(15页珍藏版)》请在冰豆网上搜索。

Spring属性注入实例讲解Word格式.docx

publicvoidsetMapValue(MapmapValue){

this.mapValue=mapValue;

 

Spring配置文件:

<

beanid="

bean1"

class="

com.spring.Bean1"

>

<

propertyname="

strValue"

value="

Hello"

/>

!

--

intValue"

123"

-->

value>

123<

/value>

/property>

listValue"

list>

list1<

list2<

/list>

setValue"

set>

set1<

set2<

/set>

arrayValue"

array1<

array2<

mapValue"

map>

entrykey="

k1"

v1"

k2"

v2"

/map>

/bean>

2、对象属性的注入

什么是属性编辑器,作用?

*自定义属性编辑器,spring配置文件中的字符串转换成相应的对象进行注入

spring已经有内置的属性编辑器,我们可以根据需求自己定义属性编辑器

如何定义属性编辑器?

*继承PropertyEditorSupport类,覆写setAsText()方法

*将属性编辑器注册到spring中

在上面Bean1中加入Date引用类型的属性及相应的方法

privateDatedateValue;

publicDategetDateValue(){

returndateValue;

publicvoidsetDateValue(DatedateValue){

this.dateValue=dateValue;

写相应的java.util.Date属性编辑器类

importjava.beans.PropertyEditorSupport;

importjava.text.ParseException;

importjava.text.SimpleDateFormat;

/**

*

*@authorAdministrator

*

*/

publicclassUtilDatePropertyEditorextendsPropertyEditorSupport{

privateStringformat="

yyyy-MM-dd"

;

@Override

publicvoidsetAsText(Stringtext)throwsIllegalArgumentException{

System.out.println("

UtilDatePropertyEditor.saveAsText()--text="

+text);

SimpleDateFormatsdf=newSimpleDateFormat(format);

try{

Dated=sdf.parse(text);

this.setValue(d);

}catch(ParseExceptione){

e.printStackTrace();

}

publicvoidsetFormat(Stringformat){

this.format=format;

在配置文件中加入

dateValue"

2008-08-15<

--定义属性编辑器-->

customEditorConfigurer"

org.springframework.beans.factory.config.CustomEditorConfigurer"

customEditors"

java.util.Date"

beanclass="

com.spring.UtilDatePropertyEditor"

format"

/entry>

3、公共属性的注入

*通过<

bean>

标签定义公共的属性,指定abstract=true

*具有相同属性的类在<

标签中指定其parent属性

publicclassBean2{

privateBean3bean3;

privateBean4bean4;

privateBean5bean5;

publicBean3getBean3(){

returnbean3;

publicvoidsetBean3(Bean3bean3){

this.bean3=bean3;

publicBean4getBean4(){

returnbean4;

publicvoidsetBean4(Bean4bean4){

this.bean4=bean4;

publicBean5getBean5(){

returnbean5;

publicvoidsetBean5(Bean5bean5){

this.bean5=bean5;

publicclassBean3{

privateintid;

privateStringname;

privateStringpassword;

publicintgetId(){

returnid;

publicvoidsetId(intid){

this.id=id;

publicStringgetName(){

returnname;

publicvoidsetName(Stringname){

this.name=name;

publicStringgetPassword(){

returnpassword;

publicvoidsetPassword(Stringpassword){

this.password=password;

publicclassBean4{

publicclassBean5{

privateintage;

publicintgetAge(){

returnage;

publicvoidsetAge(intage){

this.age=age;

bean2"

com.spring.Bean2"

bean3"

ref="

bean4"

refbean="

bean5"

beanAbstract"

abstract="

true"

id"

1000"

name"

Jack"

com.spring.Bean3"

parent="

Tom"

password"

com.spring.Bean4"

com.spring.Bean5"

age"

20"

////////////////////////

Spring的核心机制依赖注入简介

2006-09-3008:

00作者:

出处:

csdn责任编辑:

方舟  设值注入

  设值注入是指通过setter方法传入被调用者的实例。

这种注入方式简单、直观,因而在Spring的依赖注入里大量使用。

看下面代码,是Person的接口

//定义Person接口

publicinterfacePerson

{

 //Person接口里定义一个使用斧子的方法

 publicvoiduseAxe();

  然后是Axe的接口

//定义Axe接口

publicinterfaceAxe

 //Axe接口里有个砍的方法

 publicvoidchop();

}

  Person的实现类

//Chinese实现Person接口

publicclassChineseimplementsPerson

 //面向Axe接口编程,而不是具体的实现类

 privateAxeaxe;

 //默认的构造器

 publicChinese()

 {}

 //设值注入所需的setter方法

 publicvoidsetAxe(Axeaxe)

 {

  this.axe=axe;

 }

 //实现Person接口的useAxe方法

 publicvoiduseAxe()

  System.out.println(axe.chop());

  Axe的第一个实现类

//Axe的第一个实现类StoneAxe

publicclassStoneAxeimplementsAxe

 //默认构造器

 publicStoneAxe()

 //实现Axe接口的chop方法

 publicStringchop()

  return"

石斧砍柴好慢"

  下面采用Spring的配置文件将Person实例和Axe实例组织在一起。

配置文件如下所示:

<!

--下面是标准的XML文件头-->

<?

xmlversion="

1.0"

encoding="

gb2312"

?

--下面一行定义Spring的XML配置文件的dtd-->

"

http:

//www.springframework.org/dtd/spring-beans.dtd"

--以上三行对所有的Spring配置文件都是相同的-->

--Spring配置文件的根元素-->

<BEANS>

 <!

—定义第一bean,该bean的id是chinese,class指定该bean实例的实现类-->

 <BEANclass=lee.Chineseid=chinese>

--property元素用来指定需要容器注入的属性,axe属性需要容器注入此处是设值注入,因此Chinese类必须拥有setAxe方法-->

<propertyname="

axe"

--此处将另一个bean的引用注入给chinesebean-->

<REFlocal="

”stoneAxe”/"

</property>

</BEAN>

--定义stoneAxebean-->

<BEANclass=lee.StoneAxeid=stoneAxe/>

</BEANS>

  从配置文件中,可以看到Spring管理bean的灵巧性。

bean与bean之间的依赖关系放在配置文件里组织,而不是写在代码里。

通过配置文件的指定,Spring能精确地为每个bean注入属性。

因此,配置文件里的bean的class元素,不能仅仅是接口,而必须是真正的实现类。

  Spring会自动接管每个bean定义里的property元素定义。

Spring会在执行无参数的构造器后、创建默认的bean实例后,调用对应的setter方法为程序注入属性值。

property定义的属性值将不再由该bean来主动创建、管理,而改为被动接收Spring的注入。

  每个bean的id属性是该bean的惟一标识,程序通过id属性访问bean,bean与bean的依赖关系也通过id属性完成。

  下面看主程序部分:

publicclassBeanTest

 //主方法,程序的入口

 publicstaticvoidmain(String[]args)throwsException

  //因为是独立的应用程序,显式地实例化Spring的上下文。

  ApplicationContextctx=newFileSystemXmlApplicationContext("

bean.xml"

);

  //通过Personbean的id来获取bean实例,面向接口编程,因此

  //此处强制类型转换为接口类型

  Personp=(Person)ctx.getBean("

chinese"

  //直接执行Person的userAxe()方法。

  p.useAxe();

  程序的执行结果如下:

  石斧砍柴好慢

  主程序调用Person的useAxe()方法时,该方法的方法体内需要使用Axe的实例,但程序里没有任何地方将特定的Person实例和Axe实例耦合在一起。

或者说,程序里没有为Person实例传入Axe的实例,Axe实例由Spring在运行期间动态注入。

  Person实例不仅不需要了解Axe实例的具体实现,甚至无须了解Axe的创建过程。

程序在运行到需要Axe实例的时候,Spring创建了Axe实例,然后注入给需要Axe实例的调用者。

Person实例运行到需要Axe实例的地方,自然就产生了Axe实例,用来供Person实例使用。

  调用者不仅无须关心被调用者的实现过程,连工厂定位都可以省略(真是按需分配啊!

)。

下面也给出使用Ant编译和运行该应用的简单脚本:

--定义编译该项目的基本信息-->

<PROJECTname="

spring"

default="

."

basedir="

--定义编译和运行该项目时所需的库文件-->

<PATHid=classpath>

--该路径下存放spring.jar和其他第三方类库-->

 <FILESETdir=..\..\lib>

  <INCLUDEname="

*.jar"

/>

 </FILESET>

--同时还需要引用已经编译过的class文件-->

 <PATHELEMENTpath="

</PATH>

--编译全部的java文件-->

<TARGETdescription="

Compileallsourcecode"

name="

compile"

--指定编译后的class文件的存放位置-->

<JAVACdebug="

destdir="

 deprecation="

false"

optimize="

failonerror="

--指定需要编译的源文件的存放位置-->

 <SRCpath="

--指定编译这些java文件需要的类库位置-->

 <CLASSPATHrefid="

classpath"

</JAVAC>

</TARGET>

--运行特定的主程序-->

runthemainclass"

run"

depends="

--指定运行的主程序:

lee.BeanTest。

-->

<JAVAfailonerror="

fork="

yes"

classname="

lee.BeanTest"

--指定运行这些java文件需要的类库位置-->

</JAVA>

</PROJECT>

  如果需要改写Axe的实现类。

或者说,提供另一个实现类给Person实例使用。

Person接口、Chinese类都无须改变。

只需提供另一个Axe的实现,然后对配置文件进行简单的修改即可。

  Axe的另一个实现如下:

//Axe的另一个实现类SteelAxe

publicclassSteelAxeimplementsAxe

 publicSteelAxe()

钢斧砍柴真快"

  然后,修改原来的Spring配置文件,在其中增加如下一行:

--定义一个steelAxebean-->

<BEANclass=lee.SteelAxeid=steelAxe/>

  该行重新定义了一个Axe的实现:

SteelAxe。

然后修改chinesebean的配置,将原来传入stoneAxe的地方改为传入steelAxe。

也就是将

  改成

”steelAxe”/"

  此时再次执行程序,将得到如下结果:

  钢斧砍柴真快

  Person与Axe之间没有任何代码耦合关系,bean与bean之间的依赖关系由Spring管理。

采用setter方法为目标bean注入属性的方式,称为设值注入。

  业务对象的更换变得相当简单,对象与对象之间的依赖关系从代码里分离出来,通过配置文件动态管理。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 语文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1