The Spring @Required annotation example shows you how to use the @Required annotation. This annotation helps you check some dependency at startup rather than at runtime. So that this make it more clear and quicker to detech exceptions and errors. By default, this annotation is not configured, thus we must instruct the spring container explicitly.
Let’s see the detail example below.
Application Data Access Object(DAO)
We are creating simple HelloDAO class like below:
1 2 3 4 5 6 7 8 9 10 |
package com.javabycode.spring.core.required; public class HelloDAO { public String getMsgHello() { // do stuffs return "Spring multiple bean configuration files!"; } } |
Application Service and Using Spring @Required Annotation Example
We are creating simple service class. This class has a property called HelloDAO which we annotated the setter method with @Required, like below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.javabycode.spring.core.required; import org.springframework.beans.factory.annotation.Required; public class HelloService { private HelloDAO helloDAO; @Required public void setHelloDAO(HelloDAO helloDAO) { this.helloDAO = helloDAO; } public HelloDAO getHelloDAO() { return helloDAO; } } |
Configuring Spring annotation
By default, the @Required annotation will not found by Spring. We have to configure the RequiredAnnotationBeanPostProcessor that check if all the bean properties with the @Required annotation have been configured correctly. We have three options to enable this RequiredAnnotationBeanPostProcessor:
a. Configure the bean directly by including a bean with class org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor in bean configuration file.
b. By adding the
c. By adding the
The below configuration uses
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean id="helloDAO" class="com.javabycode.spring.core.required.HelloDAO"/> <bean id="helloService1" class="com.javabycode.spring.core.required.HelloService"> <property name="helloDAO" ref="helloDAO"/> </bean> <bean id="helloService2" class="com.javabycode.spring.core.required.HelloService"/> </beans> |
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="helloService2" 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 @Required 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 MyApplication { public static void main(String... args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); HelloService helloService1 = (HelloService)context.getBean("helloService1"); System.out.println(helloService1); HelloService helloService2 = (HelloService)context.getBean("helloService2"); System.out.println(helloService2); } } |
Run the main above, the console will print BeanInitializationException like this
1 |
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloService2' 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 'helloService2' |
That’s it on the Spring @Required annotation example.
References
@Required Annotation Spring Doc
RequiredAnnotationBeanPostProcessor.java
Download complete source code, click link below
spring-@required-annotation-example.zip (218 downloads)