Spring Aware Interfaces for beans

Sometimes it is required that our beans needs to get some information about Spring container and its resources.

For example, sometime bean need to know the current Application Context using which it can perform some operations like loading specific bean from the container in a programmatic way.

So to make the beans aware about this, spring provides lot of Aware interfaces.

All we have to do is, make our bean to implement the Aware interface and implement the setter method of it.

org.springframework.beans.factory.Aware is the root marker interface.

All the Aware interfaces which we use are the sub interfaces of the Aware interface.

Some of the commonly used Aware interfaces are

1) ApplicationContextAware
Bean implementing this interface can get the current application context and this can be used to call any service from the application context

2) BeanFactoryAware
Bean implementing this interface can get the current bean factory and this can be used to call any service from the bean factory

3) BeanNameAware
Bean implementing this interface can get its name defined in the Spring container.

4) MessageSourceAware
Bean implementing this interface can get the access to message source object which is used to achieve internationalization

5) ServletContextAware
Bean implementing this interface can get the access to ServeltContext which is used to access servlet context parameters and attributes

6) ServletConfigAware
Bean implementing this interface can get the access to ServletConfig object which is used to get the servlet config parameters

7) ApplicationEventPublisherAware
Bean implementing this interface can publish the application events and we need to create listener which listen this event.

8) ResourceLoaderAware
Bean implementing this interface can load the resources from the classpath or any external file.

Let us see ApplicationContextAware and BeanFactoryAware with example

Create a class which implements ApplicationContextAware as below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.kb.beans;
 
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
 
public class ApplicationContextAwareImpl implements ApplicationContextAware{
    
    private ApplicationContext applicationContext;
 
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        
        System.out.println("setApplicationContext method called");
        this.applicationContext = applicationContext;
        
    }
    
    public void displayPersonDetails(){
        Person person = (Person)applicationContext.getBean("person");
        System.out.println(person.getName());
        
        System.out.println(applicationContext.isSingleton("person"));
    }
 
}
package com.kb.beans;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class ApplicationContextAwareImpl implements ApplicationContextAware{
	
	private ApplicationContext applicationContext;

	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		
		System.out.println("setApplicationContext method called");
		this.applicationContext = applicationContext;
		
	}
	
	public void displayPersonDetails(){
		Person person = (Person)applicationContext.getBean("person");
		System.out.println(person.getName());
		
		System.out.println(applicationContext.isSingleton("person"));
	}

}

In this class, we are trying to access ApplicationContext by implementing ApplicationContextAware.

So using ApplicationContext, we have accessed the Person bean from the context
And also we have checked whether Person bean is singleton or not using ApplicationContext.

Create class which implements BeanFactoryAware as below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.kb.beans;
 
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
 
public class BeanFactoryAwareImpl implements BeanFactoryAware{
 
    private BeanFactory beanFactory;
    
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("setBeanFactory method called");
        this.beanFactory = beanFactory;
        
    }
    
    public void displayPersonDetails(){
        Person person = (Person)beanFactory.getBean("person");
        System.out.println(person.getName());
        
        System.out.println(beanFactory.isSingleton("person"));
    }
}
package com.kb.beans;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;

public class BeanFactoryAwareImpl implements BeanFactoryAware{

	private BeanFactory beanFactory;
	
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		System.out.println("setBeanFactory method called");
		this.beanFactory = beanFactory;
		
	}
	
	public void displayPersonDetails(){
		Person person = (Person)beanFactory.getBean("person");
		System.out.println(person.getName());
		
		System.out.println(beanFactory.isSingleton("person"));
	}
}

Create a Person class as below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.kb.beans;
 
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
 
public class Person {
    
    private String name;
    
    public void setName(String name) {
        this.name = name;
    }
 
    public String getName() {
        return name;
    }
}
package com.kb.beans;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

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

	public String getName() {
		return name;
	}
}

Now create the beans.xml file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">
 
    <bean id="applicationContextAware" class="com.kb.beans.ApplicationContextAwareImpl"/>
 
    <bean id="beanFactoryAware" class="com.kb.beans.BeanFactoryAwareImpl"/>
 
    <bean id="person" class="com.kb.beans.Person">
        <property name="name" value="Raj"/>
    </bean>
</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"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context
  		http://www.springframework.org/schema/context/spring-context-3.2.xsd">

	<bean id="applicationContextAware" class="com.kb.beans.ApplicationContextAwareImpl"/>

	<bean id="beanFactoryAware" class="com.kb.beans.BeanFactoryAwareImpl"/>

	<bean id="person" class="com.kb.beans.Person">
		<property name="name" value="Raj"/>
	</bean>
</beans>

Now write a SpringAwareTest class to test the output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.kb.beans;
 
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class SpringAwareTest {
 
    public static void main(String[] args) {
        
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
 
        ApplicationContextAwareImpl applicationContextAwareImpl = (ApplicationContextAwareImpl) applicationContext.getBean("applicationContextAware");
        
        System.out.println("Application context aware output");
        applicationContextAwareImpl.displayPersonDetails();
        
        BeanFactoryAwareImpl beanFactoryAwareImpl = (BeanFactoryAwareImpl) applicationContext.getBean("beanFactoryAware");
 
        System.out.println("Bean factory aware output");
        beanFactoryAwareImpl.displayPersonDetails();
        applicationContext.registerShutdownHook();
    }
}
package com.kb.beans;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringAwareTest {

	public static void main(String[] args) {
		
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");

		ApplicationContextAwareImpl applicationContextAwareImpl = (ApplicationContextAwareImpl) applicationContext.getBean("applicationContextAware");
		
		System.out.println("Application context aware output");
		applicationContextAwareImpl.displayPersonDetails();
		
		BeanFactoryAwareImpl beanFactoryAwareImpl = (BeanFactoryAwareImpl) applicationContext.getBean("beanFactoryAware");

		System.out.println("Bean factory aware output");
		beanFactoryAwareImpl.displayPersonDetails();
		applicationContext.registerShutdownHook();
	}
}

Run the above class and see the output as below

So if we observe the output, we can see that ApplicationContext and BeanFactory are set first by the container as we have implemented ApplicationContextAware and BeanFactoryAware interfaces

Then we are accessing the bean value from the application context and Bean factory

Then we are checking the scope of the bean using application context and Bean factory

Download this project SpringAwareInterfaces.zip

About the Author

Karibasappa G C (KB)
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