Define custom @Required-style annotation in Spring
The @Required
annotation is used to make sure a particular property has been set. If you are migrate your existing project to Spring framework or have your own @Required-style
annotation for whatever reasons, Spring is allow you to define your custom @Required-style
annotation, which is equivalent to @Required
annotation.
In this example, you will create a custom @Required-style
annotation named @Mandatory
, which is equivalent to @Required
annotation.
1. Create the @Mandatory interface
package com.mkyong.common;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Mandatory {
}
2. Apply it to a property
package com.mkyong.common;
public class Customer
{
private Person person;
private int type;
private String action;
@Mandatory
public void setPerson(Person person) {
this.person = person;
}
//getter and setter methods
}
3. Register it
Include your new @Mandatory
annotation in ‘RequiredAnnotationBeanPostProcessor
’ class.
<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">
<property name="requiredAnnotationType" value="com.mkyong.common.Mandatory"/>
</bean>
<bean id="CustomerBean" class="com.mkyong.common.Customer">
<property name="action" value="buy" />
<property name="type" value="1" />
</bean>
</beans>
4. Done
Done, you just created a new custom @Required-style
annotation named @Mandatory
, which is equivalent to @Required
annotation.
Define custom @Required-style annotation in Spring的更多相关文章
- @Query Annotation in Spring Data JPA--转
原文地址:http://javabeat.net/spring-data-jpa-query/ In my previous post on Spring Data, I have explained ...
- Spring学习笔记之三----基于Annotation的Spring IOC配置
使用Annotation 来创建Bean有两种方式 在配置类中创建bean(配置类是指标注为@Configuration的类),在配置类中每一个创建bean的方法都应该标注为@Bean,可以在@Bea ...
- Spring学习笔记之四----基于Annotation的Spring AOP编程
你能使用@Aspect annotation将某个Java类标注为Aspect,这个Aspect类里的所有公有方法都可以成为一个Advice,Spring提供了5个Annotation去将某个方法标注 ...
- 基于 Annotation 的 Spring AOP 权限验证方法的实现
1. 配置 applicationContext 在 Spring 中支持 AOP 的配置非常的简单,只需要在 Spring 配置文件 applicationContext.xml 中添加: < ...
- How to create custom methods for use in spring security expression language annotations
From:http://stackoverflow.com/questions/6632982/how-to-create-custom-methods-for-use-in-spring-secur ...
- [TypeScript] Define Custom Type Guard Functions in TypeScript
One aspect of control flow based type analysis is that the TypeScript compiler narrows the type of a ...
- iOS - UITableViewCell Custom Selection Style Color
Customize UITextView selection color in UITableView Link : http://derekneely.com/2010/01/uitableview ...
- Define Custom Data Filter Using Pre-Query Trigger In Oracle Forms
Oracle Forms is having its default records filter, which we can use through Enter Query mode to spec ...
- 使用GridFsTemplate在mongodb中存取文件
spring-data-mongodb之gridfs mongodb除了能够存储大量的数据外,还内置了一个非常好用的文件系统.基于mongodb集群的优势,GridFS当然也是分布式的,而且备份也 ...
随机推荐
- UVa 11992 (线段树 区间修改) Fast Matrix Operations
比较综合的一道题目. 二维的线段树,支持区间的add和set操作,然后询问子矩阵的sum,min,max 写完这道题也是醉醉哒,代码仓库里还有一份代码就是在query的过程中也pushdown向下传递 ...
- LA 3510 (置换 循环分解) Pixel Shuffle
思路挺简单的,题目中的每个命令(包括命令的逆)相当于一个置换. 用O(n2k)的时间复杂度从右往左求出这些置换的乘积A,然后求m使Am = I(I为全等置换) 还是先把A分解循环,m则等于所有循环节长 ...
- Android、iPhone和Java三个平台一致的加密工具
先前一直在做安卓,最近要开发iPhone客户端,这其中遇到的最让人纠结的要属Java.Android和iPhone三个平台加解密不一致的问题. 因为手机端后台通常是用JAVA开发的Web Servic ...
- LeetCode Triangle 三角形(最短路)
题意:给一个用序列堆成的三角形,第n层的元素个数为n,从顶往下,每个元素可以选择与自己最近的两个下层元素往下走,类似一棵二叉树,求最短路. [], [,4], [6,,7], [4,,8,3] 注意: ...
- HDU 3749 Financial Crisis 经济危机(点双连通分量)
题意: 给一个图n个点m条边(不一定连通),接下来又q个询问,询问两个点是为“不相连”,“仅有一条路径可达”,“有两条及以上的不同路径可达”三种情况中的哪一种.注:两条以上的路径指的是路径上的点连1个 ...
- AngularJS promise()
实例说明一 <!DOCTYPE html> <html ng-app="my-app"> <head> <meta charset=&qu ...
- 如何借助于UML完成我们对系统的设计?谈谈我的理解
首先要说的是我对面向对象的理解,以及设计类的依据: http://www.cnblogs.com/xinchrome/p/4904931.html 理解了这篇文章,也就理解了现在要说的. 在面向对象编 ...
- 《C++ Primer 4th》读书笔记 第9章-顺序容器
原创文章,转载请注明出处:http://www.cnblogs.com/DayByDay/p/3936460.html
- Egret应用开发实践(01) Egret与WebPack
Egret Egret引擎是一款使用TypeScript语言构建的开源免费的移动游戏引擎.Egret仅是纯粹的使用TypeScript语言来开发游戏,开发后可以使用Egret来打包为HTML5网页游戏 ...
- 关于web中的自适应布局
一.”自适应网页设计”的概念 2010年,Ethan Marcotte提出了“自适应网页设计”(Responsive Web Design)--这个名词,指可以自动识别屏幕宽度.并做出相应调整的网页设 ...