This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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!
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.