从无状态bean创建有状态会话bean [英] Create a stateful session bean from a stateless bean
问题描述
上下文如下:
客户端应用程序使用无状态会话bean来登录EJB服务器应用程序。如果登录成功,则客户端应获取一个有状态会话Bean,以便对其个人数据执行某些事务。但是,我希望login方法返回此有状态会话Bean的新实例,以使客户端不应该能够通过身份验证手动调用此会话Bean并执行事务。可能吗 ?
An client application uses a stateless session bean in order to login on an EJB server application. If the login is successful, the client should get a stateful session bean in order to perform some transactions on his personal data. However, I would want that the login method returns a new instance of this stateful session bean such that a client should not be able to manually call this session bean and perform transactions without being authenticated. Is it possible ?
在我的无状态bean中,我有以下代码:
In my stateless bean I have the following code :
@Resource
private SessionContext context;
...
public StatefulBeanRemote login(username, password) {
if (ok) {
StatefulBeanRemote bean = (StatefulBeanRemote) context.lookup("StatefulBeanRemote");
return bean;
}
查找总是失败。我不知道我在做什么错...
The lookup always fail. I don't know what I am doing wrong...
推荐答案
您正在执行的查找与以下内容相同:
The lookup you're performing is the same as:
new InitialContext().lookup("java:comp/env/StatefulBeanRemote");
您是否已定义对StatefulBeanRemote的EJB引用?也许这就是您所需要的:
Have you defined an EJB reference to StatefulBeanRemote? Perhaps that is all you need:
@EJB(name="StatefulBeanRemote", beanInterface=StatefulBeanRemote.class)
public class MyClass {
@Resource
private SessionContext context;
...
}
这篇关于从无状态bean创建有状态会话bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!