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 |
package com.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 @interface Mandatory { } |
Application Data Access Object(DAO)
We are creating simple HelloDAO class like below:
1 2 3 4 5 6 7 8 |
package com.javabycode.spring.core.required; public class HelloDAO { public String getMsgHello() { // 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 |
package com.javabycode.spring.core.required; public class HelloService { private HelloDAO helloDAO; public HelloDAO getHelloDAO() { return helloDAO; } @Mandatory public void setHelloDAO(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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?xml version="1.0" encoding="UTF-8"?> <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.xsd"> <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"> <property name="requiredAnnotationType" value="com.javabycode.spring.core.required.Mandatory"/> </bean> <bean id="helloDAO" class="com.javabycode.spring.core.required.HelloDAO"/> <bean id="service1" class="com.javabycode.spring.core.required.HelloService"> <property name="helloDAO" ref="helloDAO"/> </bean> <bean id="service2" class="com.javabycode.spring.core.required.HelloService"/> </beans> |
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:
1 2 3 |
<bean id="service2" class="com.javabycode.spring.core.required.HelloService"> <meta key="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor.skipRequiredCheck" value="true"/> </bean> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.javabycode.spring.core.required; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MySpringApplication { public static void main(String... args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); HelloService service1 = (HelloService)context.getBean("service1"); System.out.println(service1); HelloService service2 = (HelloService)context.getBean("service2"); System.out.println(service2); } } |
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.
References
Spring @Required Annotation
RequiredAnnotationBeanPostProcessor.java
Download complete source code, click link below
spring-custom-@required-styled-annotation-example-1.zip (269 downloads)