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