Google

Feb 19, 2014

Spring lookup-method injection to inject prototype scoped bean into a singleton bean

This post extends Tutorial to understand Spring scopes -- singleton Vs prototype.

Step 0: You need asm and cgilib libraries in addition to Spring libraries.



Step 1: define the Dao (Data Access Object) interface.


package com.mycompany.understanding.spring;

public interface MyDao {
 abstract void printData();
}

Step 2: Define the Dao implementation.

package com.mycompany.understanding.spring;

public class MyDaoImpl implements MyDao {

 @Override
 public void printData() {
  System.out.println("printing data"); 
  System.out.println(this);
 }
}


Step 3: Define the service interface.

package com.mycompany.understanding.spring;

public interface MyService {
 abstract void performTask();
}

Step 4: Define the service implementation. Note that the class is abstract as Spring will decorate this class with cgilib.

package com.mycompany.understanding.spring;

public abstract class MyServiceImpl implements MyService {

 protected abstract MyDao createMyDao();

 @Override
 public void performTask() {
  System.out.println("Performing tasks .............");
  createMyDao().printData();
 }
}

Step 5: The spring context file applicationContext.xml that wires up dao and service. Take note of the "lookup-method".

<?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="myDaoDef" class="com.mycompany.understanding.spring.MyDaoImpl" scope="prototype"/>
    
    <bean id="myServiceDef" class="com.mycompany.understanding.spring.MyServiceImpl" scope="singleton"> 
       <lookup-method name="createMyDao" bean="myDaoDef" /> 
    </bean>

</beans>

Step 6: Executable main class.

package com.mycompany.understanding.spring;

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

public class MyMainApp {
 
 public static void main(String[] args) {
  ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
  for (int i = 0; i <3; i++) {
   MyService service = (MyService) applicationContext.getBean("myServiceDef");
   System.out.println(service);
   service.performTask();
  }
 }
}

Output if you run the above class

com.mycompany.understanding.spring.MyServiceImpl$$EnhancerByCGLIB$$35dfd4bb@750efc01
Performing tasks .............
printing data
com.mycompany.understanding.spring.MyDaoImpl@2ac2e1b1
com.mycompany.understanding.spring.MyServiceImpl$$EnhancerByCGLIB$$35dfd4bb@750efc01
Performing tasks .............
printing data
com.mycompany.understanding.spring.MyDaoImpl@606f4165
com.mycompany.understanding.spring.MyServiceImpl$$EnhancerByCGLIB$$35dfd4bb@750efc01
Performing tasks .............
printing data
com.mycompany.understanding.spring.MyDaoImpl@282e7f59


Single instance of service has 3 separate instances of  Dao.

Labels: ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home