博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Define custom @Required-style annotation in Spring
阅读量:6278 次
发布时间:2019-06-22

本文共 1408 字,大约阅读时间需要 4 分钟。

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.

4. Done

Done, you just created a new custom @Required-style annotation named @Mandatory, which is equivalent to @Required annotation.

转载于:https://www.cnblogs.com/ghgyj/p/4748334.html

你可能感兴趣的文章
js实现页面跳转的几种方式
查看>>
sbt笔记一 hello-sbt
查看>>
常用链接
查看>>
pitfall override private method
查看>>
!important 和 * ----hack
查看>>
聊天界面图文混排
查看>>
控件的拖动
查看>>
svn eclipse unable to load default svn client的解决办法
查看>>
Android.mk 文件语法详解
查看>>
QT liunx 工具下载
查看>>
内核源码树
查看>>
Java 5 特性 Instrumentation 实践
查看>>
AppScan使用
查看>>
Java NIO框架Netty教程(三) 字符串消息收发(转)
查看>>
Ucenter 会员同步登录通讯原理
查看>>
php--------获取当前时间、时间戳
查看>>
Spring MVC中文文档翻译发布
查看>>
docker centos环境部署tomcat
查看>>
JavaScript 基础(九): 条件 语句
查看>>
Linux系统固定IP配置
查看>>