Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guice and general application configuration

Tags:

For a monitoring software written in Java I consider using Google Guice as DI provider. The project needs to load its configuration from an external resource (file or database). The application is designed to run in standalone mode or in a servlet container.

At the moment the configuration does not contain bindings or parameters for dependency injection, only some global application settings (JDBC connection definitions and associated database management/monitoring objects).

I see two options:

  • to use another library, for example Apache Commons Configuration, which supports file and JDBC configuration sources (and many other)

or

  • to use a file based addon for Guice like guice-xml-config to store the application options (this would allow to configure the DI part later if it becomes necessary).

Would you recommend to use Guice for both tasks, or keep the general application configuration separate from the dependency injection? Which advantages and disadvantages would you consider the most important ones?

like image 900
mjn Avatar asked Jan 26 '11 14:01

mjn


People also ask

What is Guice used for?

Google Guice (pronounced like "juice") is an open-source software framework for the Java platform released by Google under the Apache License. It provides support for dependency injection using annotations to configure Java objects.

Which is better Guice or Spring?

Spring allows you to omit the @Autowired annotation when there's only one constructor. Guice allows binding to a Provider, as well as injecting a Provider of your class, even when your class has no Provider binding.

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 are modules in Guice?

The Module is the core building block for an Injector which is Guice's object-graph builder. First step is to create an injector and then we can use the injector to get the objects. public static void main(String[] args) { /* * Guice.createInjector() takes Modules, and returns a new Injector * instance.


1 Answers

It's straightforward to slurp a property file in a Guice module:

public class MyModule extends AbstractModule {    @Override   protected void configure() {     try {         Properties properties = new Properties();         properties.load(new FileReader("my.properties"));         Names.bindProperties(binder(), properties);     } catch (IOException ex) {         //...     }   } }  

Later it's easy to switch from Properties to other config sources.

[Edit]

BTW, you can get the injected properties by annotating it with @Named("myKey").

like image 174
Landei Avatar answered Sep 19 '22 18:09

Landei