Thank you all for your help. This is why I need to do this:

I have an immutable class called ProjectType, and it has a few predefined
instances held in public static final fields (ProjectType.GD,
ProjectType.TF, etc.) These instances need to be initialized with data from
the database, so my approach was to do that inside the static initializer.
However, I need to access the service that provides the database operations,
and the service is registered in the IoC container.

If anyone has a better idea, I will be glad to learn. I believe there are
rare occasions when the registry is needed outside of normal injection, so
having a convenient way to get to it in Tapestry would be nice, although it
may open up opportunities for abuse.

Benny Law

On Wed, Oct 7, 2009 at 4:58 AM, Ben Gidley <b...@gidley.co.uk> wrote:

> You can get it from the servlet context - it adds it to a context variable.
>  Registry registry = (Registry)
> getServletContext().getAttribute(TapestryFilter.REGISTRY_CONTEXT_NAME);
>
> This shouldn't really be used inside another service as it is a bit
> confusing. You may also have to make sure it doesn't get called until
> tapestry-ioc has initialised via the servlet filter otherwise the attribute
> won't be there yet.
>
> Ben Gidley
>
> www.gidley.co.uk
> b...@gidley.co.uk
>
>
> On Wed, Oct 7, 2009 at 9:37 AM, Alfie Kirkpatrick <
> alfie.kirkpatr...@ioko.com> wrote:
>
> > You can inject ObjectLocator into a service but it doesn't sound like
> > this would work for you as it's still essentially injection in the
> > normal way.
> >
> > For webapps TapestryFilter doesn't put the registry in a static anywhere
> > so there is no way to get the registry from 'outside' the dependency
> > injection framework AFAIK. You could of course copy TapestryFilter,
> > write your own, and put it into a static/threadlocal.
> >
> > Maybe you can explain why you have a static initialiser that needs the
> > registry? It sounds very 'un-tapestry' ;-)
> >
> > Alfie.
> >
> > -----Original Message-----
> > From: Benny Law [mailto:benny.mk....@gmail.com]
> > Sent: 07 October 2009 01:34
> > To: Tapestry Users
> > Subject: Accessing the T5 IoC Registry
> >
> > Hello,
> >
> > How can I obtain a service from the IoC registry inside a static
> > initializer? Injection doesn't seem to work with static members, so I
> > need
> > to access the registry directly, or is there a better way?
> >
> > Thanks,
> >
> > Benny Law
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
> > For additional commands, e-mail: users-h...@tapestry.apache.org
> >
> >
>

Reply via email to