javaspringdependency-injectioninversion-of-controljavabeans

Spring Bean hierarchy of objects based on tenant/region


I have a spring webapp. Here's a simple structure to explain my problem

java/
└── com/
    └── example/
        ├── controller/
        │   └── MyController.java       // REST Controller
        ├── service/
        │   └── MyService.java          // Service interface
        ├── client/
        │   └── EC2Client.java          // EC2Client

MyController injects MyService and MyService injects EC2Client.
There are multiple Beans of EC2Client. These are used as per the region. Region is received through the rest API in controller.

I would like to create bean hierarchy based on regions(us-east, us-west, etc). So for each region, I should have a controller, service, and a ec2client bean.

Ideally if I had to create a single class bean based on region I could create quailifier beans. But how do I do this for the whole heirarchy. I could potentially have 5+ regions So, want to make this dynamic in nature


Solution

  • For any one who stumbles across this, solution was to use Spring Custom Scopes.