@Required
Incase the bean value is not defined using required will display the spring error message instead of null pointer exception for the bean whose value is not defined.
spring.xml
<bean class="com.mugil.shapes.Circle" id="circleId"> </bean>
Note above the pointA is undefined for above circle bean
Spring Error without @Required annotation
Exception in thread "main" java.lang.NullPointerException at com.mugil.shapes.Circle.drawShape(Circle.java:11) at com.mugil.shapes.DrawingApp.main(DrawingApp.java:17)
circle.java
public class Circle implements Shape{ private Point center; @Override public void drawShape() { System.out.println("Shape of Circle "); System.out.println("Center of Cirlce "+ getCenter().getX() + " - " + getCenter().getY()); } public Point getCenter() { return center; } @Required public void setCenter(Point center) { this.center = center; } }
spring.xml
<bean class="com.mugil.shapes.Circle" id="circleId"> </bean> <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
Spring Error is Thrown
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'circleId' defined in class path resource [spring1.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Property 'center' is required for bean 'circleId'
Autowired Annotation
- @Autowired works by taking into consideration type,name,qualifier
- If type not found name will be considered
spring.xml(by Type)
<beans> <bean id="PointA" class="com.mugil.shapes.Point"> <property name="x" value="0"/> <property name="y" value="20"/> </bean> <bean class="com.mugil.shapes.Circle" id="circleId"></bean> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> </beans>
spring.xml(by Name)
<beans> <bean id="center" class="com.mugil.shapes.Point"> <property name="x" value="0"/> <property name="y" value="20"/> </bean> <bean class="com.mugil.shapes.Circle" id="circleId"></bean> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> </beans>
In above xml center is the name of instance variable in circle class
spring.xml(by Qualifier)
<?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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <bean id="pointA" class="com.mugil.shapes.Point"> <qualifier value="circle"/> <property name="x" value="20"/> <property name="y" value="0"/> </bean> <bean class="com.mugil.shapes.Circle" id="circleId"></bean> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> </beans>
circle.java
public class Circle implements Shape{ @Autowired @Qualifier("circle") private Point center; @Override public void drawShape() { System.out.println("Shape of Circle "); System.out.println("Center of Cirlce "+ getCenter().getX() + " - " + getCenter().getY()); } public Point getCenter() { return center; } public void setCenter(Point center) { this.center = center; } }