I have a bean which I use to load cache. I give the key for cache from Spring injection. I am duplicating the same bean just for the property and Spring is creating multiple instances of same bean. Is there anyway I can use one instance of the bean?
<aop:config>
<aop:pointcut id="terminalPointcut"
expression="execution(* *..TerminalDao.getTerminals())" />
<aop:pointcut id="miscPointcut"
expression="execution(* *..MiscDao.getMiscTableList(*))" />
<aop:pointcut id="errorPointcut"
expression="execution(* *..ErrorDao.getErrorList())" />
<aop:advisor id="terminalCacheLoaderAdvisor"
advice-ref="terminalCacheLoaderAdvice" pointcut-ref="terminalPointcut" />
<aop:advisor id="miscCacheLoaderAdvisor"
advice-ref="miscCacheLoaderAdvice" pointcut-ref="miscPointcut" />
<aop:advisor id="errorCdDetailCacheLoaderAdvisor"
advice-ref="errorCacheLoaderAdvice" pointcut-ref="errorPointcut" />
</aop:config>
<bean id="miscCacheLoaderAdvice" class="com.temp.ehCache.interceptor.CacheLoader">
<property name="cacheManager" ref="simpleCacheManager" />
<property name="cache_data_key" value="MISC_DATA_KEY" />
</bean>
<bean id="errorCacheLoaderAdvice" class="com.temp.ehCache.interceptor.CacheLoader">
<property name="cacheManager" ref="simpleCacheManager" />
<property name="cache_data_key" value="ERROR_DATA_KEY" />
</bean>
You obviously need the same bean twice with two different configurations. So even if it were possible to combine it in one bean it would be a total pain (You could e.g. use ThreadLocals to set the property as needed etc.)
I’d say change your design. If
com.temp.ehCache.interceptor.CacheLoaderis heavy, try to extract the heavy stuff to a delegate bean that can be used by theCacheLoaderbeans. Keep theCacheLoaderbeans as small as possible, and it won’t be a problem to have more than one of them around (as long as you don’t use autowiring by type).