English 中文(简体)
Guice, bind different database provider based on yaml config
原标题:

I have an application config file that looks something like this:

database:
  type: [db-type]
  username: [name]
  password: [pw]
  server: [ip]
  database: [db-name]

db-type can be any of the following: {postgresql, mysql, mssql, file}. I wanted to configure the binding, such that (it s hibernate based) the app loads a special Provider of SessionFactory depending on which of the values is used, i.e. a PostgresqlSessionFactoryProvider.

The problem is, that Guice also takes care of injecting an instance of Config.class into classes that need access to it. Now I need to access the config, while setting up the binding... It s sort of a chicken-egg problem.

How do I get around that?

最佳回答

I have found a way to do this. It might not be the best way to do it, so if you know better, I will still look at the answers.

The point is, that the binder does first have to be completely configured, so it can provide an instance of Config.class.

So I came up with a custom Provider for SessionFactory.class that needs a config and the guice injector itself, so that when all the info has been slurped by guice, it has the means to provide a different implementation based on the config.

public class SessionFactoryProvider implements Provider<SessionFactory> {

    private Config config;
    private Injector injector;

    @Inject
    public SessionFactoryProvider(Config config, Injector injector) {
        this.config = config;
        this.injector = injector;
    }

    @Override
    public SessionFactory get() {
        switch (config.database.type) {
        case postgresql:
            return injector.getInstance(PostgresqlSessionFactoryProvider.class).get();
        case mysql:
            return injector.getInstance(MysqlSessionFactoryProvider.class).get();
        case file:
            return injector.getInstance(FileBasedSessionFactoryProvider.class).get();
            /* some more providers... */
        default:
            return injector.getInstance(FileBasedSessionFactoryProvider.class).get();
        }
    }
}

What do you think? Is this a good way to do this?

问题回答

暂无回答




相关问题
Multiple Hibernate instances using C3P0

I am facing a weird problem and it seems to be c3p0 related. I am starting two instances of an app in the same java vm which interact with each other. After some operations "APPARENT DEADLOCK" ...

Hibernate vs Ibatis caching

We can speed up a hibernate app easyly with 2nd level cache using infinispan or ehcache/terracotta,... but ibatis only have a simple interface to implement for caching. And hibernate knows more ...

Using annotations to implement a static join in hibernate

I m relatively new to hibernate and was wondering if someone could help me out. While I have no issues implementing a normal join on multiple columns in hibernate using the @JoinColumns tag, I m ...

Hibernate query with fetch question

I have a 2 entities in a One-To-Many relationship: OfficeView.java: public class OfficeView implements java.io.Serializable { private Integer officeId; private String addr1; private ...

hibernate interceptors : afterTransactionCompletion

I wrote a Hibernate interceptor : public class MyInterceptor extends EmptyInterceptor { private boolean isCanal=false; public boolean onSave(Object entity, Serializable arg1, Object[] arg2, String[]...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

Hibernate/GORM: collection was not processed by flush()

I have an integration test in my Grails application that fails when I try to save an entity of type Member invitingMember.save(flush: true) This raises the following exception org.hibernate....

Hibernate Criteria API equivalent for "elements()"

Is it possible to implement the following query using Criteria API? select order from ORDER as order,ITEM as item where item.itemID like ITM_01 and item in elements(order.items)

热门标签