最小化Spring XML配置
Spring提供两种技巧,可以帮助我们减少XML的配置数量。
1.自动装配(autowiring)有助于减少甚至消除配置<property>元素和<constructor-arg>元素,让Spring自动识别如何装配Bean的依赖关系。
2.自动检测(autodiscovery)比自动装配更进一步,让Spring自动识别哪些类需要被配置成Spring Bean,从而减少对<bean>元素的使用。
一、4种类型的自动装配
1.byName,把与Bean的属性具有相同名字(或者ID)的其他Bean自动装配到Bean的对应属性中。如果没有跟属性的名字相匹配的Bean,则该属性不进行装配。
<bean id="instrument" class="cn.edu.stu.springidol.Saxphone" /> <bean id="kenny2" class="cn.edu.stu.springidol.Instrumentalist" autowire="byName">
<property name="song" value="Jingle Bells" />
</bean>
Saxphone的id属性与kenny的instrument属性的名字是一样的。
缺点:需要假设Bean的名字与其他Bean的属性名字一样。
2.byType,把与Bean的属性具有相同类型的其他Bean自动装配到Bean的对应属性中,如果没有跟属性的类型相匹配的Bean,则该属性不被装配。
<bean id="saxphone" class="cn.edu.stu.springidol.Saxphone" /> <bean id="kenny2" class="cn.edu.stu.springidol.Instrumentalist" autowire="byType">
<property name="song" value="Jingle Bells" />
</bean>
缺点:如果Spring寻找到多个Bean的类型与需要自动装配的类型都相匹配,Spring不会猜测哪一个Bean更适合装配,而是抛出一个异常,所以应用只允许存在一个Bean与需要自动装配的属性类型相匹配。
两种解决方法可以解决byType带来的歧义:
1.声明候选Bean的primary属性为true,但默认的primary属性是true的,所以必须声明除候选Bean外的其他非候选Bean的primary属性为false
2.设置在自动装配时被忽略的Bean,autowire-candidate属性为false,要求Spring在自动装配时,忽略Bean作为候选Bean
3.constructor,把与Bean的构造器入参具有相同类型的其他Bean自动装配到Bean构造器的对应入参中。
如果通过构造器注入来配置Bean,可以移除<constructor-arg>元素
<bean id="duke2" class="cn.edu.stu.springidol.PoeticJuggler" autowire="constructor" />
缺点:当发现多个Bean匹配构造器入参,Spring不会猜测哪一个更适合,此外,如果一个类有多个构造器都满足自动装配条件,Spring也不会猜测哪一个更适合。
4.autodetect,首先尝试使用constructor进行自动装配,如果失败,再尝试使用byType进行自动装配。
2.5之前还可以使用,3.0之后就没有了。
默认自动装配:
在<beans>上增加一个default-autowire属性,作用于Spring配置文件的所有Bean,可以用<bean>元素的autowire属性覆盖<beans>的默认自动装配策略。
还可以使用混合使用自动装配和显示装配。
二、使用注解装配
Spring容器默认禁用注解装配,启用方式的使用context命名空间配置中的<context:annotation-config>元素。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config /> </beans>
1.使用@Autowired
不仅可以标注setter方法,还可标注需要自动装配Bean引用的任意方法,构造器,属性。
可选的自动装配,属性不一定非要装配,null值也可以接受,通过设置required属性为false来配置自动装配是可选的。但是当使用构造器装配时,只有一个构造器可以将required属性设置为true,其他使用@Autowired注解标注的构造器只能将required设置为false,当使用@Autowired标注多个构造器,Spring选择满足装配条件中入参最多的那个。
限定歧义性的依赖,可能有至少两个Bean完全满足装配条件,如何选择。
使用Qualifier("beanID")明确指定注入ID为beanID的Bean
或者:
XML声明Bean
<bean id="saxphone" class="cn.edu.stu.springidol.Saxphone">
<qualifier value="stringed" />
</bean>
@Autowired
@Qualifier("stringed")
private Instrument instrument;
还可以创建自定义的限定器。
2.@Inject实现基于标准的自动装配
可以用来自动装配属性、方法和构造器,与@Autowired不同的是,没有required属性。
限定@Inject所标注的属性,使用@Named("beanID")
创建自定义限定器,与@Autowired类似。
在注解中使用表达式:
@Value("Eruption")
private String song; @Value("#{systemProperties.myFavoriteSong}")
private String song;
三、自动检测Bean
<context:annotation-config>有助于消除<property>和<constructor-arg>元素,但仍需要使用<bean>显示定义Bean。
<context:component-scan>除了完成<context:annotation-config>一样的工作,还允许spring自动检测Bean和定义Bean,这意味着不使用<bean>元素。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan
base-package="cn.edu.stu.springidol">
</context:component-scan> </beans>
<context:component-scan>会扫描指定的包及其所有子包,并找出能够自动注册为Spring的类。
如何知道哪些类需要注册为Spring Bean?
1.为自动检测标注Bean
<context:component-scan>会查找使用构造型注解所标注的类
i.@Component,通用的构造性注解,标识该类为Spring组件
ii.@Controller,标识该类定义为Spring MVC controller
iii.@Repository,标识该类定义为数据仓库
iv.@Service,标识该类定义为服务
v.使用@Component标注的任意自定义注解
import org.springframework.stereotype.Component; @Component
public class Guitar implements Instrument { @Override
public void play() {
System.out.println("Strum Strum Strum"); } }
Guitar会被自动地注册为Spring Bean,ID默认为无限定类名,这里是guitar,可以通过@Component("anotherName")显式命名ID。
2.过滤组件扫描
使用<context:include-filter>告知哪些类需要注册为Spring Bean,使用<context:exclude-filter>告知哪些类不需要注册为Spring Bean。
5种过滤器类型:
i.annotaion,使用指定注解所标注的类,通过expression属性指定要扫描的注解
ii.assignable,扫描派生于expression属性指定类型的那些类
iii.aspject,扫描expression属性所指定的AspectJ表达式所匹配的那些类
iv.custom,使用自定义的org.springframework.core.type.TypeFilter实现类,该类由expression指定
v.regex,扫描类的名称与expression属性所指定的正则表达式所匹配的那些类
四、使用Spring基于Java的配置(为了不使用XML为目的)
1.创建基于Java的配置
首先启动<context:component-scan>,这需要少量的XML配置,
<context:component-scan>会自动注册那些使用某种构造型注解所标注的Bean,但它也会自动加载使用@Configuration注解所标注的类。
2.定义一个配置类(类放在base-package指定的包下)
@Configuration
public class SpringIdolConfig { }
3.声明一个简单Bean
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class SpringIdolConfig {
@Bean
public Performer duke() {
return new Juggler();
}
}
使用@Bean注解标注的duke方法会告知Spring我们希望该方法定义的Bean被注册进Spring的应用上下文中,因此,在其他Bean的声明方法中引用这个方法,Spring会拦截该方法的调用,并尝试在应用上下文中查找该Bean,而不是让方法创建一个新的实例。
最小化Spring XML配置的更多相关文章
- 2015年12月10日 spring初级知识讲解(二)最小化Spring XML配置 注解
序,随着Spring容器管理Bean数量增加,XML文件会越来越大,而且纯手工配置XML很繁琐,Spring和JAVA都提供了一些注解方式用以简化XML配置. 目录 一.自动装配(autowiring ...
- [Spring]04_最小化Spring XML配置
4.1 自动装配 Bean Spring 装配 bean 时,有时非常明确,就是需要将某个 bean 的引用装配给指定属性. 例如,若应用上下文中只有一个 javax.sql.DataSource 类 ...
- Spring学习笔记—最小化Spring XML配置
自动装配(autowiring)有助于减少甚至消除配置<property>元素和<constructor-arg>元素,让Spring自动识别如何装配Bean的依赖关系. 自动 ...
- 最小化spring XML配置,Spring提供了4种自动装配策略。
1.ByName自动装配:匹配属性的名字 在配置文件中的写法: <bean name="course" class="course类的全包名">&l ...
- 最小可用 Spring MVC 配置
[最小可用 Spring MVC 配置] 1.导入有概率用到的JAR包, -> pom.xml 的更佳实践 - 1.0 <- <project xmlns="http:// ...
- spring xml配置注入改为手动注入过程
项目中需要使用MQ组件来接受消息,但是有的时候,在使用的时候,并不能满足spring注入的条件,无法注入.例如 在jfinal的config的afterJFinalStart中,由于jfinal集成s ...
- Centos 7最小化安装后配置
关闭SELINUX vi /etc/sysconfig/selinux SELINUX=disabled :wq 配置网卡(最小化安装后ifconfig无法使用),该配置的前提是采用 NAT模式 vi ...
- Spring XML配置里的Bean自动装配
Spring自动装配 这段是我们之前编写的代码,代码中我们使用了P命名空间 并且使用手动装配的方式将car <bean id="address" class="cn ...
- virtualbox 最小化安装centos7 配置双网卡(nat和桥接)记录----已经过期
该文章已经过期 请参考另一篇文章:virtualbox 安装centos7 配置桥接网卡,实现主机和虚拟机互通,虚拟机可以访问外网,解决无法上网问题 先说明一下为什么要配置双网卡? 配置nat ...
随机推荐
- iOS 2D绘图详解(Quartz 2D)之路径(点,直线,虚线,曲线,圆弧,椭圆,矩形)
前言:一个路径可以包含由一个或者多个shape以及子路径subpath,quartz提供了很多方便的shape可以直接调用.例如:point,line,Arc(圆弧),Curves(曲线),Ellip ...
- iOS开发——网络编程Swift篇&(六)异步Post方式
异步Post方式 // MARK: - 异步Post方式 func asynchronousPost() { //创建NSURL对象 var url:NSURL! = NSURL(string: &q ...
- C链表操作
#define _CRT_SECURE_NO_WARNINGS #include <stdlib.h> #include <string.h> #include <std ...
- asp.net常用函数
ASP.NET网络编程中常用到的27个函数集 Abs(number) 取得数值的绝对值. Asc(String) 取得字符串表达式的第一个字符ASCII 码. Atn(number) 取得一个 ...
- divide-conquer-combine(4.1 from the introduction to algorithm)
this example is from chapter 4 in <the introduction to algorithm> the main idea is all showed ...
- 实例源码--Android简单音乐播放器源码
下载源码 技术要点: 1.本地音乐管理 2.在线音乐听歌.下载 3.音频流的解码 4. HTTP通信模块 5. Sqlite数据库 6. 源码带详细的中文注释 ...... 详细介绍: 1.本 ...
- cocos2dx 背景用小尺寸图片滚动填充的方法
直接上代码 在初始化方法中添加图片: bool BackGroundLayer::init() { frameCache=CCSpriteFrameCache::sharedSpriteFrameCa ...
- python分页和session和计算时间差
分页 #!/usr/bin/env python # -*- coding:utf-8 -*- class Pagenation: def __init__(self,current_page,all ...
- oracle的控制语句if和循环语句loop while for
pl/sql的控制结构if-then if-then-else if-then-elsif-else 案例1:编写一个过程,可以输入一个雇员名,如果该雇员的工资低于2000,就给该雇员工资增加10% ...
- 用python理解web并发模型
最简单的并发 import socket response = 'HTTP/1.1 200 OK\r\nConnection:Close\r\nContent-Length:11\r\n\r\nHel ...