@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

  1. @Autowired works by taking into consideration type,name,qualifier
  2. 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;
	}		
}

Reading constant from properties file
constant.properties

PointA.PointX=-20
PointA.PointY=0

spring.xml

<beans>
	<bean id="pointA" class="com.mugil.shapes.Point">
		<property name="x" value="${PointA.PointX}"/>
		<property name="y" value="${PointA.PointY}"/>
	</bean>
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="constant.properties"></property>
	</bean>
</beans>

Using Interface
spring.xml

<beans>
	<bean id="pointA" class="com.mugil.shapes.Point">
		<property name="x" value="${PointA.PointX}"/>
		<property name="y" value="${PointA.PointY}"/>
	</bean>
	<bean class="com.mugil.shapes.Circle" id="circleId">
		<property name="center" ref="pointA"/>
	</bean>
</beans>

shape.java

public interface Shape { 
	public void drawShape();
}

triangle.java

public class Triangle implements Shape
{
 public void drawShape()
 {
   System.out.println("Shape of Triangle");
 }
}

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;
	}

	public void setCenter(Point center) {
		this.center = center;
	}		
}

The objShape will call the draw method based on the instance referenced at runtime
shape.java

public class DrawingApp {
	public static void main(String[] args)  {
		 ApplicationContext objContext = new ClassPathXmlApplicationContext("spring1.xml");
		Shape objShape =  (Shape)objContext.getBean("circleId");
		objShape.drawShape();
	}
}

ApplicationContextAware and BeanNameAware

  1. The Aware interface has the feel of the listener, callback, or observer design patterns.
  2. Aware interface, which is a super interface to the two.The xxxAware interface is a common pattern used within the Spring framework.
  3. They are typically used to allow a Spring managed bean to be given an object (via the interfaces setXxx method) at Spring bootstrap time.During bootstrapping, Spring will examine each bean to determine if it implements any of the xxxAware interfaces. When one is found, it invokes the interface method, providing the piece of information that is being asked for.

In Spring Bean Lifecycle from creation to Destruction is managed by Spring Container. There may be scenarios where we would want to access bean created by spring container from non spring managed class. The beans created by spring container is available in ApplicationContext. Whenever there are any changes to the bean it would be updated in applicationcontext.
By implementing ApplicationContextAware in the bean which should be accessed outside and calling the setApplicationContext when new ClassPathXmlApplicationContext is called from outside class we can have access to bean from context.

SpringBeans.xml

<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-3.0.xsd">
	<bean id="helloBean" class="com.mkyong.core.HelloWorld">
		<property name="name" value="Mugil" />
	</bean>
</beans>

HelloWorld.java – Spring Bean

public class HelloWorld implements ApplicationContextAware
{
 private String name;
 private ApplicationContext objContext = null;


 public void setName(String name) {
  this.name = name;
 }

 public void printHello() {
  System.out.println("Spring 3 : Hello ! " + name);
 }

 public void setApplicationContext(ApplicationContext arg0) throws BeansException {
  this.objContext = arg0;
  System.out.println("Called when Object to new ClassPathXmlApplicationContext('springbeans.xml') is created");
 }
}

App.java

public class App 
{
 public static void main(String[] args) 
 {
  ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml");

  HelloWorld obj = (HelloWorld) context.getBean("helloBean");
  obj.printHello();
 }
}

Output

Called when Object to new ClassPathXmlApplicationContext('springbeans.xml') is created
Spring 3 : Hello ! Mugil

When spring instantiates beans, it looks for a couple of interfaces like ApplicationContextAware and InitializingBean. If they are found, the methods are invoked.

Class<?> beanClass = beanDefinition.getClass();
Object bean = beanClass.newInstance();
if (bean instanceof ApplicationContextAware) 
{
    ((ApplicationContextAware) bean).setApplicationContext(ctx);
}

In newer version it may be better to use annotations, rather than implementing spring-specific interfaces

@Inject // or @Autowired
private ApplicationContext ctx

When BeanPostProcessor implement Object to execute the postProcessBeforeInitialization method,for example ApplicationContextAwareProcessor that added before.

