I probably wasn't clear enough.

By assisted injection, I was talking about something that looks like this:
==========
public interface MyClassFactory{
   public MyClass create(String myString);
}

public class MyClass{
   private String myString;
   private MyObject superThing;
   @Inject
   public MyClass(@Assisted String myString, MyObject injectedThing){
     myString = myString;
     superThing = injectedThing;    
}

===========
When you call MyClassFactory.create("Something");

Guice provides the variable injectedThing to the constructor.

By not using assisted injection, I'm talking about this
==========
public interface MyClassFactory{
   public MyClass create(String myString);
}

public class MyClass{
   private String myString;
   @Inject
   MyObject superThing;

   @Inject
   public MyClass(String myString){
     myString = myString;
}
==========
It's still assisted in a sense, but the field superThing isn't assigned via 
the constructor and I don't have to annotate myString with @Assisted to 
indicate that it's provided by the calling code.

I still create the object with MyClassFactory.create("Something");

And the end result still works the same.  It's really just a different way 
of going about it.  I don't know if it works for everyone in every 
situation, but field injection the way I did in the second example is what 
I see more commonly in the code I'm working with.  The @Inject notation on 
the constructor was something I missed entirely because it existed in base 
classes rather than the classes I was examining, and how the objects were 
being created was buried deep enough that I had a difficult time finding 
the code and figuring out how to do it myself.

My use of the term "Assisted Injection" was really centered around the fact 
that the first method is what shows up in the Guice docs and in a few 
websites that I found.


On Thursday, April 11, 2013 12:44:03 PM UTC-7, Cédric Beust ♔ wrote:
>
> On Thu, Apr 11, 2013 at 11:43 AM, Newbie McBozo 
> <[email protected]<javascript:>
> > wrote:
>
>>
>> I had 6 parameters required on instantiation, and one necessary injected 
>> variable that did not need to be private.  Rather than annotate 6 variables 
>> and have Guice add the 7th, I chose to simply inject the 7th as a member 
>> variable directly rather than in the constructor.
>>
>
> If you're not using assisted injection, then it looks like you must be 
> instantiating your object by calling new yourself, which means your field 
> will not get injected (unless you inject it manually after creation, which 
> you should avoid).
>
> Are you sure what you are describing works?
>
> -- 
> Cédric
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to