Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

conditional @Autowired?

Tags:

I have a HsqldbReconciler (for "work" with a HSQLDB database) which I autowired, like:

@Autowired
HsqldbReconciler hsqldbReconciler;

In Future there will be a OracleReconciler, MssqlReconciler, etc. I will need to use them accordingly to the type of connection a user has chosen.

How should I implement this? Usually I would have a kind of factory, which returns only the needed Reconciler. The only way in spring, I can currently imagine, is to Autowire an instance of each Reconciler, then use one of them in the code. Is there a better way?

like image 845
tobi Avatar asked Aug 20 '16 13:08

tobi


People also ask

How do you Autowire conditional beans in spring boot?

You can use @Autowired(required=false) and null check in stopScheduler method.

What is spring boot conditional?

It's used to indicate whether a given component is eligible for registration based on a defined condition.

What is the difference between @qualifier and Autowired?

The difference are that @Autowired and @Qualifier are the spring annotation while @Resource is the standard java annotation (from JSR-250) . Besides , @Resource only supports for fields and setter injection while @Autowired supports fields , setter ,constructors and multi-argument methods injection.

Why is @autowired annotation not recommended?

If we had used Autowired, we would have been getting the following error in the test. Therefore, it is not recommended to use Autowired. Safety — Forces Spring to provide mandatory dependencies. We make sure that the created objects are valid after construction.


2 Answers

make a Factory Class that will contain all your beans, e.g

@Component
class Factory{
  @Autowired HsqldbReconciler hsqldb;
  @Autowired OracleReconciler oracle;
  @Autowired MssqlReconciler mssql;

  public Object getInstance(String type){
    switch(type){
     case "mssql" : return mssql;
     case "oracle" : return oracle;
     // and so on  
     default : return null;
   }

  }

}

Now use this Factory as follows

class SomeClass{

  @Autowired private Factory factory;

  public Object someMethod(){
    Object reconciler = factory.getInstance("mssql");
    ((MssqlReconciler)reconciler).someMethod();
  }
}
like image 76
Ekansh Rastogi Avatar answered Sep 26 '22 16:09

Ekansh Rastogi


Define them in your Config with the same name, but different conditions:

@Bean(name = "dbReconciler")
@Conditional(HsqldbReconcilerEnabled.class)
public ReconcilerBase getHsqldbReconciler() {
    return new HsqldbReconciler();
}
@Bean(name = "dbReconciler")
@Conditional(OracleReconcilerEnabled.class)
public ReconcilerBase getOracleReconciler() {
    return new OracleReconciler();
}
@Bean(name = "dbReconciler")
@Conditional(MssqlReconcilerEnabled.class)
public ReconcilerBase getMssqlReconciler() {
    return new MssqlReconciler();
}

create conditions reading from app.properties:

HsqldbReconciler.enabled=true
OracleReconciler.enabled=false
MssqlReconciler.enabled=false

like this:

public class HsqldbReconcilerEnabled implements Condition {
    private static final String PROP_ENABLED = "HsqldbReconciler.enabled";   
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
         String property = context.getEnvironment().getProperty(PROP_ENABLED);
         return Boolean.parseBoolean(property);
    }
}
// etc...

use like:

@Autowired
@Qualifier("dbReconciler")
ReconcilerBase dbReconsiler;

ensure you're not enabling multiple beans at the same time.

like image 30
Alex Avatar answered Sep 22 '22 16:09

Alex