Dropwizard and Guice: injecting Environment

前端 未结 3 2034
隐瞒了意图╮
隐瞒了意图╮ 2021-02-15 17:53

I am currently building a Dropwizard + Guice + Jersey-based application where the database access is being handled by JDBI for the time being.

What I am trying to achiev

3条回答
  •  温柔的废话
    2021-02-15 18:33

    This is how I use Guice with Dropwizard. Inside your run() method add the line

    Guice.createInjector(new ConsoleModule()); 
    

    You cannot inject Environ

    Create the class ConsoleModule

    public class ConsoleModule extends AbstractModule {
    
        //configuration and env variable declaration
    
        public  ConsoleModule(ConsoleConfiguration consoleConfig, Environment env)
        {
            this.consoleConfig = consoleConfig;
            this.env= env;
        }
    
        protected void configure()
        {
            //You should not inject Configuration and Environment in your provider since you are mixing     
            //dropwizard framework stuff with Guice.Neverthless you will have to bind them in the below order
    
            bind(Configuration.class).toInstance(consoleConfig.class);
            bind(Environment.class).toInstance(env.class);
            bind(UserDAO.class).toProvider(UserDAOProvider.class).in(Singleton.class);
        }
    }
    

提交回复
热议问题