aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Two session beans, one class, much confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Two session beans, one class, much confusion" Watch "Two session beans, one class, much confusion" New topic
Author

Two session beans, one class, much confusion

Dan Bonelli
Greenhorn

Joined: Jun 29, 2010
Posts: 2
I'm a bit confused as to how env-entry values are scoped when you specify multiple session beans using the same class. Using this kind fellow's work as a starting point, I have a simple session bean defined thus:



Simple POJO, no annotations except the resource. To make sure my environment wasn't on acid (which, incidentally, consists of Sun's JVM and Glassfish V3.0.1 full), I configured this bean with the following ejb-jar.xml:



When I test the class and call SimpleBean.getSomeValue(), it predictably returns "This is the value for SimpleBean", so the injection is clearly working. However, when I wire up two beans like so:



'This is the value for AnotherBean' injected is into both OneBean and AnotherBean. I am looking up my EJB's this way:



This begs the question: can you even configure two session beans using the same class, but with different names and configuration values? I'm sure the answer is staring me in the face, but four hours of Googling has not yielded a solution. Thanks in advance for your help!

Thanks,
Dan





Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10102
    
165

Dan Bonelli wrote:
However, when I wire up two beans like so:



'This is the value for AnotherBean' injected is into both OneBean and AnotherBean.

Hmm, looks like a bug to me.

Dan Bonelli wrote:

This begs the question: can you even configure two session beans using the same class, but with different names and configuration values?

Yes, you can.

[My Blog] [JavaRanch Journal]
Dan Bonelli
Greenhorn

Joined: Jun 29, 2010
Posts: 2
Thanks for the reply, Jaikiran.

If I do a lookup from the IntialContext, like so:

I get the same result. When specifying multiple session beans mapping to one class like I'm doing, it seems like Glassfish overwrites someValue with the last one specified in ejb-jar. I need to do some digging in Glassfish's source code to better understand how they go about doing resource injection.

Is there another way aside from ejb-jar.xml and <env-entry> to give a configuration parameter to an EJB? The whole reason I'm even mucking about in ejb-jar.xml like this is because the EJB spec demands that EJBs have a no-argument constructor. Ideally what I'd like to do is define an annotation like so:
which is applied at the injection point, and somehow be able to get at the annotation from inside SimpleBean.
I could make that work with a little introspection if there was some way from within the SimpleBean EJB to see what class and field it had been injected into. From what I can see, that information isn't available at run-time to an EJB.

More generally, what is the standard way of setting per-instance configuration of an EJB once it's been injected? I've resorted to initializing someValue from a method like so:

This works and achieves the desired result, but it seems somewhat odd to me that the designers of the EJB spec gave us a container-managed way to inject beans via annotations, but no simple way to initialize them.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Two session beans, one class, much confusion