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. SQL Server 创建和使用索引

    创建索引: (1)在SQL Server Management Studio中,选择并右击要创建索引的表,从弹出菜单中选择“设计”,打开表设计器.右键单击表设计器,从弹出菜单中选择“索引/键”命令,打 ...

  2. Linux学习一

    1.Linux的优缺点: 长处: 稳定的系统 免费或少许费用 安全性,漏洞的高速修补 多任务,多用户 用户与用户的规划 相对不耗资源的系统 适合须要小内核的嵌入式系统 整合度佳且多样的图形用户界面 缺 ...

  3. 手动安装minGW

    minGW是C语言编译包,将GCC编译器在Windows平台上编译软件提供支持. 手工安装minGW是一件很繁琐的事情,但是搞懂它很有用,因为C语言本身是一个很小的语法系统,全靠 各种库在支持,安装m ...

  4. maven添加额外archetype

    用Eclipse + m2e 插件新建maven项目时发现archetype太少了,网上搜索如何添加额外的archetype. http://maven.apache.org/archetype/ma ...

  5. Java遍历包中所有类

    PackageUtil 类 import java.io.File; import java.net.URL; import java.net.URLClassLoader; import java. ...

  6. 【网络爬虫】Httpclient4.X中使用HTTPS的方法采集12306网站

    HttpClient请求https的实例: package train; import java.io.IOException; import java.security.NoSuchAlgorith ...

  7. listView里面添加gridview

    package com.example.cc.ecustapp.Adapter; import android.content.Context;import android.util.DisplayM ...

  8. linux学习笔记10---命令nl

    nl命令在linux系统中用来计算文件中行号.nl 可以将输出的文件内容自动的加上行号! nl命令读取 file 参数(缺省情况下标准输入),计算输入中的行号,将计算过的行号写入标准输出.在输出中,n ...

  9. 2种实现CXF方法例子

    转载自:http://www.blogjava.net/sai5201314vicky/articles/353078.html 大家好,今天我要介绍的现实webservice的一种技术——CXF 由 ...

  10. 免安装Oracleclient和PL/SQL

    写在前面: Oracle是典型的C/S结构,服务端提供oracle服务的实例,主要用于数据库的管理,对象的管理与存储.数据的 存储.查询.数据库资源的监控.监听等一些服务. 而client仅仅是一个与 ...