While at runtime, spring can replace beans created in the context of spring ( HotswappableTargetSource ), it is not intended for cases like yours.
Remember that there is one spring Context for your application, all threads use the same instances (in most cases), this means that when replacing a bean implementation, you emotionally do this for all users of your application. To prevent this, you are facing Thread security issues using thread locators, as indicated in another answer.
Although you can continue this approach and come to an implementation that does its job, it will definitely be a very far-fetched way to solve this problem.
You should take a step back and look at your problem from a more useful, systemic point of view. Rid your books on templates and see how this can be resolved, whether you use spring or another structure. Service Locator, Factory bean, etc., described in some of the answers above, is a step in the right direction.
Your use case is fairly common for multi-tenant applications.
For example, as mentioned in the question, each Tenant may have a different commission amount or even a different commission calculation algorithm. A simple solution for this would be to implement the CommissionCalculationService , which accepts tenantId , and any other domain object, on the basis of which the commission should be calculated, I would assume that it would be something like Order or Sale , which makes sense in your application.
Now you need a CommissionServiceFactory or ServiceLocator that will contain tenant-specific implementations of the CommissionCalculationService . Service Locator is created when the spring context is loaded, and implementation classes are also introduced at application startup.
When you want to calculate the commission for the tenant, you basically get tenantId from the userโs login, pass the tenant ID to your service locator, based on the transmitted tenantId , the service locator returns the corresponding instance of the service implementation. In your calling class, use this instance to calculate the commission for the tenant.
Another template to consider is the Strategy Pattern or even the Template Pattern .
On the bottom line, even if you want a specific logical tenant to run cleanly, don't do anything about changing the beans loaded into the context. You have classes in your context that can handle all your specific tenant logic. Rely on design patterns to use the right bean from the context based on the tenant ID.
Sorry if the answer was a bit detailed, I thought it was necessary to explain why I think that updating beans in a loaded spring Context is not a suitable solution.