2

I am injecting a Stateless session bean in a class that has many static methods. I would like to access this bean from these Static method. There is no documentation on this one.

So I wondering if its allowed. If yes, any downsides? Recommended / not recommended?

Kevin Rave
  • 13,876
  • 35
  • 109
  • 173

2 Answers2

1

If its allowed? Yes, it can be allowed within container and outside of container.

You can access a EJB from non-EJB. Please check below link.

https://stackoverflow.com/a/9061924/1718893

If the class and EJB are in same project/deployable, then JNDI call should be easy. Few steps mentioned in above link can be skipped.

Recommended in scenario like below -

Depending on scenario, this can be a recommended approach. I came across this situation when I had to implement factory pattern. The EJBs were called only when required and based on conditions. I could have made Factory class itself a EJB. But due to its dependencies on some other decision making components, that was not possible.

Not recommended in scenraio like below -

Depending on how much disciplined development is followed, such approach can be discouraged as well. If all the developers are not much experienced with EJBs, its transaction management and deployment, then down the line, this approach can create dangerous situations.

Community
  • 1
  • 1
Taps
  • 68
  • 6
  • 1
    Thanks. The question is about accessing them from STATIC method. And its not about HOW. Its about "if we can" and "if its a good thing/ recommended" to do and – Kevin Rave Jul 12 '13 at 13:09
  • Hi Kevin, my answer might be not in expected format. I've tried answering question again point by point. – Taps Jul 15 '13 at 07:30
  • Hmmm.. I guess you are missing a point. I am not asking about access from NON-EJB, I am asking about the access from "static method". – Kevin Rave Jul 15 '13 at 15:32
0

You can definitely access static method or variables from an instance method or variable... but can not access an instance method or variable from a static method!

Ali
  • 899
  • 2
  • 13
  • 33