tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Brett Delle Grazie <>
Subject Re: custom class as resource
Date Mon, 31 Jan 2011 01:32:59 GMT

On 31 January 2011 00:37, alexis <> wrote:
> Hello, if i need to share a custom class (that instantiate other clases) within my webapp,
where multiple threads need to access this class to read and write (via synchronized methods),
how can achieve this?

It sounds like your class is representative of a factory pattern,
albeit with some synchronisation. Is this correct?

If so, you could:
(a) Use a ServletContextListener to instantiate the class. It will
need to be defined in your web.xml appropriately.
This is an appropriate place to 'shutdown' the factory as well.
(b) Use one of the many frameworks that instantiate stuff for you.
e.g. Spring Framework.  Spring will do this
in a webapp, standalone or testing context with no change to your
actual class.  To the best of my
knowledge, most inversion-of-control frameworks (like Spring) use the
ServletContextListener approach for
instantiation in a web application.

If you go for option (b) you should consider whether simply to use the
framework only and drop your factory
class (if that's possible).

> thanks

Best Regards,


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message