Required feature in Autowiring

@Autowired has an argument called required=boolean value
By default it is set to true . we can explicitely make it false if we want.
If it is true(default behavior) then dependent bean should be available otherwise it throws exception
Example

  1. package com.kb.autowiringAnnotation;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Qualifier;
  5.  
  6. public class Person {
  7.    
  8.     @Autowired
  9.     Car car;
  10.    
  11.     public Car getCar() {
  12.         return car;
  13.     }
  14.  
  15.  
  16. }
package com.kb.autowiringAnnotation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class Person {
	
	@Autowired
	Car car;
	
	public Car getCar() {
		return car;
	}


}

beans.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:context="http://www.springframework.org/schema/context"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7.    http://www.springframework.org/schema/context
  8.    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  9. <context:annotation-config/>
  10. <bean id = "person" class ="com.kb.autowiringAnnotation.Person"/>
  11. </beans>
<?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-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<bean id = "person" class ="com.kb.autowiringAnnotation.Person"/>
</beans>

Now Person has dependency on Car specified to autowire. But there is no Car bean inside beans.xml file
also @Autowired annotation has default required=true
hence it throws below exception
Reason -> dependency required is set to true which is not available in spring config file.

Run this client to see the exception

  1. package com.kb.autowiringAnnotation;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
  6. public class Client {
  7.  
  8.     public static void main(String[] args) {
  9.         Person per;
  10.         ApplicationContext  applicationContext = new ClassPathXmlApplicationContext("com/kb/autowiringAnnotation/beans.xml");
  11.          per = (Person)applicationContext.getBean("person");
  12.         System.out.println("person's car is "+per.getCar());
  13.        
  14.     }
  15.    
  16. }
package com.kb.autowiringAnnotation;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Client {

	public static void main(String[] args) {
		Person per;
		ApplicationContext  applicationContext = new ClassPathXmlApplicationContext("com/kb/autowiringAnnotation/beans.xml");
		 per = (Person)applicationContext.getBean("person");
		System.out.println("person's car is "+per.getCar());
		
	}
	
}

Exception in thread “main” org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘person’: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.kb.autowiringAnnotation.Car com.kb.autowiringAnnotation.Person.car; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.kb.autowiringAnnotation.Car] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Now if we want to avoid this type of exception and to make depenent object as optional we can go for
@Autowired(required=false)
Lets modify Person class as below

  1. package com.kb.autowiringAnnotation;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Qualifier;
  5.  
  6. public class Person {
  7.    
  8.     @Autowired(required=false)
  9.     Car car;
  10.    
  11.     public Car getCar() {
  12.         return car;
  13.     }
  14.  
  15.  
  16. }
package com.kb.autowiringAnnotation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class Person {
	
	@Autowired(required=false)
	Car car;
	
	public Car getCar() {
		return car;
	}


}

Now run the same above client and see the ouput
person’s car is null
no exception is thrown as its dependent object is optional and hence dependent object is null.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on