×
DocsConsole

DocsConsole

Service Locator Pattern

By | | Updated : 2018-02-10 | Viewed : 344 times

Service Locator Pattern

Service-Locator Pattern is design pattern, which is used by Spring IOC container. Service-Locator is mainly used for instantiating the objects. Spring Container/Spring IOC container will instantiate the objects by reading the meta-data, which is nothing but application-context.xml file.

ServiceLocatorPattern.

Service-Locator Pattern is design pattern, which is used to creating the objects. Here Service-Locator Pattern uses the meta-data to create/instantiate the objects.

Please create the maven project as given below.

Spring Service Locator Pattern

Please create the ApplicationContext.java as given below.

ApplicationContext.java
/**
* 
* @author:Amar
* @since:2018-02-03 
*
*/

package com.docsconsole.spring;

public class ApplicationContext {

	SpringBeanCache springBeanCache;

	public ApplicationContext() {
		springBeanCache = new SpringBeanCache();
	}

	public SpringBean getBean(String beanName) {
		
		SpringBean bean = springBeanCache.getSpringBeanCacheBean(beanName);
		if (bean !=null) {
			return bean;
		} else {
			if (beanName == "ASpringBean") {
				SpringBean aSpringBean = new ASpringBean("ASpringBean");
				springBeanCache.beanCacheList.add(aSpringBean);
				return aSpringBean;
			} else {
				SpringBean bSpringBean =new BSpringBean("BSpringBean");
				springBeanCache.beanCacheList.add(bSpringBean);
				return bSpringBean;
			}
		}
	}

}

Please create the SpringBeanCache.java as given below.

SpringBeanCache.java
/**
* 
* @author:Amar
* @since:2018-02-03 
*
*/

package com.docsconsole.spring;

import java.util.ArrayList;
import java.util.List;

public class SpringBeanCache {

	public static List<SpringBean> beanCacheList;
	
	public SpringBeanCache(){
		beanCacheList =new ArrayList<SpringBean>();
	}
	public SpringBean getSpringBeanCacheBean(String beanName){
		   
	      for (SpringBean springBean:beanCacheList) {
	         if(beanName.equalsIgnoreCase(springBean.getBeanName())){
	            return springBean;
	         }
	      }
	      return null;
	   }
	

}

Please create the SpringBean.java as given below.

SpringBean.java
/**
* 
* @author:Amar
* @since:2018-02-03 
*
*/

package com.docsconsole.spring;

public interface SpringBean {
	
	public String getBeanName();
	public void setBeanName(String beanName);

}

Please create the ASpringBean.java as given below.

ASpringBean.java
/**
* 
* @author:Amar
* @since:2018-02-03 
*
*/

package com.docsconsole.spring;

public class ASpringBean implements SpringBean{
	

	public String beanName;
	
	public ASpringBean(String beanName){
		this.beanName = beanName;
	}

	public String getBeanName() {
		return beanName;
	}

	public void setBeanName(String beanName) {
		this.beanName = beanName;
	}

}

Please create the BSpringBean.java as given below.

BSpringBean.java
/**
* 
* @author:Amar
* @since:2018-02-03 
*
*/

package com.docsconsole.spring;

public class BSpringBean implements SpringBean{

	public String beanName;
	
	public BSpringBean(String beanName){
		this.beanName = beanName;
	}

	public String getBeanName() {
		return beanName;
	}

	public void setBeanName(String beanName) {
		this.beanName = beanName;
	}
}

Please create the TestMainClient.java as given below.

TestMainClient
/**
* 
* @author:Amar
* @since:2018-02-03 
*
*/

package com.docsconsole.spring;

public class TestMainClient {

	public static void main(String[] args) {
		
		System.out.println("------------Main mehtod has been started here------------------------------");
		ApplicationContext applicationContext = new ApplicationContext();
		System.out.println("------------ApplicationContext has been created here------------------------");
		ASpringBean aSpringBean = (ASpringBean) applicationContext.getBean("ASpringBean");
		System.out.println("------------aSpringBean object has been created------------------------");
		System.out.println("------------aSpringBean object's hashcode------------------------"+aSpringBean.hashCode());
		
		ASpringBean a1SpringBean = (ASpringBean) applicationContext.getBean("ASpringBean");
		System.out.println(a1SpringBean.hashCode());
		System.out.println("------------a1SpringBean object has been created------------------------");
		System.out.println("------------a1SpringBean object's hashcode------------------------"+a1SpringBean.hashCode());
		
		BSpringBean bSpringBean = (BSpringBean) applicationContext.getBean("BSpringBean");
		System.out.println(bSpringBean.hashCode());
		System.out.println("------------bSpringBean object has been created------------------------");
		System.out.println("------------bSpringBean object's hashcode------------------------"+bSpringBean.hashCode());
		
		
		BSpringBean b1SpringBean = (BSpringBean) applicationContext.getBean("BSpringBean");
		System.out.println(b1SpringBean.hashCode());
		System.out.println("------------b1SpringBean object has been created------------------------");
		System.out.println("------------b1SpringBean object's hashcode------------------------"+b1SpringBean.hashCode());
	}

}

If you test the TestMainClient the result will be as given below.

Spring Service Locator Pattern Result

If we observe the above result, First time object instantiated for ASpringBean. But second time ASpringBean is returned from SpringBeanCache. That’s why second time hashcode is printed the same as earlier. Two times same hashcode has been printed for ASpringBean as well as BSpringBean.

Leave A Reply