The easy solution is to bind your SingletonManager to the instance that your legacy code returns:
bind(SingletonManager.class) .toInstance(SingletonManager.getInstance()); then the inejcted instance will be the same as the one your legacy code uses. Fred On Fri, Jun 17, 2011 at 2:51 PM, Marty < [email protected]> wrote: > Hi Google guice users. > > I have a rather large Legacy Code which previously was very > dependendent on a Singleton design pattern. Basically everywhere I > was dependant on SingletonManager, which gave me access to global > state, etc, while making it impossible to test parts of the code > independantly. > > I recently introduced Google Guice to the code and have been > attempting to seperate dependancies in the code, with some success :) > > One thing that shocked me today, my code starts and seems to work , by > asking the Injector to provide a Frame, I get one SingletonManager > from the @Inject annotations in my Frame and another when I previously > call the same injector and request a SingletonManager > > ///from my Module: > > bind(SingletonManager.class).to(SingletonManagerImpl.class).in(Scopes.SINGLETON); > > ///my main method: > SingletonManager mgr = > SingletonManagerFactory.getInjector().getInstance(MainFrame.class); > //do some legacy stuff > > //now initialise the GUI (and the main Object tree). > MainFrame mainFrame; > if (CsConstants.USE_GOOGLE_GUICE_INJECTION == true) { > mainFrame = > SingletonManagerFactory.getInjector().getInstance(MainFrame.class); > } else { > mainFrame = new MainFrameImpl(); > } > //use the frame (pack, etc).... > > ///a class annoted with @Inject > public class AClass implements XXX { > private SingletonManager mgr; > > @Inject > public AClass(SingletonManager mgr) { > this.mgr = mgr; > } > } > > ///code when I need an Instance of the SingletonManager class for > Legacy code > public static SingletonManager getInstance() > { > if(instance==null) > { > //returns aa DIFFERENT instance of this > instance = > getInjector().getInstance(SingletonManager.class); > } > return instance; > } > > > Now when the MainFrame is created Google Guice goes through the code > and creates all my classes. It works great:) > > Now the problem is, because my code base is rather large, I don't have > time to change all the code to injection. In some places this will be > very difficult e.g. classes initialized by the Netbeans GUI designer > (not a good choice for Dependency Injection). So in these places where > due to time or difficulty I sometimes use my old Singleton class. Now > here is my problem, I recently discovered that I get 2 instances of a > class which is scoped as Singleton. > > 1. In the initial object tree creation. > 2. When I call SingletonManagerFactory.getInstance() > > Am I stupid? is this a bug? what should I do differently? Do you need > a complete test case? > > I swear, due to my Singleton I only have 1 injector! > > 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. > > -- 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.
