简体   繁体   English

为什么EJB中不需要初始化?

[英]Why Initialization is not required in EJB?

I am new to the Struts2 framework and to EJB as well. 我是Struts2框架和EJB的新手。 I have a class LoginDAO which implements checkUser method of an interface LoginDAOLocal . 我有一个LoginDAO类,它实现了checkUser接口的LoginDAOLocal方法。 I don't understand why I see different behavior for the following scenarios: 我不明白为什么我会看到以下情况的不同行为:

If I use an EJB ( LoginDAO is stateless session bean) as follows, method call works perfectly without any error. 如果我使用EJB( LoginDAO是无状态会话bean),如下所示,方法调用完美地运行而没有任何错误。

@EJB
private LoginDAOLocal loginDao;
loginDao.checkUser(userName,password);

If I use Struts2 as follows, it gives a Null pointer exception for the method call. 如果我按如下方式使用Struts2,它会为方法调用提供Null指针异常。

public class LoginAction extends ActionSupport {

    // Getters setters for userName and password)
    private LoginDAOLocal loginDao;
    loginDao.checkUser(this.userName,this.password);
}

If I use a simple Java application (no EJB or Struts2), the method call creates a compile time error saying loginDao is not initialized 如果我使用一个简单的Java应用程序(没有EJB或Struts2),方法调用会创建一个编译时错误,说明loginDao未初始化

public static void main(String[] args) {

    LoginDAOLocal loginDao;
    loginDao.checkUser(userName,password);
}

Can someone explain why this different behavior ? 有人能解释为什么这种不同的行为?

Without getting too much into the Java EE spec: EJBs are managed by an EJB container that exists in J2EE servers (JBoss \\ Websphere etc..). 没有太多了解Java EE规范:EJB由J2EE服务器(JBoss \\ Websphere等)中存在的EJB容器管理。 The container takes control of bean lifecycle and is responsible for creating \\ destroying beans according to the application needs. 容器控制bean生命周期,并负责根据应用程序需要创建\\销毁bean。

When running out of container (simple java application) your beans won't get initialized and you don't have a JNDI context to get beans from, even if you add @EJB annotation to the field member. 当用完容器(简单的java应用程序)时,即使将@EJB注释添加到字段成员,也不会初始化bean并且没有JNDI上下文来获取bean。

We can say that there are two ways to manage the beans, using the container (managed by the container), or by another component (managed by a servlet, listener or filter). 我们可以说有两种方法可以管理bean,使用容器(由容器管理),或者由另一个组件(由servlet,监听器或过滤器管理)。

Using components managed by the container, the container injects the references. 使用容器管理的组件,容器注入引用。 eg: 例如:

@WebServlet("/test")
public class MyServlet extends HttpServlet {

    @Resource(lookup = "jdbc/TestDS")
    private DataSource testDS;

}

By contrast, a component managed by a bean, eg: 相比之下,由bean管理的组件,例如:

@Namespace("/User")
@ResultPath(value = "/")
@Result(name = "success", location = "pages/login.jsp")
public class LoginAction extends ActionSupport {

}

is managed by the filter org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter . 由过滤器org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter管理。 The latter should be responsible for performing dependency injection . 后者应该负责执行依赖注入 Spring , for example, takes care of injecting all necessary dependencies. 例如, Spring负责注入所有必需的依赖项。

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

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