简体   繁体   中英

Can we access/call Stateless Session Bean from Static method?

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?

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.

您绝对可以从实例方法或变量访问静态方法或变量...但是不能从静态方法访问实例方法或变量!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM