Program to demonstrate how to use required attribute in @Autowired annotation for Dependency Checking.
@Autowired annotation is used by Spring Framework to auto-wire beans together based on name, type etc. If we are using @Autowired annotation on a setter, constructor or property of a bean, Spring container performs dependency checking and make sure that property is been wired properly.
When Spring container finds that there is no bean to wire, it throws an exception see source code below :
1. Create a Address POJO class
Address POJO class -
2. Create a Employee POJO class having Address as property
3. Create a Spring Configuration XML file
Here we have commented out the bean with id as "address", if we run the test class below , Spring container runs the program and finds that Employee class has address property setter method having autowired by annotation as @Autowired.
It checks Spring Configuration file and finds no match for the address property so it throws an exception see output below. As @Autowired annotation forcefully checks whether dependency is being fulfilled or not.
Exception of the program :
In order to make Spring container dependency checking to optional (optional : means we want to auto-wire the bean only if it is been provided in the XML and if its not instead of big exception it should make that property to null ) , we make use of required attribute of @Autowired annotation. The default value of this attribute is true which means it forcefully checks for dependency but if we make it false Spring treats it as optional and doesn't throw any exception
We use below Employee.java for running this scenario :
Above Employee class has @Autowired(required=false) for the setter method of Address. If we run the program with above Employee.java and rest the same we get the output as :
Output of the program :
@Autowired annotation is used by Spring Framework to auto-wire beans together based on name, type etc. If we are using @Autowired annotation on a setter, constructor or property of a bean, Spring container performs dependency checking and make sure that property is been wired properly.
When Spring container finds that there is no bean to wire, it throws an exception see source code below :
1. Create a Address POJO class
Address POJO class -
package com.hubberspot.spring.autowire; public class Address { private String street; private String city; private String state; public Address() { } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getState() { return state; } public void setState(String state) { this.state = state; } }
2. Create a Employee POJO class having Address as property
package com.hubberspot.spring.autowire; import org.springframework.beans.factory.annotation.Autowired; // Create a POJO class Employee which has a // Address Object reference as instance variable public class Employee { private String name; private int age; private Address address; public Employee() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Address getAddress() { return address; } // Annotation based autowiring of bean // Using annotation as @Autowired from the package // org.springframework.beans.factory.annotation.Autowired // Here its Autowire by setter method as we are directly // applying @Autowired annotation over the setter method @Autowired public void setAddress(Address address) { this.address = address; } }
3. Create a Spring Configuration XML file
|
Here we have commented out the bean with id as "address", if we run the test class below , Spring container runs the program and finds that Employee class has address property setter method having autowired by annotation as @Autowired.
It checks Spring Configuration file and finds no match for the address property so it throws an exception see output below. As @Autowired annotation forcefully checks whether dependency is being fulfilled or not.
package com.hubberspot.spring.autowire; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { // ApplicationContext is a Spring interface which // provides with the configuration for an application. // It provides us with all the methods that BeanFactory // provides. It loads the file resources in a older // and generic manner. It helps us to publish events to the // listener registered to it. It also provides quick support // for internationalization. It provides us with the object // requested, it reads the configuration file and provides // us with the necessary object required. // We are using concrete implementation of ApplicationContext // here called as ClassPathXmlApplicationContext because this // bean factory reads the xml file placed in the classpath of // our application. We provide ClassPathXmlApplicationContext // with a configuration file called as spring.xml placed // at classpath of our application. ApplicationContext context = new ClassPathXmlApplicationContext(("spring.xml")); // In order to get a object instantiated for a particular bean // we call getBean() method of ClassPathXmlApplicationContext // passing it the id for which the object is to be needed. // Here getBean() returns an Object. We need to cast it back // to the Employee object. Without implementing new keyword we // have injected object of Employee just by reading an xml // configuration file. Employee employee = (Employee)context.getBean("employee"); System.out.println("The name of Employee is : " + employee.getName()); System.out.println("The age of Employee is : " + employee.getAge()); if(!(employee.getAddress() == null)){ System.out.println("The address of Employee is : " + employee.getAddress().getStreet() +" "+ employee.getAddress().getCity() +" "+ employee.getAddress().getState()); } else { System.out.println("Address is not wired"); } } }
Exception of the program :
In order to make Spring container dependency checking to optional (optional : means we want to auto-wire the bean only if it is been provided in the XML and if its not instead of big exception it should make that property to null ) , we make use of required attribute of @Autowired annotation. The default value of this attribute is true which means it forcefully checks for dependency but if we make it false Spring treats it as optional and doesn't throw any exception
We use below Employee.java for running this scenario :
package com.hubberspot.spring.autowire; import org.springframework.beans.factory.annotation.Autowired; // Create a POJO class Employee which has a // Address Object reference as instance variable public class Employee { private String name; private int age; private Address address; public Employee() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Address getAddress() { return address; } // Annotation based autowiring of bean // Using annotation as @Autowired from the package // org.springframework.beans.factory.annotation.Autowired // Here its Autowire by setter method as we are directly // applying @Autowired annotation over the setter method @Autowired(required=false) public void setAddress(Address address) { this.address = address; } }
Above Employee class has @Autowired(required=false) for the setter method of Address. If we run the program with above Employee.java and rest the same we get the output as :
Output of the program :