覆盖在孩子上下文父上下文中定义的豆 [英] Overriding the bean defined in parent context in a child context

查看:157
本文介绍了覆盖在孩子上下文父上下文中定义的豆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的应用程序已经可以支持多租户的要求。每个登上客户可能潜在地覆盖1个或多个豆或在核心平台级别定义一个bean(普通code /定义)的一些属性。我想知道什么是处理这个问题的最好办法。

Our app has a requirement to support multi-tenancy. Each of the boarded customer might potentially override 1 or more beans or some properties of a bean defined at the core platform level (common code/definitions). I am wondering what is the best way to handle this.

推荐答案

Spring允许你多次重新定义了同一个bean的名字,并采取给定名称处理的最后一个bean定义是获胜的人。因此,例如,您可能有定义你的核心豆的XML文件,并导入,在一个特定的客户端的XML文件,这也重新定义了其中的一些豆类。这是一个有点脆弱,不过,因为没有机制来明确说这个bean定义是一个覆盖。

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".

我发现,最干净的方法来处理这​​是使用new @Bean 在Spring 3 -syntax介绍。而不是定义bean作为XML,你在Java中定义它们。所以,你的核心豆会在一个 @Bean -annotated类中定义,和你的客户会CONFIGS的子类,并重写相应的豆子。这使您可以使用标准的java @覆盖注释,明确地表明一个给定的bean定义被重写。

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.

这篇关于覆盖在孩子上下文父上下文中定义的豆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