序,随着Spring容器管理Bean数量增加,XML文件会越来越大,而且纯手工配置XML很繁琐,Spring和JAVA都提供了一些注解方式用以简化XML配置。

目录

一、自动装配(autowiring)
1 byName
2 byType
3 constructor
4 autodetect
5 默认自动装配
二、使用注解自动装配
1 使用@AutoWired
2 使用@Qualifier限定歧义性的依赖
3 @Inject
4 @Named
三、自动检测(autodiscovery)
1 构造型(stereotype)注解
2 过滤组件扫描
四、使用Spring基于Java的配置
1 定义一个配置类
2 声明一个简单的Bean

一、自动装配(autowiring)

自动装配有助于减少<property>元素和<constructor-arg>元素。

自动装配粗略有四种byName/byType/constructor/autudetect,自动装配策略用在<beans>或<bean>节点,达到减少XML配置的目的,自动装配只能适用于部分场景,要和手动装配搭配使用。

1 byName

与bean的属性具有相同名字的其他bean自动装配到bean的对应属性中。

例子:添加autowire="byName"后,缺少的参数就会寻找同名的bean名字进行装配。

<bean id="poeticDuke" class="single.spring.PoeticJuggler" autowire="byName">

2 byType

与bean的属性具有相同类型的其他bean自动装配到bean的对应属性中。

autowire="byType"

3 constructor

与bean的构造器入参具有相同类型的其他bean自动装配到bean构造器的对应入参中

autowire="constructor"

4 autodetect

首先尝试使用constructor进行自动装配,如果失败,再尝试使用byType进行自动装配。

autowire="autodetect"

5 默认自动装配

如果需要为spring应用上下文中的每个Bean配置相同的autowire属性,就可以配置相同的自动装配策略来简化配置,在<beans>上增加一个default-autowire属性。

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context "
       default-autowire
="byType">

注意事项:当使用constructor自动装配策略时,我们必须让Spring自动装配构造器的所有入参---我们不能混合使用constructor自动装配策略和<constructor-arg>元素。

二、使用注解自动装配

Spring2.5开始开始使用注解自动装配Bean的属性。

使用<context:annotation-config/>启动基于注解的自动装配。

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context ">
<context:annotation-config/>
<!-- bean declarations go here -->
</beans>

1 使用@AutoWired

org.springframework.beans.factory.annotation.Autowired

对属性的set方法、构造器、甚至是Bean引用的任意方法,Spring会对该方法执行byType自动装配。

例子

@Autowired
public void setInstrument(Instrument instrument) {
this.instrument = instrument;
}

另外也可对属性直接标注,required=false表示,当无匹配Bean时,默认赋值为null。而不会抛出NoSuchBeanDefinitionException

    @Autowired(required=false)
private Instrument instrument;

2 使用@Qualifier限定歧义性的依赖

org.springframework.beans.factory.annotation.Qualifier

当匹配的Bean太多时,需要使用Qualifier来限定范围。

例如:指定ID为guitar的bean

    @Autowired(required=false)
@Qualifier("guitar")
private Instrument instrument;

3 @Inject

javax.inject.Inject

    @Inject
private Poem poem;

4@Named

javax.inject.Named

    @Inject
@Named("guitar")
private Poem poem;

三、自动检测(autodiscovery)

自动检测比自动装配更进了一步,让spring能够自动识别哪些类需要被配置成Spring Bean,从而减少对<bean>元素的使用。

例子:base-package="com.springinaction.springidol"制动了扫描的包及其子包。

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context ">
<context:component-scan
base-package="com.springinaction.springidol">
</context:component-scan>
</beans>

1  构造型(stereotype)注解

@Component---通用的构造型注解,标识该类为Spring组件

org.springframework.stereotype.Component

@Component("name")此时为类指定ID为name,否则默认类名首字母小写。

@Controller---标识将该类定义为Spring MVC controller

org.springframework.stereotype.Controller

@Repository---标识该类定义为数据仓库

org.springframework.stereotype.Repository

@Service---标识该类定义为服务

org.springframework.stereotype.Service

2 过滤组件扫描

配置Instrument类的所有子类都自动注册为Spring容器的Bean

    <context:component-scan base-package="com.springinaction.springidol">
<context:include-filter type="assignable" expression="com.springinaction.springidol.Instrument" />
</context:component-scan>

除了使用自定义@SkipIt注解的类,其他所有的Instrument实现都需要注册为Spring容器的Bean。

    <context:component-scan base-package="com.springinaction.springidol">
<context:include-filter type="assignable" expression="com.springinaction.springidol.Instrument" />
<context:exclude-filter type="annotation" expression="com.springinaction.springidol.SkipIt" />
</context:component-scan>

过滤器类型type的取值及示意:

过滤器类            型               描述                                            
annotation 过滤器扫描使用指定注解所标注的那些类。通过expression属性指定要扫描的注解
assignable 过滤器扫描派生于expression属性所指定类型的那些类
aspectj 过滤器扫描与expression属性所指定的AspectJ表达式所匹配的那些类
custom 使用自定义的org.springframework.core.type.TypeFilter实现类,该类由expression属性指定
regex 过滤器扫描类的名称与expression属性所指定的正则表达式所匹配的那些类。

四、使用Spring基于Java的配置

spring 3.0提供了利用纯粹Java代码来配置spring应用了。

<context:component-scan base-package="com.springinaction.springidol" />不仅自动注册那些使用构造型(stereotype)注解所标注的Bean,而且也会自动加载使用@Configuration注解所标注的类。

1 定义一个配置类

