spring 攻略

时间:2023-02-14 16:47:10

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&num;代码实战】群蚁算法理论与实践全攻略——旅行商等路径优化问题的新方法

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

  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&semi;Apache&plus;PHP&plus;MySQL配置攻略

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

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

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

随机推荐

  1. Exception Handling引入MVP

    异常处理(Exception Handling)是所有系统的最基本的基础操作之一,其它的比如日志(Logging).审核(Auditing).缓存(Caching).事务处理(Transaction) ...

  2. vert&period;x学习(八),用JDBCClient配合c3p0操作数据库

    今天学习了下vert.x的JDBCClient,我这里将今天的学习笔记记录下来.这次学习中使用了c3p0. 用使用JDBCClient和c3p0得现在pom.xml文件里面导入对应的依赖,下面贴出xm ...

  3. JavaEE 获取路径全攻略

    本篇博客是 JavaWeb 应用服务器端在不同环境下获取文件路径的全面总结. 获取文件路径后主要应用的场景,读取 JavaWeb 自定义配置文件.在特定路径下生成各种类型的文件提供下载...... 想 ...

  4. Java多线程编程——进阶篇二

    一.线程的交互 a.线程交互的基础知识 线程交互知识点需要从java.lang.Object的类的三个方法来学习:    void notify()           唤醒在此对象监视器上等待的单个 ...

  5. Windowsphone 之xml序列化和反序列化的应用&lpar;WebService解析返回的数据DataSet &rpar;

    关于Xml的序列化和反序列化: 可以看这篇文章,http://www.cnblogs.com/Windows-phone/p/3243575.html WebService解析返回的数据DataSet ...

  6. MyBatis&plus;PageHelper实现分页

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7256105.html 前面讲到Spring+SpringMVC+MyBatis深入学习及搭建(十七)--Sp ...

  7. mongoDB学习手记2--建库、删库、插入、更新

    上一篇  讲了在windows系统下的安装和启动,本文主要讲怎么建库.删库.插入.更新 在讲之前我们说一下mongoDB的一些基本概念,我们对比关系型数据库能更直观的理解 SQL术语/概念 Mongo ...

  8. &period;net core jwt

    https://www.cnblogs.com/JacZhu/p/6837676.html

  9. linux系统管理 设置别名

    查看和设定别名 'alias' 查看系统中所有的命令别名 [root@ssgao1987 ~]# alias alias cp='cp -i' alias l.='ls -d .* --color=a ...

  10. 【公众号转载】MyBatis拦截器原理探究

    MyBatis拦截器介绍 MyBatis提供了一种插件(plugin)的功能,虽然叫做插件,但其实这是拦截器功能.那么拦截器拦截MyBatis中的哪些内容呢? 我们进入官网看一看: MyBatis 允 ...