spring注解完整版.docx

上传人:b****2 文档编号:2329408 上传时间:2022-10-28 格式:DOCX 页数:23 大小:25.86KB
下载 相关 举报
spring注解完整版.docx_第1页
第1页 / 共23页
spring注解完整版.docx_第2页
第2页 / 共23页
spring注解完整版.docx_第3页
第3页 / 共23页
spring注解完整版.docx_第4页
第4页 / 共23页
spring注解完整版.docx_第5页
第5页 / 共23页
点击查看更多>>
下载资源
资源描述

spring注解完整版.docx

《spring注解完整版.docx》由会员分享,可在线阅读,更多相关《spring注解完整版.docx(23页珍藏版)》请在冰豆网上搜索。

spring注解完整版.docx

spring注解完整版

使用Spring2.5的Autowired实现注释型的IOC161641

使用Spring2.5的新特性——Autowired可以实现快速的自动注入,而无需在xml文档里面添加bean的声明,大大减少了xml文档的维护。

(偶喜欢这个功能,因为偶对xml不感冒)。

以下是一个例子:

先编写接口Man:

publicinterfaceMan{

publicStringsayHello();

}

然后写Man的实现类Chinese和American:

@Service

publicclassChineseimplementsMan{

publicStringsayHello(){

return"IamChinese!

";

}

}

@Service

publicclassAmericanimplementsMan{

publicStringsayHello(){

return"IamAmerican!

";

}

}

@Service注释表示定义一个bean,自动根据bean的类名实例化一个首写字母为小写的bean,例如Chinese实例化为chinese,American实例化为american,如果需要自己改名字则:

@Service("你自己改的bean名")。

beans.xml

xmlversion="1.0"encoding="UTF-8"?

>

//www.springframework.org/schema/beans"

xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"

xmlns:

context="http:

//www.springframework.org/schema/context"

xsi:

schemaLocation="http:

//www.springframework.org/schema/beans

http:

//www.springframework.org/schema/beans/spring-beans-2.5.xsd

http:

//www.springframework.org/schema/context

http:

//www.springframework.org/schema/context/spring-context-2.5.xsd">

annotation-config/>

component-scanbase-package="testspring.main"/>

在spring的配置文件里面只需要加上

annotation-config/>和

component-scanbase-package="需要实现注入的类所在包"/>,可以使用base-package="*"表示全部的类。

编写主类测试:

@Service

publicclassMain{

@Autowired

@Qualifier("chinese")

privateManman;

publicstaticvoidmain(String[]args){

//TODOcodeapplicationlogichere

ApplicationContextctx=newClassPathXmlApplicationContext("beans.xml");

Mainmain=(Main)ctx.getBean("main");

System.out.println(main.getMan().sayHello());

}

publicMangetMan(){

returnman;

}

}

在Man接口前面标上@Autowired和@Qualifier注释使得Man接口可以被容器注入,当Man接口存在两个实现类的时候必须指定其中一个来注入,使用实现类首字母小写的字符串来注入。

否则可以省略,只写@Autowired

**********************

使用Spring2.5注释驱动的IoC功能

注释配置相对于XML配置具有很多的优势:

它可以充分利用Java的反射机制获取类结构信息,这些信息可以有效减少配置的工作。

如使用JPA注释配置ORM映射时,我们就不需要指定PO的属性名、类型等信息,如果关系表字段和PO属性名、类型都一致,您甚至无需编写任务属性映射信息——因为这些信息都可以通过Java反射机制获取。

注释和Java代码位于一个文件中,而XML配置采用独立的配置文件,大多数配置信息在程序开发完成后都不会调整,如果配置信息和Java代码放在一起,有助于增强程序的内聚性。

而采用独立的XML配置文件,程序员在编写一个功能时,往往需要在程序文件和配置文件中不停切换,这种思维上的不连贯会降低开发效率。

因此在很多情况下,注释配置比XML配置更受欢迎,注释配置有进一步流行的趋势。

Spring2.5的一大增强就是引入了很多注释类,现在您已经可以使用注释配置完成大部分XML配置的功能。

在这篇文章里,我们将向您讲述使用注释进行Bean定义和依赖注入的内容。

原来我们是怎么做的

在使用注释配置之前,先来回顾一下传统上是如何配置Bean并完成Bean之间依赖关系的建立。

下面是3个类,它们分别是Office、Car和Boss,这3个类需要在Spring容器中配置为Bean:

Office仅有一个属性:

清单1.Office.java

packagecom.baobaotao;

publicclassOffice{

privateStringofficeNo=”001”;

//省略get/setter

@Override

publicStringtoString(){

return"officeNo:

"+officeNo;

}

}

Car拥有两个属性:

清单2.Car.java

packagecom.baobaotao;

publicclassCar{

privateStringbrand;

privatedoubleprice;

//省略get/setter

@Override

publicStringtoString(){

return"brand:

"+brand+","+"price:

"+price;

}

}

Boss拥有Office和Car类型的两个属性:

清单3.Boss.java

packagecom.baobaotao;

publicclassBoss{

privateCarcar;

privateOfficeoffice;

//省略get/setter

@Override

publicStringtoString(){

return"car:

"+car+"\n"+"office:

"+office;

}

}

我们在Spring容器中将Office和Car声明为Bean,并注入到BossBean中:

下面是使用传统XML完成这个工作的配置文件beans.xml:

清单4.beans.xml将以上三个类配置成Bean

xmlversion="1.0"encoding="UTF-8"?

>

//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-2.5.xsd">

此部分代码在采用注释后可省略,后面有

详细的说明

当我们运行以下代码时,控制台将正确打出boss的信息:

清单5.测试类:

AnnoIoCTest.java

importorg.springframework.context.ApplicationContext;

importorg.springframework.context.support.ClassPathXmlApplicationContext;

publicclassAnnoIoCTest{

publicstaticvoidmain(String[]args){

String[]locations={"beans.xml"};

ApplicationContextctx=

newClassPathXmlApplicationContext(locations);

Bossboss=(Boss)ctx.getBean("boss");

System.out.println(boss);

}

}

这说明Spring容器已经正确完成了Bean创建和装配的工作。

使用@Autowired注释

Spring2.5引入了@Autowired注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。

来看一下使用@Autowired进行成员变量自动注入的代码:

清单6.使用@Autowired注释的Boss.java

packagecom.baobaotao;

importorg.springframework.beans.factory.annotation.Autowired;

publicclassBoss

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

当前位置:首页 > 人文社科 > 法律资料

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

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