It's a little verbose, but I read the properties in the module
constructor, save them to member fields, then bind them in the
Module#bind method. Then if the setting doesn't exist, the constructor
throws a ConfigurationException.

This has the benefit of making the configuration options quite
explicit and allows you to have another constructor that just takes
all the settings as parameters, useful for testing. It also allows you
to bind more complex configuration than just strings, etc, such as
Queue or QueueConnectionFactory when using JMS.


For example

public class AModule extends AbstractModule {
    private String value;

    public AModule(Properties properties) throws
ConfigurationException {
        value = properties.getProperty("setting");
        if (value == null)
            throw new ConfigurationException("missing setting");
    }

    protected void configure() {
        bindConstant().annotatedWith(Names.named("a")).to(value);
    }
}

On Mar 24, 8:11 pm, Martin <[email protected]> wrote:
> Hi All,
>
> I'm using Guice 2.0 to inject a class with values from a properties
> file using the @Named annotation with Names.bindProperties() as well
> as static injection using binder.requestStaticInjection() to wire
> Guice.
>
> As an example my static class "Properties" with property keys contains
> inject bindings as follows
>
>     private static final String DEFAULT_COMPONENT_VERSION       =
> "2.0";
>
>     @Inject(optional = true)
>     @Named("version")
>     public static String        PROP_COMPONENT_VERSION          =
> DEFAULT_COMPONENT_VERSION;
>
> The problem I am having is with exceptions.  If I don't use
> (optional=true) and my property is not defined inside the properties
> file, Guice will throw a CreationException.  I've been trying to catch
> this exception around binder.requestStaticInject() without success
> since a exception is throw within the Properties file.
>
> I would like to know how i can accomplish the continuation of the
> program flow and simple notify the user on start-up using log("using "
> +DEFAULT_COMPONENT_VERSION) instead of a CreationException that Guice
> throws which halts everything.
>
> With (optional=true) i never actually know if the Guice bindings fail
> or not. If they do then Guice just ignores and continues.
>
> Thanks,
> Martin
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to