Giulius
This package contains a tiny framework which sits on top of Guice and
allows configuration to be done using properties files. It uses
Apache Commons Configuration under the hood, so configuration can
come from wherever the implementation gets it.
Why not use plain Guice
Actually, if you're using this, you are. The only difference is
that it standardizes how configuration is loaded.
What it does
Guice has a way of specifying specific instances of things to inject,
using the @Named annotation, e.g.
public class MyServer {
MyServer (@Named("port") int port, @Named("baseUrl") String url) {
...
}
}
Needless to say, when we get to the level of what port to open and
what base URL to use, we are at the point of things which should not
be hard-coded into Java code.
The good thing about Guice is that wherever it is straightforward to
do something with simple Java code, you can. By being
laser-focused on the dependency injection issue, it leaves open the
question of configuration - which definitely needs to come from
somewhere. This library is one solution to that problem.
You initialize Guice in the usual way, by passing it a bunch of
Guice Modules which bind the interfaces which you code
to to implementations of those interfaces. The main difference is that
you will do it by instantiating an instance of Dependencies
and letting it take care of actual Guice initialization:
Dependencies deps = new Dependencies (new ModuleA(), new ModuleB());
Server server = deps.getInstance(Server.class);
server.start();
Default configuration is done using the ClasspathConfiguration to
create an Apache Commons Configuration object. Values from that are
used to populate Java primitives which Guice should inject that use
the @Named annotation.
Shutdown Hooks
ShutdownHookRegistry allows you to register runnables to be run on
orderly VM shutdown, or whenever Dependencies.shutdown()
is called (for example, when a servlet is unloaded or after a test
is complete). Use this to close connections and files and such.
Scopes
One of the more powerful features of Guice is scopes. A scope is
really a binding for a bunch of ThreadLocals, which Guice can use to
look up and inject objects of particular types.
This library includes base classes for single-entry and reentrant scopes,
and for wrapping ExecutorService thread pools such that the context
something was submitted in is reconstructed before it is run. This
can serve as the basis for additional frameworks on top of Guice, such
as Acteur.