Fred Faber wrote:


2010/1/20 Willi Schönborn <[email protected] <mailto:[email protected]>>

    Fred Faber wrote:



        2010/1/20 Willi Schönborn <[email protected]
        <mailto:[email protected]>
        <mailto:[email protected]
        <mailto:[email protected]>>>


           Thanks for the reply, i left a comment below.


           Fred Faber wrote:

Yeah in general this is not entirely an easy problem. Without knowing more about your problem specifics, here are
               some solutions:

               First, you can always use what you have, with
               Modules.override() to override the annotation binding:

               class RealDbModule extends AbstractModule {
                  @Override
                   protected void configure() {
                      bind(...the db provider you show in your mail);
                   }
               }

               Module myTestDbModule = Modules.override(myRealDbModule)
                 .with(new AbstractModule() {
                        @Override
                         protected void configure() {
                             bind(Key.get(String.class,
               Names.named("database.mysql.username")).toInstance("test");
                          }
                    }

               and you can install this in your private module, or do what
               you will with it.


That what i was thinking, but is there a nice way to retrieve
           "test" from
           a properties file instead of hardcoding it to the module?


        I imagine so, sure, but that seems like it has solutions that
        are independent of guice and more specific to how you want to
        read the file.  As far as design of the modules, "test"
        doesn't need to be hardcoded and there can be any logic that
        reads the file substituted in its place.

    Reading a properties file on my own is exactly what i want to avoid ;)
    Can i do:
    binder.bind(Key.get(String.class,
    Names.named("database.mysql2.username")).to(Key.get(String.class,
    Names.named("database.mysql.username")); ?


This is the concept of a bridging binding, whereby the biding attached to one Key is going to be bridged (effectively transfered, if you will) to a binding attached to another key.

You can make this general by a simple class:

class BridgingProvider<T> implements Provider<T> {

   private static <T> ScopingBindingBuider bindBridge(
       Binder binder,
       Key<T> keyToBridge,
       Class<? extends Annotation> bridgeAnnotation) {
    Provider<T> bindingProvider = new BridgingProvider(keyToBridge);
Key<T> bridgedKey = Key.get(keyToBridge.getTypeLiteral(), bridgeAnnotation);
    return binder.bind(keyToBridge).toProvider(bindingProvider);
  }

  private static final Key<T> keyToBridge;

@Inject Injector injector;

  private BridgingProvider(Key<T> keyToBridge) {
     this.keyToBridge = keyToBridge;
  }

  @Override
   public T get() {
      T bridgedInstance injector.getInstance(keyToBridge);
      return bridgedInstance;
   }
}


and then:

BridgingProvider.bindBridge(
   binder(),
   Key.get(String.class, Names.named("database.mysql2.username")),
   Names.named("database.mysql.username"));

Which will inject whatever is bound to "Key.get(String.class, Names.named("database.mysql2.username"))" as the value when asked to inject something with the key, "Key.get(String.class, Names.named("database.mysql.username")).

-Fred
That looks great. I will give you feedback as soon as i tried it.


               * * *

               You can add a crufty solution, such as passing in a
               constructor arg to your DbModule:

               class DbModule extends AbstractModule {
                    private final DbModule(String annotationName) {
                      this.annotationName = annotationName;
                   }

                  @Override
                   protected void configure() {
                      bind(Database.class).toProvider(new
        Provider<Database>() {
                           @Inject Injector injector;
                                 @Override
                           public Database get() {
                             Named named = Names.named(annotationName);
                             Key<String> annotationKey =
               Key.get(String.class, named);
                             String dbUser =
        injector.getInstance(annotaitonKey);
                              return new MySqlDatabase(......);
                   }
               }

               I almost didn't want to suggest this, and I'll justify
        it by
               saying that I do it simply to illustrate why it's not a
        great
               solution:
               1) because who knows through how many other classes you'll
               need to propagate the constructor arg
               2) because Guice isn't creating your MySqlDatabase instance

               * * *

               There are other methods along these lines as well.
         With more
               details I can probably suggest something more
        appropriate for
               your case.


               2010/1/19 Willi Schönborn <[email protected]
        <mailto:[email protected]>
               <mailto:[email protected]
        <mailto:[email protected]>>
               <mailto:[email protected]
        <mailto:[email protected]>
               <mailto:[email protected]
        <mailto:[email protected]>>>>


                  On 17/01/2010 12:14, Willi Schönborn wrote:

                      On 17/01/2010 00:18, Dhanji R. Prasanna wrote:



                          2010/1/17 Willi Schönborn
                       <[email protected]
        <mailto:[email protected]>
                       <mailto:[email protected]
        <mailto:[email protected]>>
                          <mailto:[email protected]
        <mailto:[email protected]>
                       <mailto:[email protected]
        <mailto:[email protected]>>>>


                              On 16/01/2010 18:02, Kartik Kumar wrote:

                                  This may help.
http://code.google.com/p/google-guice/wiki/BindingAnnotations

                              No, i dont want to bind multiple
                       Implementations to the same
                              interface, i want to configure (!)
                              multiple instances of the same class
                       independently using
                              annotations (@Named).


                          You're referring to the robot legs problem:
http://code.google.com/p/google-guice/wiki/FrequentlyAskedQuestions

                          (look for "robot legs")


                  An idea? Anyone?

                      I managed to solve a part of the problem using
        private
                   modules,
                      but there is something left i dont know
                      how to do:

                      Better example:
                          public interface Database
                      and
                          class MySqlDatabase implements Database {

                              @Named("database.mysql.username")
                              private String username;

                          }

                      Lets assume i actually want to work with two
        different
                   mysql
                      databases and
                       i have a settings.properties which is bound using
                      Names.bindProperties() and contains:

                      database.mysql.username = test
                      database.mysql.second.username = user

                      I need a way to "override" a constant bound by
                      Names.bindProperties(...)
                      in a private module by using the value of another
                   constant binding.
                      E.g. I want to set the value of
                   "database.mysql.username" to the
                      value of
                      "database.mysql.second.username" in my private
        module
                   without
                      the need to parse the properties file on my own.


                          Dhanji.



                          --    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]
        <mailto:[email protected]>
                       <mailto:[email protected]
        <mailto:[email protected]>>
                          <mailto:[email protected]
        <mailto:[email protected]>
                       <mailto:[email protected]
        <mailto:[email protected]>>>.

                          To unsubscribe from this group, send email to
                          [email protected]
        <mailto:google-guice%[email protected]>
<mailto:google-guice%[email protected]
        <mailto:google-guice%[email protected]>>
<mailto:[email protected]
        <mailto:google-guice%[email protected]>
<mailto:google-guice%[email protected]
        <mailto:google-guice%[email protected]>>>.

                          For more options, visit this group at
http://groups.google.com/group/google-guice?hl=en.




                  --    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]
        <mailto:[email protected]>
               <mailto:[email protected]
        <mailto:[email protected]>>
                  <mailto:[email protected]
        <mailto:[email protected]>
               <mailto:[email protected]
        <mailto:[email protected]>>>.

                  To unsubscribe from this group, send email to
                  [email protected]
        <mailto:google-guice%[email protected]>
               <mailto:google-guice%[email protected]
        <mailto:google-guice%[email protected]>>
                  <mailto:google-guice%[email protected]
        <mailto:google-guice%[email protected]>
               <mailto:google-guice%[email protected]
        <mailto:google-guice%[email protected]>>>.


                  For more options, visit this group at
                  http://groups.google.com/group/google-guice?hl=en.



------------------------------------------------------------------------

               --        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]
        <mailto:[email protected]>
               <mailto:[email protected]
        <mailto:[email protected]>>.
               To unsubscribe from this group, send email to
               [email protected]
        <mailto:google-guice%[email protected]>
               <mailto:google-guice%[email protected]
        <mailto:google-guice%[email protected]>>.
               For more options, visit this group at
               http://groups.google.com/group/google-guice?hl=en.



           --
           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]
        <mailto:[email protected]>
           <mailto:[email protected]
        <mailto:[email protected]>>.
           To unsubscribe from this group, send email to
           [email protected]
        <mailto:google-guice%[email protected]>
           <mailto:google-guice%[email protected]
        <mailto:google-guice%[email protected]>>.
           For more options, visit this group at
           http://groups.google.com/group/google-guice?hl=en.





        ------------------------------------------------------------------------

-- 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]
        <mailto:[email protected]>.
        To unsubscribe from this group, send email to
        [email protected]
        <mailto:google-guice%[email protected]>.
        For more options, visit this group at
        http://groups.google.com/group/google-guice?hl=en.



    --
    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]
    <mailto:[email protected]>.
    To unsubscribe from this group, send email to
    [email protected]
    <mailto:google-guice%[email protected]>.
    For more options, visit this group at
    http://groups.google.com/group/google-guice?hl=en.





------------------------------------------------------------------------

--
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.

-- 
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