-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Instantiate Guice bound instances on-demand #17
Comments
Thanks for the idea. I see where you're heading, but your current solution does not let me specify the locale at instanciation time. I guess, you could enhance it to use assisted-injection and create a generic factory, with |
@spootsy I don't agree that such code should be included in c10n core. We use quite different code:
And init it on the context listener of our web app.
All of this could be sample in docs, but as I think - not in a core code. |
Just to clarify, what @Spikhalsky is pointing out. Whenever you create a proxy with Locale.setDefault(Locale.ENGLISH);
Messages msg = C10N.get(Message.class);
System.out.println(msg.greeting());
Locale.setDefault(Locale.RUSSIAN);
System.out.println(msg.greeting()); and you will see greetings in two different languages. However, this is not the case if you use The way I see @spootsy suggestion useful is if it allows us to inject a factory like: interface C10NFactory<T>{
T get(Locale);
} So if you want to dynamically change locale in your app (not necessarily using a Servlets API, of course), you would just inject the factory and get the proxy for the locale you want. |
@rodionmoiseev Ah, sorry, I didn't realise this was the case. The issue I was having with the original C10N Guice module (and the reason I didn't notice this "unbound proxy" functionality) is that I was initialising the injector prior to C10N. This meant the proxies were created with an unconfigured C10N, and so they always returned default values, no matter what I returned from the locale provider. Just another case of not RTFM properly :) You may well already have a good reason for not doing so, but perhaps C10N could throw an exception if you request a proxy instance before calling "C10N.configure()"? |
No probs. I'll see if I can make it more clear in the manual. Actually, I have had several reports where calling |
Hi,
First, let me say this is a really nice library - very easy to pick up and use :)
One thing I think could be improved though, is to provide an option of instantiating C10N interfaces on-demand when retrieved through Guice. At the moment, they're all created during injector initialisation so you can't, at runtime, change the locale of the instances provided through Guice.
Here's a rough idea of the change I'm proposing to the existing C10NModule (don't have git handy so not submitted as a patch, sorry)
This way, we can change our locale at will, and it also means we can delay configuring C10N until right before we're ready to use it.
The text was updated successfully, but these errors were encountered: