Why there's no configuration file at all for dependency injection with Google Guice?

后端 未结 6 2276
生来不讨喜
生来不讨喜 2021-02-19 23:51

I am checking out Google Guice as DI framework but I am a bit puzzled: why there\'s no configuration file at all?

I found a partial explanation on this question but it i

6条回答
  •  无人及你
    2021-02-20 00:50

    It is trivial to introduce boostrapping using configuration files if you are so inclined. We use Guice together with a simple API that loads property files where the things go that really need to be parameterized. This can be used together with @Named annotations and such, and of course you can have some conditionals in modules (though it is a good idea not to over-do that).

    This is an example of how part of our bootstrapping is set up:

    public class MetModules extends AbstractModule {
    
        private static final Logger log = LoggerFactory.getLogger(MetModules.class);
    
        private final Settings settings;
    
        public MetModules(Settings settings) {
            this.settings = settings;
        }
    
        @Override
        protected void configure() {
    
            // common (stage independent modules) go here
            install(new CommandsModule());
            install(new ServletsModule());
            install(new DataBaseModule(settings));
            install(new JobsModule(settings));
    
            // any development/ production specific modules
            Stage stage = currentStage();
            if (Stage.DEVELOPMENT.equals(stage)) {
                configureForDevelopment();
            } else { // PRODUCTION
                configureForProduction();
            }
        }
    
        /**
         * Install modules that will be used in development.
         */
        private void configureForDevelopment() {
    
            // Mock implementation of email delivery that just logs it got a
            // message rather than trying to send it.
            install(new AbstractModule() {
                @Override
                protected void configure() {
                    bind(Delivery.class).toInstance(new Delivery() {
    
                        public String deliver(MailMessageExchange exchange)
                                throws DeliveryException {
                            log.info("email message: "
                                    + exchange.getMessage().getMailMessage()
                                    + " to "
                                    + Arrays.asList(exchange.getMessage()
                                            .getMailMessage().getTo())
                                    + " (not sent)");
                            return "fooMessageId";
                        }
                    });
                }
            });
    
            // local in-memory registry suffices
            install(new LocalServiceRegistryModule());
    
            // local in memory db implementations of services
            install(new LocalServicesModule());
        }
    
        /**
         * Install modules that will be used in production.
         */
        private void configureForProduction() {
            // we really only need this (error interception and audit logging)
            // in production
            install(new AopModule());
            install(new ZooKeeperServiceRegistryModule());      }
    }
    

    Where Settings is what reads our properties files and such. Right now development/ production together with specific settings overrides specific for deployments seems to do the job for us, but we could go further with this if we wanted to obviously.

提交回复
热议问题