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
- 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;
- }
- }
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
- <?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>
<?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
- 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());
- }
- }
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
- 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;
- }
- }
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.