private void invokeAwareInterfaces(Object bean) {
        if (bean instanceof Aware) {
            if (bean instanceof EnvironmentAware) {
                ((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment());
            }
            if (bean instanceof EmbeddedValueResolverAware) {
                ((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(
                        new EmbeddedValueResolver(this.applicationContext.getBeanFactory()));
            }
            if (bean instanceof ResourceLoaderAware) {
                ((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext);
            }
            if (bean instanceof ApplicationEventPublisherAware) {
                ((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext);
            }
            if (bean instanceof MessageSourceAware) {
                ((MessageSourceAware) bean).setMessageSource(this.applicationContext);
            }
            if (bean instanceof ApplicationContextAware) {
                ((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
            }
        }
    }

When it is Invoked
Shape.java

//Application System out would be Printed
ApplicationContext objContext = new ClassPathXmlApplicationContext("spring1.xml");

//this.strTriangle would be printed
BeanFactory  objBeanFactory = new XmlBeanFactory(new FileSystemResource("spring.xml"));
Triangle objTriangle2 =  (Triangle)objBeanFactory.getBean("triangleName");

BeanFactoryAware
BeanFactory is used for BeanFactoryAware whereas ApplicationContextAware is used for ApplicationContext.Note that the ApplicationContext interface is a subclass of BeanFactory, and provides additional methods on top of the basic BeanFactory interface.

BeanNameAware Interface
Bean implementing this interface can get its name defined in the Spring container
One possible area of use could be if your building on/ extending the spring framework and would like to acquire the bean name for logging purposes/wiring them etc.

MyBeanName.java

public class MyBeanName implements BeanNameAware 
{
 @Override
 public void setBeanName(String beanName) 
 {
  System.out.println(beanName);
 }
}

Config.java

@Configuration
public class Config 
{ 
    @Bean(name = "myCustomBeanName")
    public MyBeanName getMyBeanName() {
        return new MyBeanName();
    }
}
myCustomBeanName
  1. beanName property represents the bean id registered in the Spring container.When a new bean is given a name in the spring container and if you want to access the name then BeanNameAware should be used
  2. In the above example when the code is run it outputs myCustomBeanName which is the name offered to bean by container at runtime.
  3. If no name is given its going to print name of the method – getMyBeanName as bean name

If you require access to the additional features available on an ApplicationContext? If so, then you should of course use ApplicationContextAware. If not, BeanFactoryAware will be sufficient.Amongst many other things, an ApplicationContext has additional methods for inspecting the beans e.g. containsBeanDefinition, getBeanDefinitionCount, getBeanDefinitionNames, getBeanNamesForType, getBeansOfType that may be useful to you and which are not available on BeanFactory

we should avoid using any of the Aware interfaces, unless we need them. Implementing these interfaces will couple the code to the Spring framework.

————————————————————————————————————————————————————

Inheriting Bean Definition
spring.xml

<beans>
	<bean id="parentTriangle" class="com.mugil.shapes.Triangle">
		<property name="pointA">
			<ref bean="pointA"/>
		</property>
	</bean>
	<bean id="triangleId" class="com.mugil.shapes.Triangle" parent="parentTriangle">
		<property name="pointB" ref="pointB"/>
		<property name="pointC" ref="pointC"/>
	</bean>
<beans>

Above the bean parentTriangle is inherited by the child bean triangleId

spring.xml
Bean definition can also be made as abstract by using abstract=”true” like one below

<beans>
	<bean id="parentTriangle" class="com.mugil.shapes.Triangle" abstract="true">
		<property name="pointA">
			<ref bean="pointA"/>
		</property>
	</bean>
</beans>

Managing Lifecycle of Bean

  1. Note the object for Context objContext is referred using AbstractApplicationContext not ApplicationContext
  2. registerShutdownHook() registers a hook which gets called at the end of application for cleanup

Shape.java

 AbstractApplicationContext objContext = new ClassPathXmlApplicationContext("spring1.xml");
 objContext.registerShutdownHook();

Triangle.java

public class Triangle implements InitializingBean, DisposableBean 
{
 @Override
 public void destroy() throws Exception {
   System.out.println("DisposableBean Called");
 }

 @Override
 public void afterPropertiesSet() throws Exception {
   System.out.println("InitializingBean Called");
 }
}

We can all initialize the methods which should be called for initialization in spring.xml as below
spring.xml

<bean id="triangleId" class="com.mugil.shapes.Triangle" init-method="myInit" destroy-method="myDestroy">
                <property name="pointA" ref="pointA"/>
		<property name="pointB" ref="pointB"/>
		<property name="pointC" ref="pointC"/>
	</bean>

Triangle.java

public class Triangle implements InitializingBean, DisposableBean 
{
  public void myInit()
  {
    System.out.println("Custom Init Method");
  }
	
  public void myDestroy()
  {
    System.out.println("Custom Destroy Method");
  }
}

Priority of method call when it is defined by using XML and Interface implementation

  1. Init method of XML will be called
  2. Custom Init method of Interface will be called
  3. Destroy method of XML will be called
  4. Custom Destroy method of Interface will be called

Bean Post Processor

  1. Will work before and after bean initialization
  2. Works only when called using application initialization of bean. Does not work with setter initialization
  3. called for every initialization of parent and child bean in class

Bean Post Processor

<beans>
	<bean id="triangleId" class="com.mugil.shapes.Triangle">
	    <property name="pointA" ref="pointA"/>
		<property name="pointB" ref="pointB"/>
		<property name="pointC" ref="pointC"/>
	</bean>
	
	<bean id="pointA" class="com.mugil.shapes.Point">
		<property name="x" value="-20"/>
		<property name="y" value="0"/>
	</bean>
	<bean id="pointB" class="com.mugil.shapes.Point">
		<property name="x" value="0"/>
		<property name="y" value="0"/>
	</bean>
	<bean id="pointC" class="com.mugil.shapes.Point">
		<property name="x" value="20"/>
		<property name="y" value="0"/>
	</bean>
	
	<bean class="com.mugil.shapes.BeanInitialization"/>
	
</beans>

BeanInitialization.java

public class BeanInitialization implements BeanPostProcessor
{
	@Override
	public Object postProcessAfterInitialization(Object obj, String objName) throws BeansException 
       {
	  System.out.println("After Initialization of " + objName);
	  return obj;
	}

	@Override
	public Object postProcessBeforeInitialization(Object obj, String objName) throws BeansException 
       {
	  System.out.println("Before Initialization of " + objName);
	  return obj;
	}

}

So the above code runs four times for bean initialization of pointA,pointB,pointC and Triangle

shape.java

public class Shape 
{ 
  public static void main(String[] args)  
  {
    ApplicationContext objContext = new ClassPathXmlApplicationContext("spring1.xml");
    Triangle objTriangle2 =  (Triangle)objContext.getBean("triangleId");
    . 
    .
    .
}

BeanFactoryPostProcessor initialization happens before the beans gets initialized in the bean factory.

BeanInitialization2.java

public class BeanInitialization2 implements BeanFactoryPostProcessor
{
  @Override
  public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0) throws BeansException 
  {
     System.out.println("This is Bean factory Post Processor");
  }
}

spring.xml

<beans>
  <bean class="com.mugil.shapes.BeanInitialization2"/>	
</beans>