@Required annotation

は、特定のプロパティが設定されていることを確認するために使用されます。何らかの理由で既存のプロジェクトをSpringフレームワークに移行したり、@ Requiredスタイルのアノテーションを持っている場合、@Requiredアノテーションと同等の@カスタムスタイルのアノテーションを定義することができます。

この例では、

@ Required`アノテーションと同等の

@ Mandatory

というカスタム

@ Required-style`アノテーションを作成します。

1. @Mandatoryインターフェースを作成する

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.それをプロパティに適用する

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.登録する

あなたの新しい

@ Mandatory

アノテーションを ‘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">
    <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.完了

完了しました。@

必須

という名前の新しい@カスタムスタイルの注釈を作成しました。これは@Required注釈と同じです。