Using Names.bindProperties with injected property file

1,483 views
Skip to first unread message

Axelle Ziegler

unread,
Mar 12, 2012, 12:55:47 PM3/12/12
to google-guice
Hello,

Is there any way to use an injected Properties object as an argument
to Names.bindProperties ?

Basically our configuration files are injected in a module and we
need to retrieve them in another module to configure our database
provider. However, MyBatis-Guice uses Named properties so it means we
have to create bindings using the content of the Properties object.

Currently we add a provider for each properties as follow :

@Provides
@Named( "JDBC.username" )
String username( @AccountDatabaseProperties final Properties
databaseConfiguration ) {
return databaseConfiguration.getProperty( "JDBC.username" );
}

but that means we have to add one such line for each new property
name. We could of course simply hold a static list of the property
names to avoid code duplication, but it would be much more efficient
to use Names.bindProperties to perform this work, however it seems to
mean we have to load our properties file in the database configuration
module. Does anyone see a possible workaround ? Am I missing
something ?

Thanks in advance,

Simone Tripodi

unread,
Mar 13, 2012, 8:26:22 AM3/13/12
to google...@googlegroups.com
Hi Axelle,

did you read the samples of MyBatis-Guice? I honestly don't understand
why you have to map the configuration in a custom DOM when you could
use directly the bound properties.

Have a look at how a basic sample[1] works and stop feeling pain. Just
use Names.bindProperties and load your JDBC configs. Use a build
profile if you need to include different configs depending on the
environment you are deploying the application.

HTH,
-Simo

[1] http://code.google.com/p/mybatis/source/browse/sub-projects/mybatis-guice/trunk/src/test/java/org/mybatis/guice/sample/SampleBasicTest.java

http://people.apache.org/~simonetripodi/
http://simonetripodi.livejournal.com/
http://twitter.com/simonetripodi
http://www.99soft.org/

> --
> You received this message because you are subscribed to the Google Groups "google-guice" group.
> To post to this group, send email to google...@googlegroups.com.
> To unsubscribe from this group, send email to google-guice...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.
>

Axelle Ziegler

unread,
Mar 13, 2012, 8:39:31 AM3/13/12
to google...@googlegroups.com
Yes I did read the samples, and we have a working setup using it,
it just forces us to adopt a specific build and implementation pattern
for our database configuration files.
We inject the configuration files as custom DOM in all of our app
because various configuration files are used across different parts of
our application. So we could always switch back to the default mode,
but I'd rather explore other options first :)

Thanks a lot,

Axelle

Simone Tripodi

unread,
Mar 13, 2012, 6:05:24 PM3/13/12
to google...@googlegroups.com
Hi Axelle,

you can still use both approaches, they are not mutable exclusive: if
you bind configurations via Names.bindProperties and annotate your DOM
with @Named, then request inject the DOM object were needed AND build
the JDBC connection using the MyBatis integration module.

HTH, all the best,
-Simo

Axelle Ziegler

unread,
Mar 13, 2012, 6:08:21 PM3/13/12
to google-guice
Yep, I think It'll end up this way. I tried to force another way
because our dom is currently bound in a different module (and our app
consists of several separate components, so this sorts of matter), but
I can always reload a specific config file in another location.

Thanks a lot for your help

--
Axelle

On Tue, Mar 13, 2012 at 11:05 PM, Simone Tripodi

Simone Tripodi

unread,
Mar 14, 2012, 6:06:37 AM3/14/12
to google...@googlegroups.com
My pleasure, you are welcome Axelle ;)

if you need to work with variables interpolations inside the
configurations, in order to simplify the different environment
profiling, take in consideration the rocoto[1] extension.

HTH, best,
-Simo

[1] http://99soft.github.com/rocoto/

On Tue, Mar 13, 2012 at 11:08 PM, Axelle Ziegler

Reply all
Reply to author
Forward
0 new messages