tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <ch...@christopherschultz.net>
Subject Re: replacement for useBean directive
Date Thu, 15 Jan 2009 00:05:38 GMT
-----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-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message