java 无状态和有状态 bean 区别的 EJB 示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7946589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 22:08:45  来源:igfitidea点击:

EJB example for stateless and stateful beans difference

javajakarta-eeejb

提问by Evgeni Dimitrov

I'm new to EJB, and I'm trying to understand the diference between Stateless and Stateful bean, so I made a simple example to test them.

我是 EJB 新手,我试图了解无状态和有状态 bean 之间的区别,所以我做了一个简单的例子来测试它们。

@Stateless
public class Service {

private int num;

    public Service(){
    }

    public int getNum() {
        return num;
    }

    public void setNum() {
        this.num++;
    }
}


@WebServlet("/Controller1")
public class Controller1 extends HttpServlet {
    @EJB
    private Service serv;

    public Controller1() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        serv.setNum();
        response.getWriter().println(serv.getNum());
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

}

And the Stateful equivalent:

和有状态的等价物:

@Stateful
public class ServiceStateful implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private int num;

    public ServiceStateful(){
    }

    public int getNum() {
        return num;
    }

    public void setNum() {
        this.num++;
    }
}


@WebServlet("/Controller")
public class Controller extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @EJB
    private ServiceStateful serv;

    public Controller() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        serv.setNum();
        response.getWriter().println(serv.getNum());
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

}

Both examples act exactly the same, which is surprising for me. Can someone please explain what is the deal here?

这两个示例的行为完全相同,这让我感到惊讶。有人可以解释一下这里的交易是什么吗?

回答by JB Nizet

Your first example returns the previously set number by chance only : the get method could have been invoked on a different bean instance. It just happened to use the same instance in this particular case, but you can't count on it.

您的第一个示例仅偶然返回先前设置的数字:可能已在不同的 bean 实例上调用了 get 方法。在这种特殊情况下,它碰巧使用了相同的实例,但您不能指望它。

The second one is guaranteed to return you the previously set number, provided another request doesn't call the set method before the get method is called. A stateful bean should not be injected into a servlet.

如果另一个请求在调用 get 方法之前没有调用 set 方法,则保证第二个返回先前设置的数字。不应将有状态 bean 注入 servlet。

回答by G_H

You can use instance variables of a stateless session bean, but they're not guaranteed to be preserved across method calls. If both approaches behave the same, it simply means you're probably getting the same stateless session bean instance across method calls within the same session.

您可以使用无状态会话 bean 的实例变量,但不能保证在方法调用之间保留它们。如果两种方法的行为相同,则仅意味着您可能在同一会话中的方法调用之间获得相同的无状态会话 bean 实例。

回答by superyass

A statefulbean maintain it initial state during the conversation with the client (on or many).

状态bean 在与客户端(一个或多个)对话期间保持其初始状态。

A Statelessbean state can be changed (it attributes) during the conversation with the client (doesn't affect other clients)

一个无状态Bean状态是可以改变的(它的属性)与客户端的会话期间(不影响其他客户端)

you can see the difference if you execute multiple times!!

如果执行多次,您可以看到差异!!

回答by keuleJ

You are not supposed to have member fields (aka state) in a stateless session bean. For a stateful session bean it's ok. That's the difference.

在无状态会话 bean 中不应该有成员字段(又名状态)。对于有状态会话 bean 来说没问题。这就是区别。