Tuesday, August 9, 2016

Design Pattern - SingleTon

SingleTon class must be threadsafe , must produce only single instance per application per JVM. ENUM are immutable constants and hence  very easy to use for SingleTon class.


Method Injection  in Spring  IOC 


  < bean id="claimServiceManager" class="com.hp.eclaims.service.manager.ClaimServiceManager" > < property name="claimServiceDao" ref="claimServiceDao" /> < property name="claimServiceUtil" ref="claimServiceUtil" /> < lookup-method name="createNewTaskExecutor" bean="taskExecutor" /> < lookup-method name="createNewTask" bean="tasks" />
</ bean >
                 < bean id="tasks" class="com.hp.eclaims.service.util.ServiceTasks" scope="prototype">
                 < bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" scope="prototype" init-method="initialize" destroy-method="shutdown">


Above example shows how spring IOC achieves method injection (DI) .
Every time new objects of taskexecutor and tasks will get instantiated and passed into ClaimServiceManager thorugh abstract method createNewTaskExecutor and createNewTask . Those two are defined as  prototype scope in DD .

It is  helpful in  passing  prototype  scope object to singleton object type .