Spring dependency check using @Required annotation example
If you want to make parameters mandatory for setter-based dependency injection, you must need to use @Required
annotation because dependency-check
attribute of <bean/>
tag is deprecated and no longer supported since Spring 3.x release.
Apply @Required
annotation to all those setter methods that you want to make parameters mandatory or user have to pass it. Before preceding next, you just need to activate the @Required
annotation. To activate it you need to create RequiredAnnotationBeanPostProcessor
class object.
<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor" />
Check the complete example.
Spring Beans
Create an Employee
bean class that has multiple parameters like firstName, lastName, address, mobile, etc. and you want to make firstName and mobile must be passed by the user. To do this, apply the @Required
annotation to the setter method of firstName and mobile.
package org.websparrow.beans;
import org.springframework.beans.factory.annotation.Required;
public class Employee {
// Generate setters method
private String firstName;
private String lastName;
private String address;
private int mobile;
@Required
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setAddress(String address) {
this.address = address;
}
@Required
public void setMobile(int mobile) {
this.mobile = mobile;
}
// business logic
public void displayData() {
System.out.println("Employee details...\n");
System.out.println("First Name: " + firstName);
System.out.println("Last Name: " + lastName);
System.out.println("Address: " + address);
System.out.println("Mobile: " + mobile);
}
}
Spring Beans Configuration
Now activate the @Required
annotation by creating an object of RequiredAnnotationBeanPostProcessor
class in your configuration file.
<?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-2.5.xsd">
<!-- activate the @Required annotation -->
<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor" />
<!-- you must need to inject all those parameters value where you have applied @Required annotation -->
<bean id="emp" class="org.websparrow.beans.Employee">
<property name="firstName" value="Atul" />
<property name="mobile" value="236181627" />
</bean>
</beans>
Run it
Load configuration file and run it.
package org.websparrow.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.websparrow.beans.Employee;
public class Client {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
Employee employee = (Employee) applicationContext.getBean("emp");
employee.displayData();
}
}
Here is the two possible output display on your console log.
1- It works fine and display all those parameters value that you have passed for mandatory parameters and rest will show null
.
Employee details...
First Name: Atul
Last Name: null
Address: null
Mobile: 236181627
2- You will get the following exception because you have made some parameters mandatory but didn’t pass the value and Spring forces you to pass all those mandatory parameters value.
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'emp' defined in class path resource [spring.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Property 'mobile' is required for bean 'emp'
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'emp' defined in class path resource [spring.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Property 'mobile' is required for bean 'emp'
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:591)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$6/75457651.getObject(Unknown Source)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:758)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
at org.websparrow.test.Client.main(Client.java:10)
Caused by: org.springframework.beans.factory.BeanInitializationException: Property 'mobile' is required for bean 'emp'
at org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor.postProcessPropertyValues(RequiredAnnotationBeanPostProcessor.java:156)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1344)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
... 12 more
Download Source Code: spring-dependency-check-using-required-annotation-example