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.
1 Answers
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.
-
are you sure you can re-define a bean with the same id/name in a child ctx? – Aravind Yarram Nov 22 '10 at 21:04
-
9looking at this page http://stackoverflow.com/tags/spring/topusers I'd think he knows what he's talking about – Sean Patrick Floyd Nov 22 '10 at 21:10
-
thank you. I am not questioning the correctness of his post. I wanted some re-assurance on that particular statement as I have been told by my dev resource that this is not possible in new versions of spring i.e. 2.5.x and later. He tried to re-define the bean (that was defined in parent context) in a child context with the same name and he received an error during startup. – Aravind Yarram Nov 22 '10 at 21:29
-
@Pangea: It logs warnings, yes, but not exceptions. Can you give us an example? – skaffman Nov 22 '10 at 22:10
-
@skaffman: I have not tried this but was under the impression that it is not possible, can you possibly share an detailed example outlining the process of re-defining the beans ? – Rachel Nov 23 '10 at 03:48
-
4it is possible. instead of discussing this topic here, why don't you just make a simple test as see for yourself? :-P – Neeme Praks Nov 23 '10 at 16:36
-
Weirdly enough, it did not work for me. To be more specific, when I have resourceServerConfigurer bean defined in ResourceServerConfig, and another resourceServerConfigurer in ResourceServerTestConfig extends ResourceServerConfig, the overriding works, but in a log I get a warning that bean from ResourceServerConfig overrides ResourceServerTestConfig, not vice versa (but debugging shows otherwise). But when I move ResourceServerTestConfig to "test" sources, the config gets created but overriding does not happen, Spring uses bean from ResourceServerConfig. – Maksim Gumerov Apr 11 '18 at 14:44
-
Also, I failed to find any indications on how Spring processes inherited Java configs in its manual. Say, "modularized configurations" does not consider this scenario, unlike @Import, nested configurations etc. – Maksim Gumerov Apr 11 '18 at 14:47
-
This doesn't work. I was actually under impression that this method works but actually, spring overrides the bean based on the file path (package name + class name) order of configuration file. So basically, just make sure your override configuration file comes after the main file (alphabetically). – Shivankur Pal Jan 12 '22 at 18:50