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
-DartifactId=example-on-aware-interface
-DarchetypeArtifactId=maven-archetype-quickstart
-DinteractiveMode=false

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

Classes
Car.java
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);
        engine.start();
        System.out.println("running...");
    }

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

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

    }

}
Interface
IEngine.java
package com.sjg.beans;

public interface IEngine {
    void start();

}
YamahaEngine.java
package com.sjg.beans;

public class YamahaEngine implements IEngine {

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

    }

}
BajajEngine.java
package com.sjg.beans;

public class BajajEngine implements IEngine {

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

    }

}
application-context.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";
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">;

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

</beans>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">;
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.sjg</groupId>
  <artifactId>example-on-aware-interface</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>example-on-aware-interface</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.srimanjavagroup.com</url>;

  <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.0.1.RELEASE</version>
        </dependency>
    </dependencies>
</project>
Test.java
package com.sjg.test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

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

}

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


About author

User Image
Sriman

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.

0

-Comments

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.