jilen
jilen

Reputation: 5763

Guice multi database auto switch

I have three database cofinguration

  1. local db for unit test environment
  2. test db for development environment
  3. online db for product environment

How Can I automatic inject different db properties according to environment ?

By using xml with spring, I can use maven profiles and Resources filter feature to have this work done.

What 's the solution with guice?

Upvotes: 1

Views: 149

Answers (1)

A.H.
A.H.

Reputation: 66273

In the simplest case you can use binder.bindConstant() within your Module like this:

bindConstant().annotatedWith(Names.named("user.name")).to(System.getProperty("user.name"));

You use this like this:

@Inject @Named("user.name")
private String userName;

How you set the system properties at the start of the program is up to you.

Another but similar way is the utility method cNames.bindProperties(Binder, Map<String, String>) where you can set arbitrary properties in one go.

Upvotes: 1

Related Questions