spring 攻略
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 攻略的更多相关文章
- 《spring 攻略》笔记1
chapter1 spring简介 两种spring ioc容器实现类型: BeanFactory ApplicationContext 应用程序上下文 DI技巧: @Autowired(requir ...
- 【JAVA EE企业级开发四步走完全攻略】
本文是J2EE企业级开发四步走完全攻略索引,因内容比较广泛,涉及整个JAVA EE开发相关知识,这是一个长期的计划,单个发blog比较零散,所以整理此索引,决定以后每发一季JAVA EE blog后会 ...
- Java - 框架之 SpringBoot 攻略day01
Spring-Boot 攻略 day01 spring-boot 一. 基本配置加运行 1. 导入配置文件(pom.xml 文件中) <parent> <gr ...
- 图文详解:阿里宠儿【小兔】RabbitMQ的养成攻略
- 【C#代码实战】群蚁算法理论与实践全攻略——旅行商等路径优化问题的新方法
若干年前读研的时候,学院有一个教授,专门做群蚁算法的,很厉害,偶尔了解了一点点.感觉也是生物智能的一个体现,和遗传算法.神经网络有异曲同工之妙.只不过当时没有实际需求学习,所以没去研究.最近有一个这样 ...
- 微软MVP攻略 (如何成为MVP?一个SQL Server MVP的经验之谈)
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 初衷 什么是微软MVP? 成为微软MVP的条件? 如何成为微软MVP? (一) 申请时间划分 (二) 前期准备 (三) ...
- Windows下LATEX排版论文攻略—CTeX、JabRef使用介绍
Windows下LATEX排版论文攻略—CTeX.JabRef使用介绍 一.工具介绍 TeX是一个很好排版工具,在学术界十分流行,特别是数学.物理学和计算机科学界. CTeX是TeX中的一个版本,指的 ...
- linux下安装apache与php;Apache+PHP+MySQL配置攻略
1.apache 在如下页面下载apache的for Linux 的源码包 http://www.apache.org/dist/httpd/; 存至/home/xx目录,xx是自建文件 ...
- 生成 PDF 全攻略【2】在已有PDF上添加内容
项目在变,需求在变,不变的永远是敲击键盘的程序员..... PDF 生成后,有时候需要在PDF上面添加一些其他的内容,比如文字,图片.... 经历几次失败的尝试,终于获取到了正确的代码书写方式. 在此 ...
随机推荐
- PHP中的__call和__callStatic方法(未看完)
如何防止调用不存在的方法而出错,使用__call魔术重载方法. __call方法原型如下: mixed __call(string $name,array $arguments) 当调用一个不可访问的 ...
- atitit..国富论 在现代it企业项目管理中的作用attialx 总结---国富论读后感 attialx
atitit..国富论 在现代it企业项目管理中的作用attialx 总结---国富论读后感 attialx 1. 国民财富的性质和原因的研究(简称:<国富论>) 1 2. 蕴含的重要管理 ...
- source insight 支持verilog 及使用技巧
安装 1.首先到官网下载clf文件 http://www.sourceinsight.com/public/languages/ 2.配置source insight 以前有的先删除 Options ...
- ERROR C2676
直接上代码: nl.h #ifndef NL_H #define NL_H #include <iosfwd> namespace ZJ { /** Insert a newline ch ...
- java大数模板
java 大数计算 这几天做了几道用大数的题,发现java来做大数运算十分方便.对acmer来说是十分实用的 1.valueOf(parament); 将参数转换为制定的类型 比如 int a=3 ...
- mysql唯一主键生成代码
<insert id="insertPeople" parameterType="PeopleSchedule"> <selectKe ...
- ext,exrReturn新增,修改删除等用
package cn.edu.hbcf.common.vo; /** * Ext Ajax 返回对象 * * @author * @date 2012-02-21 19:30:00 * */ publ ...
- Unix系统编程(二)open的练习
在上一篇中学习了一下open函数的基本用法,这里就要练习一下.计算机就是要实践嘛. 用open创建一个文件 #include <sys/types.h> #include <sys/ ...
- 爬虫 (6)- Scrapy 实战案例 - 爬取不锈钢的相关钢卷信息
超详细创建流程及思路 一. 新建项目 1.创建文件夹,然后在对应文件夹创建一个新的python项目 2.点击Terminal命令行窗口,运行下面的命令创建scrapy项目 scrapy startpr ...
- zend studio 10.6.2 字体大小 设置
如果汉化的:窗体-->常规-->外观-->颜色和字体-->基本-->文字字体 点击编辑 如果未汉化:Window->Preferences->General ...