Blog Image

What is Aware Interface with example.  

Here we are going to deal with spring provided Aware Interface to get the current bean factory for using Container services.

In some cases, in our class we may need to call the method of another class to use its functionality. So, to call the method our class need to talk to the other class, either we can use composition or inheritance. As it is recommended to use composition we declare it as attribute. But we should not create the object of that class so, as we are using spring we can inject it.
But we don't want to create and don't want to inject, so we need to pull the object from IOC container. But to get the object first of all we need BeanFactory, whic is an implicit object of IOC container, so to get the BeanFactory from Ioc container, we need to implement our class from BeanFactoryAware and should override the setBeanFactory method, so IOC container will call that method and inject the BeanFactory as shown below.

There are some list of common Aware Interface in spring

 1) ApplicationContextAware.
 2) BeanFactoryAware.
 3) BeanNameAware.
 4) MessageSourceAware.
 5) ServletContextAware.
 6) ServletConfigAware.
 7) ApplicationEventPublisherAware.
 8) ResourceLoaderAware.

Let us See the example on BeanFactoryAware and how we can use in our projects lets explore.

Technologies used :-
 1) java (1.8).
 2) maven (3.5.2).
 3) Spring (3.0.1).

Project Structure

Execute below command for creating maven project.
mvn archetype:generate -DgroupId=com.sjg

Below is the maven project structure with packages and java src files.

package com.sjg.beans;

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

public class Car implements BeanFactoryAware {
    private String beanId;
    private BeanFactory factory;

    public void run() {
        IEngine engine = null;

        engine = factory.getBean(beanId, IEngine.class);

    public void setBeanId(String beanId) {
        this.beanId = beanId;

    public void setBeanFactory(BeanFactory factory) throws BeansException {
        this.factory = factory;


package com.sjg.beans;

public interface IEngine {
    void start();

package com.sjg.beans;

public class YamahaEngine implements IEngine {

    public void start() {
        System.out.println("Yamaha Engine started..");


package com.sjg.beans;

public class BajajEngine implements IEngine {

    public void start() {
        System.out.println("Bajaj Engine Started...");


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="";

    <bean id="car" class="com.sjg.beans.Car">
        <property name="beanId">
            <idref bean="yamahaEngine" />
    <bean id="yamahaEngine" class="com.sjg.beans.YamahaEngine" />
    <bean id="bajajEngine" class="com.sjg.beans.BajajEngine" />

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns=""; xmlns:xsi="";


  <!-- FIXME change it to the project's website -->

package com.sjg.test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;

import com.sjg.beans.Car;

public class Test {
    public static void main(String[] args) {
        BeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/sjg/common/application-context.xml"));
        Car car = factory.getBean("car", Car.class);;


Please find attached the as an maven project as a reference. Feel free to pass your comments in the blog.

About author

User Image

A software developer on JDK and JEE platform. I am passionate about Java technology and always an explorer and learner in new technologies in Java. I have experience on Open Source technologies like Struts, Spring, Jsf etc.. and even strong knowledge on Integration/Distributed world like Ejb, Web Services and Restful Services.



Be the first person to write a comment for this Blog
Load More

No More Comments

Leave a Comment

Your comment has been posted and will appear soon.