简体   繁体   English

关于EJB Bean构造和Java Servlet初始化

[英]About EJB Beans Constructs and Java Servlet Inits

I have created a SimpleBean.java 我创建了一个SimpleBean.java

public class SimpleBean implements SimpleBeanRemote  
{  
    private static String str = "Test";  

    private int value =0;  

    public SimpleBean()  
    {  
        G += " A";  
    value++;  
    }   

    public String getValue()
    {  
        return G + Integer.toString(value);  
    }  
}

In the servlet, I have declared an private @EJB SimpleBeanRemote bean. 在servlet中,我声明了一个private @EJB SimpleBeanRemote bean。 In the doGet handler, bean.getValue() statement is called. doGet处理程序中,将bean.getValue()语句。
The return outcome: Test A A1 返回结果:测试A A1
It seems that the SimpleBean is constructed by twice. 看来SimpleBean是由两次构造的。 For my observation, Once is in the EJB construction and another issued by the Servlet for @EJB bean. 以我的观察,Once位于EJB构造中,另一个位于Servlet的@EJB bean中。 I want to ask is it correct for the initialization of the EJB. 我想问一下EJB初始化是否正确。 If the SimpleBean is created twice, then the first one is no use? 如果SimpleBean创建两次,那么第一个没有用吗?

Is It possible for constructing the EJB object for one-time only. 是否可以仅一次构造EJB对象。

Would we share and discuss these operations mean? 我们将分享和讨论这些操作的意思吗?

To understand that, you should take a look at the types of session beans . 要了解这一点,您应该看看会话bean类型

And, if you are sure you need this: 而且,如果您确定需要以下内容:

constructing the EJB object for one-time only. 仅一次构造EJB对象。

, you should go for a singleton session bean (using the JavaEE 6 @Singleton annotation). ,则应该使用单例会话bean(使用JavaEE 6 @Singleton批注)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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