@Configuration注解等价于XML中<beans>元素

package com.springinaction.springidol;
import org.springframework.context.annotation.Configuration; @Configuration
public class Juggler{
//Bean declaration methods go here
}

2 声明一个简单的Bean

org.springframework.context.annotation.Bean

@Bean等价于<bean>,告知Spring将返回一个对象,方法名duke做为该Bean的ID,

    @Bean
public Performer duke(){
return new Juggler();
}

2015年12月10日 spring初级知识讲解(二)最小化Spring XML配置 注解的更多相关文章

  1. Network Real Trace Analysis 2015年12月10日

    了解网络中真实的流量,国内很难找到巨人的肩膀. WAND是新西兰waikato 大学计算机系的研究小组,主要做网络测量,大规模网络流量捕获,网络分析.还做专业的分析软件. libtrace是其开源的分 ...

  2. 2015年12月10日 spring初级知识讲解(三)Spring消息之activeMQ消息队列

    基础 JMS消息 一.下载ActiveMQ并安装 地址:http://activemq.apache.org/ 最新版本:5.13.0 下载完后解压缩到本地硬盘中,解压目录中activemq-core ...

  3. 2015年12月01日 GitHub入门学习(二)手把手教你Git安装

    序:Mac与Linux中,Mac都预装了Git,各版本的Linux也都提供了Git的软件包.下面手把手教你Windows下的安装. 一.Git Windows GUI 下载地址 msysgit htt ...

  4. 2015年12月13日 spring初级知识讲解(四)面向切面的Spring

    2015年12月13日 具体内容待补充...

  5. 我的Python成长之路---第一天---Python基础(作业2:三级菜单)---2015年12月26日(雾霾)

    作业二:三级菜单 三级菜单 可一次进入各个子菜单 思路: 这个题看似不难,难点在于三层循环的嵌套,我的思路就是通过flag的真假来控制每一层的循环的,简单来说就是就是通过给每一层循环一个单独的布尔变量 ...

  6. 我的Python成长之路---第一天---Python基础(1)---2015年12月26日(雾霾)

    2015年12月26日是个特别的日子,我的Python成之路迈出第一步.见到了心目中的Python大神(Alex),也认识到了新的志向相投的伙伴,非常开心. 尽管之前看过一些Python的视频.书,算 ...

  7. 2015年12月28日 Java基础系列(六)流

    2015年12月28日 Java基础系列(六)流2015年12月28日 Java基础系列(六)流2015年12月28日 Java基础系列(六)流

  8. 2016年12月10日 星期六 --出埃及记 Exodus 21:5

    2016年12月10日 星期六 --出埃及记 Exodus 21:5 "But if the servant declares, `I love my master and my wife ...

  9. 【C++】命令行Hangman #2015年12月15日 00:20:27

    增加了可以在构造Hangman对象时通过传入参数设定“最大猜测次数”的功能.少量修改.# 2015年12月15日 00:20:22 https://github.com/shalliestera/ha ...

随机推荐

  1. Unity连Photon服务器入门详解

    Photon是目前比较好用的游戏服务器.目前网上对于Photon的服务器讲解比较少,最近也对Photon做了初步的了解,做一个极其详细的入门. 首先就是得下载Photon咯 https://www.p ...

  2. AngularJs Cookies 操作

    $cookiesProvider 使用$cookiesProvider改变$cookies服务的默认行为. 默认属性 path:字符串,cookies只在这个路径及其子路径可用.默认情况下,这个将会是 ...

  3. Redis 学习笔记

    1 Redis优势 性能极高 – Redis能读的速度是110000次/s,写的速度是81000次/s . 丰富的数据类型 – Redis支持二进制案例的 Strings, Lists, Hashes ...

  4. HTML5学习总结-09 拖放和手机触屏事件

    一 拖放 拖放(Drag 和 drop)是 HTML5 标准的组成部分.拖放是一种常见的特性,即抓取对象以后拖到另一个位置.在 HTML5 中,拖放是标准的一部分,任何元素都能够拖放. 课程参考 ht ...

  5. RMQ模板

    RMQ:范围最小值问题.给出一个n个元素的数组A1,A2,...,An,设计一个数据结构支持查询操作Query(L,R):计算min{AL,AL+1,...,AR}. 每次用一个循环来求最小值显然不够 ...

  6. oneM2M启动Release 3标准化,华为引领物联网技术布局

    http://developer.huawei.com/cn/ict/news/cn/2016/06/onem2m [韩国,首尔,2016年6月] 国际权威的物联网组织oneM2M召开第23次技术全会 ...

  7. _mkdir

    [内容摘要]: C语言 在VS2013环境下使用_mkdir返回值是-,而且文件夹不存在,#include stdio.h#include direct.hmain(){)printf("无 ...

  8. MySQL增加列,修改列名、列属性,删除列

    mysql修改表名,列名,列类型,添加表列,删除表列 alter table test rename test1; --修改表名 alter table test add  column name v ...

  9. 10月16日下午MySQL数据库CRUD操作(增加、删除、修改、查询)

    1.MySQL注释语法--,# 2.2.后缀是.sql的文件是数据库查询文件. 3.保存查询. 关闭查询时会弹出提示是否保存,保存的是这段文字,不是表格(只要是执行成功了表格已经建立了).保存以后下次 ...

  10. thinkphp连接数据库

    版本:3.1.1 连接数据库的具体位置 thinkphp/Config/convention.php,默认修改数据库在这里就可以了 但是为了方便,把数据库配置写到Index/Conf/config.p ...