Spring学习(九)-----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;
}
@Required
public 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/context
http://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/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://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/beans
http://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

最新文章

  1. Hadoop入门学习笔记---part4
  2. android自定义圆形图片和遇到的问题
  3. centos 重启php-fpm
  4. skip-name-resolv
  5. PHPCMS数据筛选功能实现
  6. [转]Erlang不能错过的盛宴
  7. (转)SQLSERVER表分区的介绍(一)
  8. iOS开发加快审核app
  9. 运营商级NAT(Carrier-grade NAT)
  10. linq 在查询表达式中处理 null 值
  11. Postman教程——发送第一个请求
  12. 关于LINUX各类系统资源整合
  13. java并发编程基础 --- 4.1线程简介
  14. Head First Java设计模式思维导图总结
  15. HDFS APPEND性能测试
  16. Python操作MongoDB和Redis
  17. 一年工作经验的大专生程序员(java后台)
  18. C# 引用的程序集没有强名称
  19. 升级Linux内核导致vmware无法使用(vmnet模块无法编译)解决方式
  20. hadoop mysql install (5)

热门文章

  1. C++ —— 小操作
  2. nginx下配置多个web服务
  3. sharepoint rest 脚本发送邮件
  4. Myeclipse2014无法启动,启动十分之一自动闪退,闪退
  5. 【MySQL】基本语句
  6. python爬虫练习 -- 签名器+GUI界面(Tkinter)
  7. Xquery的初步学习(一次Lab作业的总结)
  8. vim插件管理 - vim-plug
  9. 网页中的图像&lt;img&gt;
  10. 一条SQL语句的千回百转