Like a white label spring beans

We have an application with a white designation (one application that supports corporate experience for several customers). We would like to be able to download a joint version of the component to support custom components for each client. For example, something like:

<!-- default service --> <bean id="service" class="com.blah.myService" primary="true"> <property name="myBean" ref="bean" /> </bean> <!-- custom service for client 123 --> <bean id="service_123" class="com.blah.myService"> <property name="myBean" ref="bean" /> </bean> <!-- default bean --> <bean id="bean" class="com.blah.Bean" primary="true"/> <!-- bean for client 123 --> <bean id="bean_123" class="com.blah.Bean" /> 

We tried to subclass ApplicationContext, and this works for the top-level bean, but auto-negotiated collaborators connect and cache during spring boot.

As an example, if I call getBean ("service"), I can intercept the call in my custom ApplicationContext and return service_123, but the "bean" property uses the cached version and does not call the getBean method again, so I cannot connect to the user version.

Is there an easy way to achieve this type of custom runtime injection?

+4
source share
3 answers

First of all, you may not need a subclass of ApplicationContext to implement such custom instance creation logic - you can create a BeanPostProcessor instead.

To solve the problem with the cached version of the bean, you can wrap the returned bean in some kind of proxy server - either using AOP or manually (for example, see TargetSource and its subclasses).

+1
source

It looks like this could be accomplished with spring profiles.

You will have a profile for each customer with special beans. Plus profile for customers by default.

In this article, you should start with profiles, although the use case is completely different.

+1
source

One way to manually load these beans:

 myAppContext.getBean("bean_" + customer.getId()); 

If you need your class to access the ApplicationContext instance, create a class to implement ApplicationContextAware.

0
source

Source: https://habr.com/ru/post/1299159/


All Articles