Spring 专题
专题目录
您的位置:java > Spring专题 > Spring使用@Required注解依赖检查
Spring使用@Required注解依赖检查
作者:--    发布时间:2019-11-20
spring依赖检查 bean 配置文件用于确定的特定类型(基本,集合或对象)的所有属性被设置。在大多数情况下,你只需要确保特定属性已经设置但不是所有属性..
对于这种情况,你需要 @required 注解,请参见下面的例子:

@required示例

customer对象,适用@required在 setperson()方法,以确保 person 属性已设置。
package com.h3.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.h3.common.customer">
		<property name="action" value="buy" />
		<property name="type" value="1" />
	</bean>

	<bean id="personbean" class="com.h3.common.person">
		<property name="name" value="h3" />
		<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.h3.common.customer">
		<property name="action" value="buy" />
		<property name="type" value="1" />
	</bean>

	<bean id="personbean" class="com.h3.common.person">
		<property name="name" value="h3" />
		<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 注解。


网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册