简体   繁体   English

具有相同 bean 和不同属性的 Spring 配置文件

[英]Spring config file with same bean and different properties

I have a bean which I use to load cache.我有一个用于加载缓存的 bean。 I give the key for cache from Spring injection.我从 Spring 注入中给出了缓存的密钥。 I am duplicating the same bean just for the property and Spring is creating multiple instances of same bean.我只是为属性复制同一个 bean,Spring 正在创建同一个 bean 的多个实例。 Is there anyway I can use one instance of the 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> 
<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.您显然需要两次具有两种不同配置的同一个 bean。 So even if it were possible to combine it in one bean it would be a total pain (You could eg use ThreadLocals to set the property as needed etc.)因此,即使可以将它组合在一个 bean 中,这也将是一件非常痛苦的事情(例如,您可以使用 ThreadLocals 根据需要设置属性等)

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 the CacheLoader beans.如果com.temp.ehCache.interceptor.CacheLoader很重,请尝试将重的内容提取到可由CacheLoader bean 使用的委托 bean。 Keep the CacheLoader 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).保持CacheLoader bean 尽可能小,并且拥有多个它们不会成为问题(只要您不使用按类型自动装配)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM