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

Step 1: You need asm and cgilib libraries in addition to Spring libraries shown below.

spring-lokkup-needs-asm-cgilib

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

Step 3: Define the Dao implementation.

Step 4: Define the service interface.

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

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

Step 7: Executable main class.

Output if you run the above class

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.


Java Interview FAQs

800+ Java Interview Q&As

Top