You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Tyler Wilcock <tw...@widen.com> on 2017/10/05 19:24:54 UTC

IoC Question

Question about Tapestry's IoC system.  Let's pretend I register a service
called EventTracker.java.  EventTracker has a private List field containing
a list of Events, a method to add those events, and a method to retrieve
those events.  Now let's say I have another class that @Inject's that
EventTracker service and records a bunch of events.

If I @Inject the EventTracker service into yet another class, can I read
those events that the previous class added?  Or will it be an empty slate?
Generally, my question is this: do services save their state in between
injections in different classes?

Re: IoC Question

Posted by Dmitry Gusev <dm...@gmail.com>.
Hi,

By default services are singletons, which means all injections reference
the same object, so all your events will be available at every injection.

You can read about defining service scopes here:
http://tapestry.apache.org/defining-tapestry-ioc-services.html

On Thursday, October 5, 2017, Tyler Wilcock <tw...@widen.com> wrote:

> Question about Tapestry's IoC system.  Let's pretend I register a service
> called EventTracker.java.  EventTracker has a private List field containing
> a list of Events, a method to add those events, and a method to retrieve
> those events.  Now let's say I have another class that @Inject's that
> EventTracker service and records a bunch of events.
>
> If I @Inject the EventTracker service into yet another class, can I read
> those events that the previous class added?  Or will it be an empty slate?
> Generally, my question is this: do services save their state in between
> injections in different classes?
>


-- 
Dmitry Gusev

AnjLab Team
http://anjlab.com

Re: IoC Question

Posted by Cezary Biernacki <ce...@gmail.com>.
The default behaviour of Tapestry-IOC is that a service is a singleton. In
other words only a single instance is created and every time the same
instance is injected. So in your case, different classes will share the
same EventTracker service instance, and they will be able to see the same
events. One important consideration here is that the same instance is also
shared between threads, so in a multi-threading program (which include any
web applications) appropriate precautions must be done to prevent race
conditions. You can change default scope of the service to be per-thread,
(see @Scope annotation), in this case each thread will get a separate
instance of the service.


On Thu, Oct 5, 2017 at 9:24 PM, Tyler Wilcock <tw...@widen.com> wrote:

> Question about Tapestry's IoC system.  Let's pretend I register a service
> called EventTracker.java.  EventTracker has a private List field containing
> a list of Events, a method to add those events, and a method to retrieve
> those events.  Now let's say I have another class that @Inject's that
> EventTracker service and records a bunch of events.
>
> If I @Inject the EventTracker service into yet another class, can I read
> those events that the previous class added?  Or will it be an empty slate?
> Generally, my question is this: do services save their state in between
> injections in different classes?
>