Hi guys,

I really appreciate your replies. I took a look at both solutions Tim's 
micro-fwk and magic-config, but unfortunately I couldn't make it work, and 
also I am not sure if what i need. To be honest I think that I am pretty 
lost since I tried different ways to do it without success. Ill try to 
describe what "I think" my problem is and let me know if I am wrong.

The thing is, I need to bind a constant to a String value: 
*bindConstant().annotatedWith(Db4Objects.class).to( 
<value-taken-from-properties> ).*
That value should be passed to the module by the constructor or injected. 
BTW, I am using Names.bindProperties(...) to bind my config.properties 
files. 

So, my persistence module looks like:

public class PersistenceModule extends AbstractModule {

    @Inject @Named("config.db4o.database")
    private String database;
    
    @Override
    protected void configure() {
        
install(PersistenceService.usingDb4o().across(UnitOfWork.REQUEST).buildModule());
        bindConstant().annotatedWith(Db4Objects.class).to(database);
    }
}

The only way I can inject the property "database" into the module using 
@Named (correct me if i am wrong), is asking the Injector for an Instance 
of the module. I create 2 Injectors, the first one loads and bind the 
config properties, and the second injector installs all the modules created 
by the parent Injector.

final Injector parent = Guice.createInjector(new AbstractModule() {
   void configure() {
      Properties properties = new Properties();
      propeties.load(...);
      Names.bindProperties(binder(), properties);
   }
})

final Module module = Modules.combine(
   parent.getInstance(PersistenceModule.class),
   ... my other modules ... 
);

final Injector child = parent.createChildInjector(module);
Server server = child.getInstance(Server.class)
server.start();
server.join();

When I debug the application, it runs perfectly until I try to access to 
the database. When I try to load some objects from DB4O, i am getting: 

*com.google.inject.ProvisionException: Guice provision errors:*
*
*
*1) Error in custom provider, java.lang.IllegalStateException: Must specify 
either database file name: bindConstant().annotatedWith(Db4Objects); or a 
remote server host: 
bindConstant().annotatedWith(Names.named(Db4Objects.HOST)).to("localhost")*
*  at 
com.wideplay.warp.persist.spi.AbstractPersistenceModule.bindWithUnitAnnotation(AbstractPersistenceModule.java:105)
*
*  while locating com.db4o.ObjectContainer*

However I checked and the binding is executed. I think that the problem is 
because I am using two different Injectors?

The only way that is working for me is binding the constant with a *
hardcoded* value in the module and creating the injector in this way:

public class PersistenceModule extends AbstractModule {
    @Override
    protected void configure() {
        
install(PersistenceService.usingDb4o().across(UnitOfWork.REQUEST).buildModule());
        
bindConstant().annotatedWith(Db4Objects.class).to("./target/database.dat");
    }
}

final Injector parent = Guice.createInjector(new AbstractModule() {
   void configure() {
      Properties properties = new Properties();
      propeties.load(...);
      Names.bindProperties(binder(), properties);
   }
}, new PersistenceModule(), ... );

Server server = parent.getInstance(Serecomendationsrver.class)
server.start();
server.join();

Any recommendation?
Thanks!






On Monday, April 22, 2013 9:25:32 AM UTC-5, Marshall Pierce wrote:
>
>
> On Apr 22, 2013, at 3:59 AM, Tim Boudreau <[email protected] <javascript:>> 
> wrote: 
>
> > I wrote a micro-framework for exactly that purpose - binding strings to 
> contents from properties files, on top of another micro-framework for 
> loading them without hard-coding paths (you can have system defaults in 
> /etc/myapp.properties overlaid with ~/myapp.properties overlaid with 
> ./myapp.properties and it takes care of binding them to @Named) - which 
> might be helpful: 
> > 
> > https://github.com/timboudreau/giulius 
> > and the javadoc here: 
> > 
> http://timboudreau.com/builds/job/giulius/lastSuccessfulBuild/artifact/giulius-settings/target/site/apidocs/index.html
>  
> > 
> http://timboudreau.com/builds/job/giulius/lastSuccessfulBuild/artifact/giulius/target/site/apidocs/index.html
>  
> > 
> > If you need values before the injector is created, you'll want to work 
> directly with a Settings object (like a Properties without mutator methods 
> - see the last javascript link), but you could just as easily write a 
> Provider that uses @Named to get the string and initializes it on first 
> use. 
> > 
> > -Tim 
>
> An alternate approach might be to bind your "Settings" object and use a 
> @Provides method in your PersistenceModule, like so: 
>
> public class PersistenceModule extends AbstractModule { 
>   
>     ... 
>
>     @Provides 
>     Db4OStuff getDb4O(Settings settings) { 
>         // use your settings 
>     } 
> } 
>
> Or, you could just define a PersistenceModule constructor that has a 
> Settings parameter if that's the time at which it's most convenient to work 
> with the Settings object. 
>
> If you do wish to refactor your config access code away from your existing 
> Settings class, Tim's Giulius looks simple and lightweight, though I happen 
> to prefer are more strongly typed approach, so I'm currently enjoying using 
> https://github.com/brianm/config-magic. I wrote a tiny wrapper around 
> that to make it easy to use with Guice and any commons-config configuration 
> source: https://github.com/palominolabs/config-inject. 
>
> No shortage of options for configuration handling… :)

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to