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:

Application Data Access Object(DAO)

We are creating simple HelloDAO class like below:

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.

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.

Run the main above, the console print the following exception at startup.

Exception in thread “main” org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘service2’ defined in class path resource [spring-config.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Property ‘helloDAO’ is required for bean ‘service2’

That’s all on the Spring Custom @Required Styled Annotation Example.

Spring @Required Annotation

Download complete source code, click link below

spring-custom-@required-styled-annotation-example-1.zip (269 downloads)

Leave a Comment


Please share it if you found this useful
Hide Buttons