具有相同 bean 和不同属性的 Spring 配置文件
我有一个用于加载缓存的 bean。我给出了 Spring 注入的缓存密钥。我只是为了该属性复制相同的 bean,并且 Spring 正在创建相同 bean 的多个实例。无论如何,我可以使用该 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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显然,您需要使用两种不同配置的同一个 bean 两次。因此,即使可以将它组合到一个 bean 中,这也会是一件很痛苦的事情(例如,您可以使用 ThreadLocals 根据需要设置属性等),
我想说的是更改您的设计。如果 com.temp.ehCache.interceptor.CacheLoader 很重,请尝试将重的内容提取到可以由 CacheLoader bean 使用的委托 bean。使
CacheLoader
beans 尽可能小,并且拥有多个它们不会成为问题(只要您不使用按类型自动装配)。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.CacheLoader
is heavy, try to extract the heavy stuff to a delegate bean that can be used by theCacheLoader
beans. Keep theCacheLoader
beans 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).