Blog Image

Instance Factory Method Instantiation  

Here we are going to explore about spring provided Instance Factory Method Instantiation to know how we can work with Instance Factory Method Instantiation in our projects.

Lets see the example:
Technologies used :-
1) java (1.8).
2) maven (3.5.2).
3) Spring (4.0.0).

Project Structure

Execute below command for creating maven project.
mvn archetype:generate -DgroupId=com.sjg
-DartifactId=instance-factory-method-instantiation
-DarchetypeArtifactId=maven-archetype-quickstart
-DinteractiveMode=false

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

Classes
GoogleMapRenderer.java
package com.sjg.beans;

import com.sjg.ext.beans.IGoogleEngine;

public class GoogleMapRenderer {
    private IGoogleEngine googleEngine;

    public void renderMap(String source, String destination) {
        String[] directions = null;

        directions = googleEngine.getDirections(source, destination);
        System.out.println("directions : ");
        for (String direction : directions) {
            System.out.println(direction);
        }
    }

    public void setGoogleEngine(IGoogleEngine googleEngine) {
        this.googleEngine = googleEngine;
    }

}
Interface
IGoogleEngine.java
package com.sjg.ext.beans;

public interface IGoogleEngine {
    String[] getDirections(String source, String destination);

}
IndiaGoogleEngine.java
package com.sjg.ext.beans;

public class IndiaGoogleEngine implements IGoogleEngine {

    public String[] getDirections(String source, String destination) {
       
        return new String[] { "(indx1, indy1)", "(indx2, indy2)" };
   
    }

}
USGoogleEngine.java
package com.sjg.ext.beans;

public class USGoogleEngine implements IGoogleEngine {
    public String[] getDirections(String source, String destination) {
        return new String[] { "(usx1, usy1)", "(usx2, usy2)" };
    }

}
GoogleEngineServiceLocator.java
package com.sjg.util;

import com.sjg.ext.beans.IGoogleEngine;
import com.sjg.ext.beans.IndiaGoogleEngine;
import com.sjg.ext.beans.USGoogleEngine;

public class GoogleEngineServiceLocator {
    public IGoogleEngine getIndiaGoogleEngine() {
       
        return new IndiaGoogleEngine();
    }

    public IGoogleEngine getUSGoogleEngine() {
       
        return new USGoogleEngine();
    }

}
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="googleMapRenderer" class="com.sjg.beans.GoogleMapRenderer">
        <property name="googleEngine" ref="usGoogleEngine" />
    </bean>

    <bean id="googleEngineServiceLocator" class="com.sjg.util.GoogleEngineServiceLocator" />
    <bean id="indiaGoogleEngine" factory-bean="googleEngineServiceLocator"
        factory-method="getIndiaGoogleEngine" />
    <bean id="usGoogleEngine" factory-bean="googleEngineServiceLocator"
        factory-method="getUSGoogleEngine" />

</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>instance-factory-method-instantiation</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>instance-factory-method-instantiation</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>4.0.0.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.GoogleMapRenderer;

public class Test {
    public static void main(String[] args) {
        BeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/sjg/common/application-context.xml"));
        GoogleMapRenderer renderer = factory.getBean("googleMapRenderer", GoogleMapRenderer.class);
        renderer.renderMap("source1", "dest1");
    }

}

Please find attached the instance_factory_method_instantiation.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.