5

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?

tobi
  • 753
  • 1
  • 14
  • 25
  • Probably helpful: http://stackoverflow.com/questions/19225115/how-to-do-conditional-auto-wiring-in-spring – Thilo Aug 20 '16 at 13:49

2 Answers2

2

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();
  }
}
Ekansh Rastogi
  • 2,418
  • 2
  • 14
  • 23
2

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.

Alex
  • 41
  • 4