I added a new issue (issue 460 @ http://code.google.com/p/google-guice/issues/detail?id=460 ) that has a patch to allow something like this with the API of:
Module newModule = Modules.rewrite(new MyModule()) .withScope(new NoScopeMatcher(), Scopes.SINGLETON) .build(); Sam On Fri, Jan 8, 2010 at 3:43 PM, Sam Berlin <[email protected]> wrote: > I was experimenting last night with the SPI because I took the email as a > challenge to see if I could make a singletonize(Module... modules) method > that would return a Module where all the unscoped elements were converted to > singletons. The main barrier I ran into was that there appears to be no way > to modify an element after its created, and no good way to get the building > blocks of a specific element in order to recreate it in a different way. If > there was one thing that could change to make this possible it would be to > somehow expose something similar to the 'withScoping' method in > BindingImpl. The best way I can think to do this is to have an SPI class > Rescoper that is similar to AbstractBindingBuilder, but much more limited. > In a nutshell, it would look like: > > com.google.inject.spi.Rescoper > Binding rescope(Binding, Scope) { delegate to > com.google.inject.internal.BindingRescoper.rescope } > > com.google.inject.internal.BindingRescoper > Binding rescope(Binding, Scope) { return > binding.withScoping(Scoping.forInstance(Scope)); } > > If folks think that idea of being able to modify an element after it's > created more generally useful, this could be fleshed out more to be more of > a BindingEditor. > > Sam > > On Thu, Jan 7, 2010 at 10:42 PM, Eelco Hillenius <[email protected]> > wrote: >> >> > that. So it is very strange for me that Guice's default scope is >> > "new". >> >> Imho this is a good default because it is the safest one. Being >> explicit about whether something is a singleton forces developers to >> think about the consequences. It may also be more efficient for >> objects that are cheap to construct. >> >> If you want everything to be bound as a singleton but without much >> pre-registration, how about using classpath scanning? There's a nice >> utility class in sitebricks >> >> (http://www.google.com/codesearch/p?hl=en#iHNmFuHrx4M/trunk/sitebricks/src/main/java/com/google/sitebricks/Classes.java&q=classes%20package:http://google-sitebricks%5C.googlecode%5C.com&sa=N&cd=1&ct=rc) >> that makes this easy to do, and if your services follow a naming >> pattern or implement a certain interface you could scan on that and >> bind everything you find as a singleton. Quite possibly this is >> horrible to Guice's developers, but it works and is easy to implement. >> >> Eelco >> >> -- >> 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.
