覆盖子上下文中父上下文中定义的bean

Ara*_*ram 22 architecture spring java-ee multi-tenant

我们的应用程序需要支持多租户.每个登机客户都可能会覆盖一个或多个bean或核心平台级别定义的bean的某些属性(公共代码/定义).我想知道处理这个问题的最佳方法是什么.

ska*_*man 33

Spring allows you to redefine the same bean name multiple times, and takes the last bean definition processed for a given name to be the one that wins. So for example, your could have an XML file defining your core beans, and import that in a client-specific XML file, which also redefines some of those beans. It's a bit fragile, though, since there's no mechanism to specifically say "this bean definition is an override".

I've found that the cleanest way to handle this is using the new @Bean-syntax introduced in Spring 3. Rather than defining beans as XML, you define them in Java. So your core beans would be defined in one @Bean-annotated class, and your client configs would subclass that, and override the appropriate beans. This allows you to use standard java @Override annotations, explicitly indicating that a given bean definition is being overridden.

  • 看看这个页面http://stackoverflow.com/tags/spring/topusers我认为他知道他在说什么 (9认同)
  • 有可能的.而不是在这里讨论这个主题,为什么你不做一个简单的测试,因为你自己看?:-P (4认同)