Spring依赖检查 bean 配置文件用于确定的特定类型(基本,集合或对象)的所有属性被设置。在大多数情况下,你只需要确保特定属性已经设置但不是所有属性..
对于这种情况,你需要 @Required 注解,请参见下面的例子:

@Required示例

Customer对象,适用@Required在 setPerson()方法,以确保 person 属性已设置。
package com.yiibai.common;import org.springframework.beans.factory.annotation.Required;public class Customer
{private Person person;private int type;private String action;public Person getPerson() {return person;}@Requiredpublic void setPerson(Person person) {this.person = person;}
}
简单地套用@Required注解不会强制执行该属性的检查,还需要注册一个RequiredAnnotationBeanPostProcessor以了解在bean配置文件@Required注解。
RequiredAnnotationBeanPostProcessor可以用两种方式来启用。

1. 包函 <context:annotation-config />

添加 Spring 上下文和 <context:annotation-config />在bean配置文件。
<beans ...xmlns:context="http://www.springframework.org/schema/context"...http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd">...<context:annotation-config />...
</beans>

完整的实例,

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd"><context:annotation-config /><bean id="CustomerBean" class="com.yiibai.common.Customer"><property name="action" value="buy" /><property name="type" value="1" /></bean><bean id="PersonBean" class="com.yiibai.common.Person"><property name="name" value="yiibai" /><property name="address" value="address ABC" /><property name="age" value="29" /></bean></beans>

2. 包函 RequiredAnnotationBeanPostProcessor

直接在 bean 配置文件包函“RequiredAnnotationBeanPostProcessor”。
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean
class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/><bean id="CustomerBean" class="com.yiibai.common.Customer"><property name="action" value="buy" /><property name="type" value="1" /></bean><bean id="PersonBean" class="com.yiibai.common.Person"><property name="name" value="yiibai" /><property name="address" value="address ABC" /><property name="age" value="29" /></bean>
</beans>
如果你运行它,下面的错误信息会丢的,因为 person 的属性未设置。
org.springframework.beans.factory.BeanInitializationException: Property 'person' is required for bean 'CustomerBean'
结论
尝试@Required注解,它比依赖检查XML文件中更加灵活,因为它可以适用于只有一个特定属性。
定义@Required
请阅读本文有关如何创建新的自定义@Required-style 注解。

Spring使用@Required注解依赖检查相关推荐

  1. Spring依赖检查

    在Spring中,可以使用依赖检查功能,以确保所要求的属性可设置或者注入. 依赖检查模式 4个依赖检查支持的模式: none – 没有依赖检查,这是默认的模式. simple – 如果基本类型(int ...

  2. Spring的属性依赖检查

    Spring支持4种依赖检查:默认的是none none – No dependency checking. simple – If any properties of primitive type ...

  3. 使用@required注解完成依赖检查

    使用@required注解完成依赖检查: 即在对象属性的set方法上添加 @required后,若 <bean>配置中不为该属性赋值,运行会报错. 第一步: package com.xuz ...

  4. Spring 基于注解(annotation)的配置之@Required注解

    从 Spring 2.5 开始就可以使用注解来配置依赖注入.注解连线在默认情况下在 Spring 容器中不打开.因此,在可以使用基于注解的连线之前,我们将需要在我们的 Spring 配置文件中启用它: ...

  5. Spring注解依赖注入的三种方式的优缺点以及优先选择

    当我们在使用依赖注入的时候,通常有三种方式: 1.通过构造器来注入: 2.通过setter方法来注入: 3.通过filed变量来注入: 那么他们有什么区别吗?应该选择哪种方式更好? 代码示例: Con ...

  6. 分享Spring中Bean的4种依赖检查模式

    http://developer.51cto.com/art/201104/253227.htm 下面我们来看看Spring中的Bean的4中依赖检查模式:simple,object,all,none ...

  7. Spring5——(一)spingIOC(入门介绍,spring创建bean,依赖,注入,注解方式)

    为什么要有框架? (1)对于web层来说,一个大型的程序往往需要编写大量的servlet,并且取值封装会非常繁琐. (2)对于dao层,要编写大量的sql语句,对于结果的解析也很麻烦,并且sql的复用 ...

  8. Spring 3.0 注解注入详解

    Spring 3.0 注解注入详解 2011-04-15 09:44 17ZOUGUO ITEYE博客 我要评论(1) 字号:T | T AD: 一.各种注解方式 1.@Autowired注解(不推荐 ...

  9. spring dao层注解_Spring– DAO和服务层

    spring dao层注解 欢迎来到Spring教程的第三部分. 在这一部分中,我们将继续编写Timesheet应用程序,这次我们将实现DAO层,业务服务并编写一些测试. 在上一部分中,我们定义了Ge ...

最新文章

  1. 十九、约束作用及常见约束
  2. ajax从mysql提取数据在html中_Python骚操作,提取pdf文件中的表格数据!
  3. 大规模数据处理开源软件
  4. 定积分计算器_使用科学计算器计算概率分布
  5. HttpWatch的Result中出现Aborted的原因分析[配图]
  6. elasticsearch7使用指导
  7. 15岁中国学生斩获苹果WWDC奖学金:写代码只用了2个周末
  8. Flex App的Size和Link报告
  9. 获取位置_原神蜥蜴尾巴怎么获得 蜥蜴尾巴获取位置分享
  10. ASP.NET商贸进销存管理系统源码(带数据库文档)源码免费分享
  11. SpringCloud第八章:Gateway新一代网关
  12. 逻辑回归分类——信用卡诈骗检测!这才是干货!
  13. WindowsServer修改用户密码
  14. 护眼灯买什么样的好?这几款2022年最佳的护眼灯值得一看!
  15. 人工智能专业计算机毕业设计选题推荐
  16. 【Python】EasyGUI实例——实现NJUCM绩点计算器程序
  17. 视频压缩编码和音频压缩编码基本原理
  18. 系统可用性:SRE口中的3个9,4个9...到底是个什么东西?
  19. 全自动叠片式过滤器结构、原理说明
  20. 宽带隙器件增强电机控制设计

热门文章

  1. Qt Creator列表和其他数据模型
  2. OpenGL Blinn-Phong Shader实例
  3. C语言给定数字n阶乘的末尾计算零个数(附完整源码)
  4. QML基础类型之vector2d
  5. windows C++ Opengl基础框架源码
  6. 16_clickhouse,HDFS引擎,JDBC引擎
  7. Python安装(Windows下安装/Linux下安装)
  8. Mule web service调用中的复杂类型传递
  9. Oracle分组函数
  10. 处理字符串_4_计算某个字符出现的次数