【问题标题】:Loading spring beans in jar based on application基于应用程序在jar中加载spring bean
【发布时间】:2017-08-04 07:24:20
【问题描述】:

我有一个叫做 common.jar 的 jar,其中包含两个缓存管理器 bean,如下所示,

@Bean
public CacheManager abcCacheManager() {
  /* Bean for abc service */
}

@Bean
public CacheManager xyzCacheManager() {
  /* Bean for xyz service */
}

这两个 bean 都配置了不同的缓存名称,因为我有两个不同的缓存(每个应用程序 1 个)。

现在当我运行 abc 服务时,两个缓存管理器都被激活并加载两个缓存。

有没有办法只能根据正在调用的应用程序触发特定的 bean?

【问题讨论】:

  • 在运行时触发 bean 或设置一些属性,以便在应用程序启动时它只包含您需要的 bean?

标签: java spring spring-mvc caching


【解决方案1】:

使用@Conditional 注释组之一

参见the doc 中的 44.3 条件注释

在您的情况下,它可能是 @ConditionalOnClass 或 @ConditionalOnMissingClass

【讨论】:

    猜你喜欢
    • 2015-04-09
    • 1970-01-01
    • 2012-01-16
    • 1970-01-01
    • 2018-01-29
    • 1970-01-01
    • 1970-01-01
    • 2020-07-20
    • 2023-03-23
    相关资源
    最近更新 更多