Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 is still not clear how I would be able to set my component roles (or any other thing I need to use a switch) without a config file.

Any help appreciated!

like image 617
JohnIdol Avatar asked Apr 19 '09 16:04

JohnIdol


People also ask

How does Guice dependency injection work?

Using GuiceIn each of your constructors that need to have something injected in them, you just add an @Inject annotation and that tells Guice to do it's thing. Guice figures out how to give you an Emailer based on the type. If it's a simple object, it'll instantiate it and pass it in.

What is Guice dependency injection?

Guice is an open source, Java-based dependency injection framework. It is quiet lightweight and is actively developed/managed by Google. This tutorial covers most of the topics required for a basic understanding of Google Guice and to get a feel of how it works.

Does Google use dependency injection?

Google Guice is one of the leading frameworks whose main work is to provide automatic implementation of dependency injection.

Is Guice lazy?

When does guice make the dependencies available? In general, construction is lazy (for non-singleton classes, this is intuitive - you don't know you need a new instance until someone tells you to inject one somewhere). The exception is for eager singletons which are constructed (wait for it) eagerly.


1 Answers

The configuration is in the code instead of config files, which is a valid decision for many scenarios.

Yes, it means that you have to rebuild (possibly just the modules) if you want to release a different way of plumbing your application - although of course you could still get some configuration values from command-line arguments, properties files etc if you want to.

If you regularly need to change your application plumbing and don't want to redeploy anything but a single file, Guice may not be for you. If on the other hand your main reason for using DI is to make your code clearer, and in production you'll always really use the same plumbing (or close enough) then Guice is a good option - there are often bits of logic you want to use when sorting out the plumbing anyway, and components which are generally hard to describe/construct declaratively.

Different DI frameworks have different benefits and trade-offs - use whichever one is most suitable for your application.

like image 184
Jon Skeet Avatar answered Sep 27 '22 20:09

Jon Skeet