Re: [ANN] WOInject 1.0
Re: [ANN] WOInject 1.0
- Subject: Re: [ANN] WOInject 1.0
- From: Henrique Prange <email@hidden>
- Date: Fri, 13 Apr 2012 15:20:56 -0300
Hi Ron,
The Application class is also injected (except constructor injection). You can use the @Inject annotation:
public class Application extends InjectableApplication {
public static void main(String[] argv) {
WOInject.init("com.legalsounds.Application", argv);
}
@Inject @Facebook Socialable socialPerson;
public Application() {
super();
//The socialPerson field is already injected here
socialPerson.anyMethod();
}
}
You should avoid referencing the injector() method directly in your code. That is the purpose of WOInject: to abstract the injection mechanism.
There are more comments. See below... :)
On 13/04/2012, at 04:55, Ron X wrote:
> hi
>
> public class Application extends InjectableApplication {
> private static Logger log = Logger.getLogger(Application.class);
>
> public static void main(String[] argv) {
> WOInject.init("com.legalsounds.Application", argv);
> }
>
> @Facebook Socialable socialPerson;
>
> public Application() {
>
> Injector injector = injector();
> socialPerson = injector.getInstance(Socialable.class);
> ..............fails at this line............
>
If you still want to use the injector (not recommended), you have to ask for an instance based on the configuration of your bindings. You have configured the Sociable class to be instantiated as a FacebookPerson when annotated with the Facebook annotation. You have to ask for this instance like this:
injector().getInstance(Key.get(Sociable.class, Facebook.class));
This call will return an instance of FacebookPerson class.
Again, the @Inject annotation give you a simple and clear solution, and you don't have to reference the injector() directly. It just works.
> @Override
> protected Module[] modules() {
> return new Module[] { new SocialModule() };
> }
> }
>
> public class SocialModule extends AbstractModule {
>
> @Override
> protected void configure() {
>
> bind(Socialable.class).annotatedWith(Facebook.class).to(FacebookPerson.class).in(WOScopes.SESSION);
>
This binding declaration is correct. You should preferably use the WOSessionScoped.class instead of WOScopes.SESSION (WOScopes visibility will probably be made private in the next version).
bind(Socialable.class).annotatedWith(Facebook.class).to(FacebookPerson.class).in(WOSessionScoped.class);
This means Guice will create one FacebookPerson instance per WOSession. The problem is you have injected the Socialable in the Application class. The Application is kind of a Singleton which means there is only one Application instance per application. Injecting an object of a narrower scope in the Application class is usually an error. If you keep this binding configuration and inject the Socialable in the Application class, Guice should throw an OutOfScopeException. Take a look at the Scopes section [1] of the Guice's User Guide for more information.
[1]http://code.google.com/p/google-guice/wiki/Scopes
Cheers,
Henrique
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden