1.5 指定Bean引用
为了Bean之间相互访问,在Bean配置文件中通过<ref>元素为Bean属性或构造程序参数指定Bean引用。
<property name="prefixGenerator">
<ref bean="datePrefixGenerator"> 如果在同一个配置文件,可以使用local属性
</property>
简写:<property name="prefixGenerator" ref="datePrefixGenerator"/>
pschema简写:
<bean id="a" class="xxx" p:prefixGenerator-ref="datePrefixGenerator"/> -ref 区分Bean引用与简单属性值
为构造函数指定Bean引用
<constructor-arg>
<ref local="datePrefixGenerator"/>
</constructor-arg>
简写:<constructor-arg ref="datePrefixGenerator"/>
声明内部bean
<property name="prefixGenerator">
<bean class="com.wei.test.DatePrefixGenerator">
<property name="pattern" value="yyyyMMdd"/>
</bean>
</property
构造函数写法:
<constructor-arg>
<bean class="com.wei.test.DatePrefixGenerator">
<property name="pattern" value="yyyyMMdd"/>
</bean>
</constructor-arg>

1.6 为集合元素指定数据类型
设置<value>标记的type属性指定元素类型
<property name="suffixes">
<list>
<value type="int">5</value>
<value type="int">10</value>
<value type="int">15</value>
</list>
</property>
或者设置集合标记的value-type属性指定集合所有元素的类型
<property name="suffixes">
<list value-type="int">
<value>5</value>
<value>10</value>
<value>15</value>
</list>
</property>
在java 1.5以上版本,可以用存储整数的类型安全集合定义suffixes列表,就不用再指定value-type属性。
private List<Integer> suffixes;

1.8 使用工厂Bean和Utility Schema定义集合
集合工厂Bean:ListFactoryBean、SetFactoryBean、MapFactoryBean
集合标记:<util:list>、<util:set>、<util:map>

1.10 用@Required注解检查属性
RequiredAnnotationBeanPostProcessor 是一个Spring Bean后处理器,检查带有@Required注解的所有bean属性是否设置。在每个Bean初始化之前执行。需要在Spring IoC容器中注册。只能检查属性是否已经设置,不能测试属性是否非空。
<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
或者,Spring 2.5以上
<context:annotation-config/>
自定义注解检查属性
package com.wei.test;
...
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Mandatory {

}

将注解应用到必要属性的设置方法
@Mandatory
public void setPrefixGenerator(PrefixGenerator prefixGenerator) {
this.prefixGenerator = prefixGenerator;
}

为了使用这个注解,需要在RequiredAnnotationBeanPostProcessor的requiredAnnotationType属性中指定
<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor">
<property name="requiredAnnotationType">
<value>com.wei.test.Mandatory</value>
</property>
</bean>

1.11 用XML配置自动装配Bean
<bean>的autowire属性指定自动装配模式,Spring支持的自动装配模式有:
no*
byName
byType
Constructor:小心避免歧义
autodetect
<beans>根元素的default-autowire属性,这个默认模式将被Bean自己指定的模式覆盖。

1.12 用@Autowired和@Resource自动装配Bean
Spring2.5起,可以用@Autowired和@Resource注解一个设置方法、构造程序、字段甚至任意方法自动装配特定的属性。
为了让Spring自动装配具有@Autowired和@Resource注解的属性,必须在IoC容器注册一个AutowiredAnnotationBeanPostProcessor实例。
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

<context:annotation-config/> 自动注册一个AutowiredAnnotationBeanPostProcessor实例。
------“自动装配一个兼容类型的Bean” @Autowired
默认情况下,@Autowired的属性都是必需的,如果希望是可选的,可将@Autowired的required设置为false。
@Autowired(required=false)
@Autowired
private PrefixGenerator[] prefixGenerators;

@Autowired
private List<PrefixGenerator> prefixGenerators;

@Autowired
private Map<String,PrefixGenerator> prefixGenerators;

@Autowired
@Qualifier("datePrefixGenerator")
指定一个候选Bean,当按照类型的自动装配在IoC容器中有超过一个类型兼容的Bean时不会报错。

@Qualifier 也可以应用到方法参数中进行自动装配
private void inject(@Qualifier("datePrefixGenerator") PrefixGenerator prefixGenerator){
this.prefixGenerator = prefixGenerator;
}

如果你希望一种特殊的Bean和配置在注解装饰字段或者设值方法时注入,可以创建一个自定义的限定符注解类型,这种类型必须用@Qualifier注解。
package com.wei.test;
...
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD,ElementType.PARAMETER})
@Qualifier
public @interface Generator {

String value();
}

@Autowired
@Generator("prefix")
private PrefixGenerator prefixGenerator;

<bean id="datePrefixGenerator" class="com.wei.test.DatePrefixGenerator">
<qualifier type="Generator" value="prefix"/>
<property name="pattern" value="yyyyMMdd"/>
</bean>

------“按照名称自动装配” @Resource
为一个设值方法、构造程序或者字段加上注解。默认情况下,Spring试图找到一个与属性同名的Bean。但是可以显式地在name属性中指定Bean的名称。
依赖JSR-250
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>1.0</version>
</dependency>

@Autowired
@Resource("datePrefixGenerator")
private PrefixGenerator prefixGenerator;

1.13 继承Bean配置
parent属性
如果希望父Bean只作为模板而不能检索,必须将abstract设置为true,要求Spring不要实例化这个Bean。
<bean id="sequenceGenerator" parent="baseSequenceGenerator"/>

