Spring Custom @Required Styled Annotation Example

The Spring Custom @Required Styled Annotation Example helps you create and configure your own custom spring @Required annotation type. As you know, the @Required annotation is used to make sure a particular property has been set. Spring is allow you to define your custom @Required-style annotation, which is equivalent to @Required annotation.

In this example, we will create a custom @Required-style annotation named @Mandatory, which is equivalent to @Required annotation.

Spring Custom @Required styled Annotation

First, create a custom @Mandatory annotation. We are using this annotation intead of using the @Required annotation. We need the same annotation signature as the original annotation. See our custom @Required annotation below:

1

2

3

4

5

6

7

8

9

10

11

packagecom.javabycode.spring.core.required;

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@interfaceMandatory{

}

Application Data Access Object(DAO)

We are creating simple HelloDAO class like below:

1

2

3

4

5

6

7

8

packagecom.javabycode.spring.core.required;

publicclassHelloDAO{

publicStringgetMsgHello(){

// do stuffs

return"Spring multiple bean configuration files!";

}

}

Using the custom @Required annotation We annotate the setter method with the @Mandatory annotation. Then we need to register this annotation as a @Required styled annotation with the Spring container.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

packagecom.javabycode.spring.core.required;

publicclassHelloService{

privateHelloDAO helloDAO;

publicHelloDAO getHelloDAO(){

returnhelloDAO;

}

@Mandatory

publicvoidsetHelloDAO(HelloDAO helloDAO){

this.helloDAO=helloDAO;

}

}

Registering the custom @Required annotation To make the custom @Required annotation work we must register with Spring container by using the RequiredAnnotationBeanPostProcessor bean and setting the requiredAnnotationType with the fully qualified class name of our custom annotation.

Dig deeper: This RequiredAnnotationBeanPostProcessor is responsible for checking if all the bean properties with the @Required annotations have been configured correctly.

Notice: if you want to disable or skip @Required Dependency Checking for class we can configure the bean definition with the org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor.skipRequiredCheck and assign a value of true. This will skip the required annotation completely for that class, such as below:

Demo Spring application In this Spring Custom @Required Styled Annotation Example, we create main class and load all the bean definitions using the constructor of the ApplicationContext. The Spring container will search required annoation and check its required dependencies at starting the application. If there are bean definitions that are missing required properties, the application will throw BeanInitializationException at startup.