views:

40

answers:

2

I'm working on a large legacy application using stateless session beans that has recently been migrated from EJB2 to EJB3, and I'd like to use dependency injection. Unfortunately, in a (IMO misguided) attempt to achieve decoupling, all actual business logic lies in "manager" classes to which the session beans forward their calls. Those manager classes then often use other EJBs.

Can I somehow make these manager classes capable of being injected into the EJBs via @Resource and then having the other EJBs injected into them via @EJB?

The application has to run on glassfish 2.1.

+1  A: 

(...) all actual business logic lies in "manager" classes to which the session beans forward their calls.

That was a very common pattern with EJB 2.x allowing to unit test the "manager" classes easily, outside the container, without any adherence to the EJB API.

Can I somehow make these manager classes capable of being injected into the EJBs via @Resource and then having the other EJBs injected into them via @EJB?

Not out-of-the-box with Java EE 5. Injection is limited only to first class constructs defined in the Java EE platform, including:

  • SessionContext object
  • DataSources object
  • UserTransaction
  • EntityManager interface
  • TimerService interface
  • Other enterprise beans
  • Web services
  • Message queues and topics
  • Connection factories for resource adaptes
  • Environment entries limited to String, Character, Byte, Short, Integer, Long, Boolean, Double, and Float.

In Java EE 6, this would be possible using CDI (JSR-199) and the @Inject annotation in EJBs to inject your managers and also in you managers to get EJBs injected.

Maybe you could try to deploy Weld (the RI of JSR-199) as part of your application on GlassFish v2.1. I didn't experiment this myself, so I can't confirm anything. Just in case, maybe have a look at the Chapter 18. Application servers and environments supported by Weld (GlassFish v2.1 hasn't been tested, but it doesn't mean it doesn't work).

Pascal Thivent
Ugh, that's what I feared. Will look into Weld, but this being a corporate environment (which is the reason for Glassfish 2), it's probably not allowed.
Michael Borgwardt
At least you HAVE glassfish so upgrading is at least _possible_ :) Weld can run in a servlet 2.5 environment by the way.
Thorbjørn Ravn Andersen
A: 

Pascal's suggestion about upgrading to GlassFish 3 sounds probably like the most elegant approach ;) I'd be curious to hear what prevents moving to a more recent version (not saying there can't be a reason, just wondering what the issue is here).

Alexis MP
Large corporations can be extremely conservative about software changes, including upgrades. It's not uncommon to have (as in my case) a list of "approved" software that you're allowed to use, and getting on that list requires a series of tests and trials that can easily take a year or two (90% of that time being pure beaurocratic overhead). And of course, it only happens when someone has the stamina and the budget to see it through.
Michael Borgwardt
Thanks Michael. I was wondering if the lack of clustering in the current 3.x version is an issue? Also, I've recently seen a number of corporations with dual-appserver strategies with GlassFish being used as a more agile, less constrained solution (so less bureaucracy to validate its use).
Alexis MP