You might want to contribute to the registry startup and initialize things there:

http://tapestry.apache.org/tapestry5/tapestry-ioc/startup.html


Benny Law escribió:
Thanks Ben, this looks reasonably clean to me. Unless somebody else offers a
better idea, I will probably go with this approach.

Benny

On Wed, Oct 7, 2009 at 10:08 AM, Ben Dotte <ben.do...@gmail.com> wrote:

Here's a trick we use for this. We extend TapestryFilter and override
init(Registry registry). Since this gets passed the created registry,
we then store that in a static variable on our filter and provide a
static getRegistry() method to retrieve it. This gets around the
problem that TapestryFilter stores the registry in a private instance
variable. To use your filter, just specify it in web.xml instead:

<filter>
   <filter-name>t5</filter-name>
   <filter-class>com.whatever.MyFilter</filter-class>
</filter>

This still requires that the registry has been created at the point
that it is requested, but at least you don't have to have access to
the servlet context.

Ben

On Wed, Oct 7, 2009 at 6:58 AM, Benny Law <benny.mk....@gmail.com> wrote:
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


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





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

Reply via email to