Spring dependency checking with @Required Annotation
Spring’s dependency checking in bean configuration file is used to make sure all properties of a certain types (primitive, collection or object) have been set. In most scenarios, you just need to make sure a particular property has been set, but not all properties..
For this case, you need @Required
annotation, see following example :
@Required example
A Customer
object, apply @Required
in setPerson()
method to make sure the person
property has been set.
package com.mkyong.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;
}
}
Simply apply the @Required
annotation will not enforce the property checking, you also need to register an RequiredAnnotationBeanPostProcessor
to aware of the @Required
annotation in bean configuration file.
The RequiredAnnotationBeanPostProcessor
can be enabled in two ways.
1. Include <context:annotation-config />
Add Spring context and <context:annotation-config />
in bean configuration file.
<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>
Full example,
<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.mkyong.common.Customer">
<property name="action" value="buy" />
<property name="type" value="1" />
</bean>
<bean id="PersonBean" class="com.mkyong.common.Person">
<property name="name" value="mkyong" />
<property name="address" value="address ABC" />
<property name="age" value="29" />
</bean>
</beans>
2. Include RequiredAnnotationBeanPostProcessor
Include ‘RequiredAnnotationBeanPostProcessor
’ directly in bean configuration file.
<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.mkyong.common.Customer">
<property name="action" value="buy" />
<property name="type" value="1" />
</bean>
<bean id="PersonBean" class="com.mkyong.common.Person">
<property name="name" value="mkyong" />
<property name="address" value="address ABC" />
<property name="age" value="29" />
</bean>
</beans>
If you run it , the following error message will be throw, because person
property is unset.
org.springframework.beans.factory.BeanInitializationException:
Property 'person' is required for bean 'CustomerBean'
Conclusion
Try @Required
annotation, it is more flexible than dependency checking in XML file, because it can apply to a particular property only.
Spring dependency checking with @Required Annotation的更多相关文章
- Spring properties dependency checking
In Spring,you can use dependency checking feature to make sure the required properties have been set ...
- Spring Auto-Wiring Beans with @Autowired annotation
In last Spring auto-wiring in XML example, it will autowired the matched property of any bean in cur ...
- Spring学习笔记之四----基于Annotation的Spring AOP编程
你能使用@Aspect annotation将某个Java类标注为Aspect,这个Aspect类里的所有公有方法都可以成为一个Advice,Spring提供了5个Annotation去将某个方法标注 ...
- Spring学习笔记之三----基于Annotation的Spring IOC配置
使用Annotation 来创建Bean有两种方式 在配置类中创建bean(配置类是指标注为@Configuration的类),在配置类中每一个创建bean的方法都应该标注为@Bean,可以在@Bea ...
- Spring的声明式事务----Annotation注解方式(2)
使用步骤: 步骤一.在spring配置文件中引入<tx:>命名空间<beans xmlns="http://www.springframework.org/schema/b ...
- Spring的声明式事务----Annotation注解方式(1)
这里列一个小的demo工程,直接利用Spring的jdbcTemplate访问Mysql数据库. 工程结构: 数据库中的tbl_student表结构如下: 数据实体类Student.java代码如下: ...
- Spring 常用注入注解(annotation)和其对应xml标签
使用注解需要修改bean.xml: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=& ...
- Spring+springmvc+Mybatis整合案例 annotation版(myeclipse)详细版
Spring+springmvc+Mybatis整合案例 Version:annotation版 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version=&qu ...
- Spring配置机制的优缺点 - Annotation vs XML
转自 http://tianzongqi.iteye.com/blog/1458002 XML配置的优缺点: 优点: XML配置方式进一步降低了耦合,使得应用更加容易扩展,即使对配置文件进一步修改也不 ...
随机推荐
- NFC(4)响应NFC设备时启动activity的四重过滤机制
响应NFC设备时启动activity的四重过滤机制 在一个NFC设备读取NFC标签或另一个NFC设备中的数据之前会在0.1秒之内建立NFC连接,然后数据会自动从被读取一端流向读取数据的一端(NFC设备 ...
- Java连接oracle数据库的OCI和THIN
使用jdbc连接上oracle有两种方法: 1. 使用thin连接 由于thin驱动都是纯Java代码,并且使用TCP/IP技术通过java的Socket连接上Oracle数据库,所以thin驱动是与 ...
- Android Studio设备背景色
从eclipse转到Android Studio. Android Studio的一些设置 1.设置字体大小:Settings->Editor->Colors&Fonts-> ...
- git push
使用git push直接推送未关联分支的时候,出现如下提示: $ git push Counting objects: 46, done. Delta compression using up to ...
- iOS开发:UINavigationController常用操作
NavigationController常用操作: 更改bar的背景颜色:self.navigationController?.navigationBar.barTintColor =UIColor. ...
- Qt之QTableView添加复选框(QAbstractTableModel)
简述 使用QTableView,经常会遇到复选框,要实现一个好的复选框,除了常规的功能外,还应注意以下几点: 三态:不选/半选/全选 自定义风格(样式) 下面我们介绍一下常见的实现方式: 编辑委托. ...
- PhoneGap 安装体验
npm -v #显示版本,检查npm 是否正确安装. npm install express #安装express模块 npm install -g express #加上 -g 启用global安装 ...
- [反汇编练习] 160个CrackMe之004
[反汇编练习] 160个CrackMe之004. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...
- HDU 1269 迷宫城堡 (强连通分量,常规)
题意: 判断所给的有向图是否是一个强连通图. 思路: 如果连通分量大于1则必定No,如果强连通分量大于1也是No.tarjan算法求强连通分量. #include <cstdio> #in ...
- 持有对象:总结JAVA中的常用容器和迭代器,随机数 速查
JAVA使用术语“Collection”来指代那些表示集合的对象,JAVA提供的接口很多,首先我们先来记住他们的层次结构: java集合框架的基本接口/类层次结构 java.util.Collecti ...