Define|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.`
【Define|Define custom @Required-style annotation in Spring】
4. Done Done, you just created a new custom
@Required-style
annotation named @Mandatory
, which is equivalent to `@Required`` annotation.推荐阅读
- Realm
- NO.1|NO.1 Function
- 为什么要传入|为什么要传入 undefined
- typedef的用法总结以及与#define的区别
- define与typedef、const、inline的区别
- Android|解决AS中启动AVD报错Emulator: PANIC: Cannot find AVD system path. Please define ANDROID_SDK_ROOT
- geth控制台调用智能合约方法 出现unescape is not defined错误。
- AS4中启动虚拟机时提示(Cannot find AVD system path. Please define ANDROID_SDK_ROOT)
- 030Flutter|(九)Flutter CustomScrollView 之 SliverGrid与SliverList
- 解决(Unable to execute dex: Multiple dex files define Landroid/annotation/AnimRes)