Step 1: You need asm and cgilib libraries in addition to Spring libraries shown below.
Step 2: define the Dao (Data Access Object) interface.
1 2 3 4 5 | package com.mycompany.understanding.spring; public interface MyDao { abstract void printData(); } |
Step 3: Define the Dao implementation.
1 2 3 4 5 6 7 8 9 10 | package com.mycompany.understanding.spring; public class MyDaoImpl implements MyDao { @Override public void printData() { System.out.println("printing data"); System.out.println(this); } } |
Step 4: Define the service interface.
1 2 3 4 5 | package com.mycompany.understanding.spring; public interface MyService { abstract void performTask(); } |
Step 5: Define the service implementation. Note that the class is abstract as Spring will decorate this class with cgilib.
1 2 3 4 5 6 7 8 9 10 11 12 | 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 6: The spring context file applicationContext.xml that wires up dao and service. Take note of the “lookup-method”.
1 2 3 4 5 6 7 8 9 10 11 12 | <?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 7: Executable main class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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
1 2 3 4 5 6 7 8 9 10 11 12 | 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. Hmm why will you need protype DAOs?. Just an example to demonstrate lookup method in Spring.