It's (mostly) a matter of personal preference. I personally also like 
Constructor Injection better because it clearly establishes the dependencies 
required for the class in a single location. Also, if you use constructor 
injection, you can declare your fields final, whereas you can't do that for the 
field injection.

Robert

On Nov 8, 2011, at 11/83:36 PM , Tim Fletcher wrote:

> Thanks for your response Thiago!
> 
> Again following the instructions at
> http://wiki.apache.org/tapestry/Tapestry5HowToIocOnly
> 
> public class Hello {
> 
> public final Output o;
> public Hello(Output o) {
> this.o = o;
> System.out.println(o);
> }
> }
> 
> I would like to be able to replace it with
> public class Hello {
> 
> @Inject
>        public final Output o;
> public Hello() {
> System.out.println(o);
> }
> }
> 
> However, if i do that currently, i get a Null back. Is there a way to make
> the @Inject annotation work in this way?
> 
> Also, could you expand a little on why Constructor Injection is better, or
> point me to a resource?
> 
> Again, many thanks,
> Tim
> 
> On 8 November 2011 20:37, Thiago H. de Paula Figueiredo
> <thiag...@gmail.com>wrote:
> 
>> On Tue, 08 Nov 2011 18:20:40 -0200, Tim Fletcher <zigomu...@gmail.com>
>> wrote:
>> 
>> Hi All,
>>> 
>> 
>> Hi!
>> 
>> I have followed the instructions at [wiki
>>> Tapestry5HowToIocOnly]<http://**wiki.apache.org/tapestry/**
>>> Tapestry5HowToIocOnly<http://wiki.apache.org/tapestry/Tapestry5HowToIocOnly>
>>>> 
>>> 
>>> and got that working. However, is it possible to use annotations instead
>>> of passing the instance in the constructor?
>>> 
>> 
>> Tapestry-IoC service dependency injection is the same in and out of
>> Tapestry-core (the web framework), so the answer is yes, T-IoC already does
>> what you want.
>> 
>> Advice: constructor injection, IMHO, is the best. :)
>> 
>> --
>> Thiago H. de Paula Figueiredo
>> Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,
>> and instructor
>> Owner, Ars Machina Tecnologia da Informação Ltda.
>> http://www.arsmachina.com.br
>> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org

Reply via email to