1.14 从Classpath中扫描组件
组件扫描,利用特殊的典型化注解,从classpath中自动地扫描、检测、实例化组件。@Component基本注解、(@Repository、@Service、@Controller典型化注解)
<context:component-scan base-package="com.wei" />
使用分号分隔多个扫描包。
这个元素,将注册一个AutowiredAnnotationBeanPostProcessor实例,这个实例能够自动装配带有@Autowired注解的属性。
过滤扫描的组件:
<context:component-scan base-package="com.wei">
<context:include-filter type="regex" expression="com\.apress\..*Dao.*"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
Spring支持4种过滤器表达式:annotation、assignable、regex、aspectj。
如果用include过滤器检测所有名称包含Dao的类,***DaoImpl就能在没有典型化注解的情况下被自动检测出来。

spring 攻略的更多相关文章

  1. 《spring 攻略》笔记1

    chapter1 spring简介 两种spring ioc容器实现类型: BeanFactory ApplicationContext 应用程序上下文 DI技巧: @Autowired(requir ...

  2. 【JAVA EE企业级开发四步走完全攻略】

    本文是J2EE企业级开发四步走完全攻略索引,因内容比较广泛,涉及整个JAVA EE开发相关知识,这是一个长期的计划,单个发blog比较零散,所以整理此索引,决定以后每发一季JAVA EE blog后会 ...

  3. Java - 框架之 SpringBoot 攻略day01

          Spring-Boot 攻略 day01 spring-boot   一. 基本配置加运行   1. 导入配置文件(pom.xml 文件中)   <parent> <gr ...

  4. 图文详解:阿里宠儿【小兔】RabbitMQ的养成攻略

  5. 【C#代码实战】群蚁算法理论与实践全攻略——旅行商等路径优化问题的新方法

    若干年前读研的时候,学院有一个教授,专门做群蚁算法的,很厉害,偶尔了解了一点点.感觉也是生物智能的一个体现,和遗传算法.神经网络有异曲同工之妙.只不过当时没有实际需求学习,所以没去研究.最近有一个这样 ...

  6. 微软MVP攻略 (如何成为MVP?一个SQL Server MVP的经验之谈)

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 初衷 什么是微软MVP? 成为微软MVP的条件? 如何成为微软MVP? (一) 申请时间划分 (二) 前期准备 (三) ...

  7. Windows下LATEX排版论文攻略—CTeX、JabRef使用介绍

    Windows下LATEX排版论文攻略—CTeX.JabRef使用介绍 一.工具介绍 TeX是一个很好排版工具,在学术界十分流行,特别是数学.物理学和计算机科学界. CTeX是TeX中的一个版本,指的 ...

  8. linux下安装apache与php;Apache+PHP+MySQL配置攻略

    1.apache   在如下页面下载apache的for Linux 的源码包    http://www.apache.org/dist/httpd/;   存至/home/xx目录,xx是自建文件 ...

  9. 生成 PDF 全攻略【2】在已有PDF上添加内容

    项目在变,需求在变,不变的永远是敲击键盘的程序员..... PDF 生成后,有时候需要在PDF上面添加一些其他的内容,比如文字,图片.... 经历几次失败的尝试,终于获取到了正确的代码书写方式. 在此 ...

随机推荐

  1. overflow知多少

    本文地址: http://www.hicss.net/some-overflow-knowledge/ 最近在研究OOCSS,当打开template.css阅读第一行时,震惊了,第一眼居然没看懂... ...

  2. 未设置BufferSize导致FTP下载速度过慢的问题

    開始下载前设置BufferSize就可以解决: ftpClient.setBufferSize(1024*1024); 查看commons-net的源代码.能够发现假设未设置该參数.将会一个字节一个字 ...

  3. vivado设计一:建立第一个入门工程(基于zybo)

    vivado设计一:建立第一个入门工程(基于zybo) 0赞 发表于 2014/6/17 23:03:25 阅读(8777) 评论(4) 软件:vivado 2013.4 电脑:xp系统 硬件:zyb ...

  4. matplotlib之legend

    在<matplotlib极坐标系应用之雷达图> 中,我们提出了这个问题“图例中每种成员的颜色是怎样和极坐标相应的成员的颜色相对应的呢”,那么接下来我们说说legend的一般使用和设置. 调 ...

  5. 李洪强IOS开发之iOS好项目收集

    李洪强IOS开发之iOS好项目收集 在这里收集一些最近出现的比较实用好玩的框架或者项目,会不断更新 项目 简述 日期 SCTableViewCell 类似与QQ侧滑删除Cell的Demo 201501 ...

  6. linux系统的开机引导、启动流程

    固件(CMOS/BIOS) > POST 加电自检 ↓ 自举程序(GRUB)> 加载内核 ↓ 载入内核 > 驱动硬件 ↓ 启动进程 init ↓ 读取执行配置文件/etc/initt ...

  7. cp/scp命令详解

    cp:拷贝命令 用法: cp [参数] source dest cp [参数] source ... directory 说明:将一个档案拷贝至另一个档案,或数个档案拷贝到另一目录 参数: -a 尽可 ...

  8. CCNA2.0笔记_OSPF v2

    OSPF(开放最短路径优先)协议概述: - 链路状态路由协议 - 无类路由协议 - 要点:RouterID.区域ID - 触发更新 .以传播 LSA 代替路由表更新 - 快速响应变更(比距离矢量路由协 ...

  9. 在32位Centos6.4上安装GraphicsMagick

    安装时,make总是有如下错误,最后几行 /bin/sh ./libtool --tag=CC --mode=link gcc -std=gnu99 -g -O2 -Wall -pthread -ld ...

  10. cocos2dx 富文本框,支持换行,支持神情(支持汉字截断无乱码)

    cocos2dx 富文本框,支持换行,支持表情(支持汉字截断无乱码) 小工在做了一个游戏聊天功能,里面用到插入表情的富文本和换行的问题: 先看效果,不是你要的效果,可return:(截图由于:输入的问 ...