khalid
khalid

Reputation: 69

Spring boot inject bean at runtime based on request endpoint/request param

I have an interface that has two implementations, and I'd like to conditionally inject either of the two implementations in a spring boot service.

The point is that the eligible implementation should be picked up based on the request message (JSON mapped to a POJO).

My searches leaded me to implement a FactoryBean to control selecting between those two implementations, and to keep the factory telling spring that the beans are not singleton (by returning false for the isSingleton method).

But if this is the right way, I am still not sure how to get the request message to check it and return the right bean.

Can you please tell me if I am on the right track for what I am trying to attain?

=============

UPDATE

I do not want to pollute my code and deal with managing the relation between my service and the dependencies' implementation in the service.

Considering that I will need to deal with more implementations in the future, I need my service to care only about its responsibility.

  1. I need my service to have only one reference of the generic interface and deal with it in an abstracted way.
  2. I need to find a spring-based way to choose the right implementation for each request based on a condition that is derived from the request itself, and inject it in the service.

Upvotes: 3

Views: 4575

Answers (3)

Chirdeep Tomar
Chirdeep Tomar

Reputation: 4461

Spring has a concept of Conditional Bean...

Have a look here https://www.intertech.com/Blog/spring-4-conditional-bean-configuration/

Upvotes: 1

user10367961
user10367961

Reputation:

You could @Autowire both beans in the controller and decided based on the request which one to return.

Consider the below interface:

public interface MyInterface { ... }

Sample config:

@Configuration
public class MyConfig {

    @Bean("first")
    public MyInterface firstBean() { ... }

    @Bean("second")
    public MyInterface secondBean() { ... }

}

Sample controller:

@RestController
public class MyController {

    @Autowire
    @Qualifier("first")
    public MyInterface first;

    @Autowire
    @Qualifier("second")
    public MyInterface second;

    @GetMapping
    public MyInterface doStuff(@RequestBody body) {
        if(shouldReturnFirst(body)){
            return first;
        } else {
            return second;
        }
    } 

}

Note that you should most likely not do it this way though, but have a single service, say MyService that should implement this logic for you.

@Component
public class MyService {

    public MyInterface doStuff(body) {
        if(shouldReturnFirst(body)){
            // build your response here
        } else {
            // build your response here
        }
    }

}

And just delegate to the service from the controller

@GetMapping
public MyInterface doStuff(@RequestBody body) {
    return myService.doStuff(body);
} 

Upvotes: 1

sidgate
sidgate

Reputation: 15244

One option is to inject both beans and conditionally pick the required bean. You can autowire classes implementing same interface into a Map.

Following example uses a factory class to hide the conditional check.

@Component("type1")
public class Type1 implements SomeInterface{}

@Component("type2")
public class Type2 implements SomeInterface{}

@Component
public class MyTypeFactory {

  @Autowired
  private Map<String, SomeInterface> typesMap;

  public SomeInterface getInstance(String condition){
    return typesMap.get(condition);
  }

}

@Component
public class MyService {
  @Autowired
  private MyTypeFactory factory;

  public void method(String input){
     factory.getInstance(input).callRequiredMethod();
  }

}

Upvotes: 5

Related Questions