×
DocsConsole

DocsConsole

Method Injection

By | | Updated : 2018-01-07 | Viewed : 425 times

Spring Method Injection

When we want to inject the Prototype typed Spring bean into Singleton typed, then Prototype typed bean will be instantiated only once. Because both beans life cycles are different. So we need to use the method injection to solve this problem.

    Prototype vs Single

  • Prototype: Spring bean is instantiated only once per each request to Spring container.

  • Singleton: instance is instantiated only once per Spring container.

Singleton: instance is instantiated only once per Spring container.

We will understand the problem if the Prototype Spring bean is injected into the Singleton Spring bean. Please look into the below given example.

Please crate bellow given maven project.

Spring Method Injection

Please update pom.xml file with below given pom.xml.

pom.xml.
<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.docsconsole.spring</groupId>
  <artifactId>SpringMethodInjection</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>
	<dependency>
	 <groupId>org.springframework</groupId>
	 <artifactId>spring-context</artifactId>
	 <version>3.0.0.RELEASE</version>
	 </dependency>
	 
	 <dependency>
        	<groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>2.2.2</version>
    </dependency>
   </dependencies>
   
</project>

Please create ASpringBean as given below.

ASpringBean.java
/**
* 
* @author:Amar
* @since:2017-05-19 
*
*/


package com.docsconsole.spring;

public class ASpringBean {
    private String message;
    public ASpringBean()
    {
       System.out.println("Prototype Bean Instantiated !!");
    }      
    public void setMessage(String message){
       this.message  = message;
    }
    public String  getMessage(){
       return this.message;
    }
}

Please create the BSpringBean as given below.

BSpringBean.java
/**
* 
* @author:Amar
* @since:2017-05-19 
*
*/

package com.docsconsole.spring;

public class BSpringBean {
    private ASpringBean aSpringBean;
    public BSpringBean()
    {
        System.out.println("Singleton Bean Instantiated !!");
    }
	public ASpringBean getaSpringBean() {
		return aSpringBean;
	}
	public void setaSpringBean(ASpringBean aSpringBean) {
		this.aSpringBean = aSpringBean;
	}
    
}

Please create the springbeans-config.xml as given below in the same package.

springbeans-config.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-3.0.xsd">


	   <bean id="aSpringBean" class="com.docsconsole.spring.ASpringBean" scope="prototype">
              <property name="message" value="Test Message" />
       </bean>      
       <bean id="bSpringBean" class="com.docsconsole.spring.BSpringBean" >
              <property name="aSpringBean" ref="aSpringBean"/> 
              <!-- <lookup-method bean="aSpringBean" name="getaSpringBean"/> -->                   
       </bean>
</beans>

Please crate TestMainClient.java as given below.

TestMainClient.java
/**
* 
* @author:Amar
* @since:2017-05-19 
*
*/

package com.docsconsole.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestMainClient {
	
	public static void main(String[] args) {
		
		ApplicationContext context = new ClassPathXmlApplicationContext("com/docsconsole/spring/springbeans-config.xml");
		BSpringBean singleton = (BSpringBean)context.getBean("bSpringBean");             
		ASpringBean aSpringBean = singleton.getaSpringBean();
		ASpringBean a1SpringBean = singleton.getaSpringBean();             
		System.out.println("aSpringBean is instanciated with hashcode"+aSpringBean.hashCode());
		System.out.println("a1SpringBean is instanciated with hashcode"+aSpringBean.hashCode());

	}
	
}

If you execute the TestMainClient the output will be as given below.

Spring Method Injection Result

If you observe the object’s hash code which are same. But we are expected two objects creation but here only one is created. If you want two objects creation we need to use Method injection. We can achieve Method injection by two ways which are given below.

  • 1. By using the lookup-method

  • 2. By implementing ApplicationContextAware

1. By using the lookup-method

Please change BSpringBean as given below.

/**
* 
* @author:Amar
* @since:2017-05-19 
*
*/


package com.docsconsole.spring;

public abstract class BSpringBean {
       public BSpringBean()
       {
              System.out.println("Singleton Bean Instantiated !!");
       }
       public abstract ASpringBean getaSpringBean();      
}

Please change the springbeans-config.xml as given below.

<?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-3.0.xsd">


	<bean id="aSpringBean" class="com.docsconsole.spring.ASpringBean" scope="prototype">
	 	<property name="message" value="Test Message" />
	</bean>      
	<bean id="bSpringBean" class="com.docsconsole.spring.BSpringBean" >
		<!--  <property name="aSpringBean" ref="aSpringBean"/>  -->
		<lookup-method bean="aSpringBean" name="getaSpringBean"/>                   
	</bean>
</beans>

If you execute the TestMainClient the output will be as given below.

Method Injection lookup-method

If you observe the about result, two objects are created. You can see two hashcodes.

2. By implementing ApplicationContextAware

Please change BSpringBean as given below.

public class BSpringBean implements ApplicationContextAware {
	
	private ApplicationContext applicationContext;
	
	private ASpringBean aSpringBean;

	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}
	
	public ASpringBean getaSpringBean() {
		return (ASpringBean)applicationContext.getBean("aSpringBean");
	}

	public void setaSpringBean(ASpringBean aSpringBean) {
		this.aSpringBean = aSpringBean;
	}
	
}

And please change springbeans-config.xml as given below.

<?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-3.0.xsd">


	<bean id="aSpringBean" class="com.docsconsole.spring.ASpringBean" scope="prototype">
	 	<property name="message" value="Test Message" />
	</bean>      
	<bean id="bSpringBean" class="com.docsconsole.spring.BSpringBean" >
		 <property name="aSpringBean" ref="aSpringBean"/> 
		<!-- <lookup-method bean="aSpringBean" name="getaSpringBean"/>   -->                 
	</bean>
</beans>

If you execute the TestMainClient you will get below given result.

Spring Method Injection ApplicationContextAware

If you observe here two objects are generated/instantiated. You can see two different hash code values.

Leave A Reply