-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jonathan,

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

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAklufdIACgkQ9CaO5/Lv0PDV1ACfQMCD60vazQTz5xhJeRd8mCbh
rdIAnigXjKQ+z7XDFWC6jLdBUI79wzPh
=Vipb
-----END PGP SIGNATURE-----

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

Reply via email to