How to invoke method on one @Stateless bean from another @Stateless bean

霸气de小男生 提交于 2019-12-23 12:51:25

问题


I have created stateless session bean in Java. Now I want to invoke a method of another stateless session bean. Some things are missing in my code. Usual way of invoking method does not fit here. Being invoked method at another stateless session bean retrieves data from the Internet.

Likewise, how to invoke a method from @Stateless bean of a simple Java class. I build a REST web service with Java and somehow I can't invoke methods being at simple Java class from @Stateless beans. Cheers


回答1:


Just inject it with @EJB

@Stateless
public class StatelessBean1 {
    @EJB
    private StatelessBean2 bean;
}



回答2:


There's nothing special about invoking methods on a stateless session bean. You use the exact same syntax as with every other kind of bean.

As Bozho indicated, the only thing special about EJBs is that you can't construct an instance using the new operator. You need to inject an instance or alternatively do a JNDI lookup. After that, the normal Java rules apply.

It really shouldn't need to be explained but to be sure, calling a method on a stateless session bean called 'bean':

bean.someMethod(someArgument);


来源:https://stackoverflow.com/questions/4903907/how-to-invoke-method-on-one-stateless-bean-from-another-stateless-bean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!