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上面添加一些其他的内容,比如文字,图片.... 经历几次失败的尝试,终于获取到了正确的代码书写方式. 在此 ...
随机推荐
- unity调用系统剪切板功能
package com.game.utils; import android.app.Activity; import android.content.ClipData; import android ...
- Atitit. http 代理原理 atiHttpProxy 大木马
Atitit. http 代理原理 atiHttpProxy 大木马 1. 面这张图可以清晰地阐明HttpProxy的实现原理:1 2. 代理服务器用途1 3. 其中流程具体如下:2 4. 设计规 ...
- python学习之platform模块
该模块用来访问平台相关属性. 常见属性和方法 平台架构 platform.machine() 返回平台架构.若无法确定,则返回空字符串. >>> platform.machine() ...
- 2018.5.2(7:20到的办公室开始早课 阮一峰的JS) 所有的默默努力都是为了让自己看起来毫不费力
continue语句用于立即终止本轮循环,返回循环结构的头部,开始下一轮循环. break语句用于跳出代码块或循环. 标签(label) JavaScript 语言允许,语句的前面有标签(label) ...
- tomcat打印GC日志
在catinlin.sh的最上面加上 JAVA_OPTS=" -XX:+PrintGCTimeStamps -XX:+PrintGCDetails -Xloggc:/lnmp/tomcat8 ...
- 如何在uboot上实现从网络下载版本镜像并直接在内存中加载之?
这是作者近期项目上遇到的一个需求,描述如下: 一块MT7620N的路由器单板,Flash中已存放一个版本并可以通过uboot正常加载并启动.现在需要:在uboot上电启动过程中,通过外部按键触发干涉, ...
- springBoot文档地址
文档: https://www.gitbook.com/book/qbgbook/spring-boot-reference-guide-zh/details 配置: http://docs.spri ...
- HttpClient+jsoup登录+解析 163邮箱
找了几个,只有这个靠谱,用的是httpclient4,另外还需要commons-lang和jsoup包 http://jsoup.org/ http://www.oschina.net/code/sn ...
- sql把varchar转化为int型
select Max(convert(int,id))from member_Info;
- PAT003 List Leaves
题目: Given a tree, you are supposed to list all the leaves in the order of top down, and left to righ ...