tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <>
Subject Re: replacement for useBean directive
Date Thu, 15 Jan 2009 00:05:38 GMT
Hash: SHA1


Jonathan Mast wrote:
> class BeanBag {
> private static AppleBean appleBean = null;
> private static BananaBean bananaBean = null;
> public static AppleBean getAppleBean() {

Might I recommend that you use a "regular" class instead of one with
static members and methods? I think you'll find it more flexible (for
instance, you will be able to extend the class and replace the object in
memory with the extended class, or you could wrap one object in another,
etc., neither of which are possible with static methods).

You could just create a new instance of this class (BeanBag) at app
startup and toss it into the application scope.

> In the Servlet code, I just obtain a bean like this:
> AppleBean appleBean = BeanBag.getAppleBean();

It would be a bit more complicated, but your new code would be:

BeanBag bb = (BeanBag)application.getAttribute("my.package.beanBag");
AppleBean appleBean = bb.getAppleBean();

If you're feeling sassy, you could implement a getAppleBean() method in
a superclass from which all of your servlets extend, and clean up your
code a little bit more. Tres' objectique!

> My question is: where (or if) should I implement the synchronization?

Synchronize the methods of your BeanBag class (whether static or not).

> But wouldn't making the methods in BeanBag synchronized be a better
> approach?

In the example, the bean in question was an application-scoped bean,
which requires synchronization in the generated Java code itself
(because there is a null check for the reference and the possibility of
creating a new object and inserting it into the application scope). Your
solution will have a single object in the application scope (or will be
a static singleton) and therefore the synchronization can be handled
locally (in the class).

Hope that helps,
- -chris

Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla -


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

View raw message