Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java @EJB返回空值_Java_Eclipse_Jboss_Ejb 3.1 - Fatal编程技术网

Java @EJB返回空值

Java @EJB返回空值,java,eclipse,jboss,ejb-3.1,Java,Eclipse,Jboss,Ejb 3.1,我不熟悉JBoss和J2EE。我正试图用会话Bean联系单例Bean。长期而言,我希望在Singleton bean中缓存一些信息,如果它不可用,则在数据库中查找相关信息。但首先,我要创建尽可能最简单的用例 然而,当我在Eclipse中运行这个简单的用例(获取一个应用程序计数器)时,我得到了一个对EJB的空引用,我不确定下一步该转到哪里 我正在运行EclipseKeplar、JDK1.7、JBossAS7.1和EJB3.1 这是jsp文件 <?xml version="1.0" encod

我不熟悉JBoss和J2EE。我正试图用会话Bean联系单例Bean。长期而言,我希望在Singleton bean中缓存一些信息,如果它不可用,则在数据库中查找相关信息。但首先,我要创建尽可能最简单的用例

然而,当我在Eclipse中运行这个简单的用例(获取一个应用程序计数器)时,我得到了一个对EJB的空引用,我不确定下一步该转到哪里

我正在运行EclipseKeplar、JDK1.7、JBossAS7.1和EJB3.1

这是jsp文件

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<jsp:useBean id="counter" class="com.bender.counter.Counter" scope="session"/>
<%-- <jsp:useBean id="counterBean" class="com.bender.counterbean.CounterBean" scope="application"/> --%>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Test Counter</title>
</head>

<body>
Hit Counter( <%=counter.getHitCount() %> )<br/>
<%-- Hit CounterBean( <%=counterBean.getHits() %> )<br/> --%>
</body>
</html>
这是单身豆

package com.bender.counterbean;

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;

@Startup
@Singleton
public class CounterBean {
    private int hits = 1;

    public CounterBean() {
        super();
        System.out.println("In constructor of CounterBean.");
    }

    @PostConstruct
    public void init(){
        System.out.format("In post Construct of CounterBean, hits( %d )%n", this.hits);
    }

    // Increment and return the number of hits
    public int getHits() {
        return hits++;
    }
}
package com.bender.counterbean;

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;


@Startup
@Singleton
public class CounterBean {
    private int hits = 1;

    public CounterBean() {
        super();
        System.out.println("In constructor of CounterBean.");
    }

    @PostConstruct
    public void init(){
        System.out.format("In post Construct of CounterBean, hits( %d )%n", this.hits);
    }

    // Increment and return the number of hits
    public int getHits() {
        return hits++;
    }
}
下面是server.log的输出。注意,counterbean似乎是向jndi注册的。另请注意,jsp中的注释已被注释掉。这是试图从jsp联系CounterBean。该尝试是成功的,因此我认为CounterBean正在服务器中成功运行

17:07:50,427 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-12) JBAS015876: Starting deployment of "Counter.war"
17:07:50,443 INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-15) JNDI bindings for session bean named CounterBean in deployment unit deployment "Counter.war" are as follows:

    java:global/Counter/CounterBean!com.bender.counterbean.CounterBean
    java:app/Counter/CounterBean!com.bender.counterbean.CounterBean
    java:module/CounterBean!com.bender.counterbean.CounterBean
    java:global/Counter/CounterBean
    java:app/Counter/CounterBean
    java:module/CounterBean

17:07:50,458 INFO  [stdout] (MSC service thread 1-8) In constructor of CounterBean.

17:07:50,458 INFO  [stdout] (MSC service thread 1-8) In post Construct of CounterBean, hits( 1 )

17:07:50,474 INFO  [org.jboss.web] (MSC service thread 1-4) JBAS018210: Registering web context: /Counter
17:07:50,489 INFO  [org.jboss.as.server] (management-handler-threads - 83) JBAS018559: Deployed "Counter.war"
17:07:55,185 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) Failure

谢谢你的帮助

只有DI容器(在JBoss中是weld)知道对象时,注入才起作用。这通常是因为容器本身创建了实例。您还可以将现有对象传递给容器,让它注入所有依赖项

CDI注入JSP页面在JBoss AS 7中似乎不起作用:

也许您可以看看JSP的替代方案:

  • JSF:
  • servlet:
如果您无法切换到其他技术,那么您可能必须自己进行注射。使用以下方法让CDI容器对给定对象执行所有注入:

public static <T> void programmaticInjection(Class<T> clazz, T injectionObject) throws NamingException {
    InitialContext initialContext = new InitialContext();
    Object lookup = initialContext.lookup("java:comp/BeanManager");
    BeanManager beanManager = (BeanManager) lookup;
    AnnotatedType<T> annotatedType = beanManager.createAnnotatedType(clazz);
    InjectionTarget<T> injectionTarget = beanManager.createInjectionTarget(annotatedType);
    CreationalContext<T> creationalContext = beanManager.createCreationalContext(null);
    injectionTarget.inject(injectionObject, creationalContext);
    creationalContext.release();
}

调用
programmaticInjection()。这通常是因为容器本身创建了实例。您还可以将现有对象传递给容器,让它注入所有依赖项

CDI注入JSP页面在JBoss AS 7中似乎不起作用:

也许您可以看看JSP的替代方案:

  • JSF:
  • servlet:
如果您无法切换到其他技术,那么您可能必须自己进行注射。使用以下方法让CDI容器对给定对象执行所有注入:

public static <T> void programmaticInjection(Class<T> clazz, T injectionObject) throws NamingException {
    InitialContext initialContext = new InitialContext();
    Object lookup = initialContext.lookup("java:comp/BeanManager");
    BeanManager beanManager = (BeanManager) lookup;
    AnnotatedType<T> annotatedType = beanManager.createAnnotatedType(clazz);
    InjectionTarget<T> injectionTarget = beanManager.createInjectionTarget(annotatedType);
    CreationalContext<T> creationalContext = beanManager.createCreationalContext(null);
    injectionTarget.inject(injectionObject, creationalContext);
    creationalContext.release();
}

调用
programmaticInjection()
后,将注入@EJB注释对象。

我通过添加JNDI查找解决了这个问题。我必须在JBossAS7.1实例中设置一些不正确的内容,以便一个bean可以使用(默认)JNDI,而JSP页面具有(完整)JNDI访问权限。下面是更新(和清理)的代码

JSP

还有独生子豆

package com.bender.counterbean;

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;

@Startup
@Singleton
public class CounterBean {
    private int hits = 1;

    public CounterBean() {
        super();
        System.out.println("In constructor of CounterBean.");
    }

    @PostConstruct
    public void init(){
        System.out.format("In post Construct of CounterBean, hits( %d )%n", this.hits);
    }

    // Increment and return the number of hits
    public int getHits() {
        return hits++;
    }
}
package com.bender.counterbean;

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;


@Startup
@Singleton
public class CounterBean {
    private int hits = 1;

    public CounterBean() {
        super();
        System.out.println("In constructor of CounterBean.");
    }

    @PostConstruct
    public void init(){
        System.out.format("In post Construct of CounterBean, hits( %d )%n", this.hits);
    }

    // Increment and return the number of hits
    public int getHits() {
        return hits++;
    }
}
这是JBoss控制台的输出

16:21:55,740 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-12) JBAS015876: Starting deployment of "Counter.war"
16:21:55,888 INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-1) JNDI bindings for session bean named CounterBean in deployment unit deployment "Counter.war" are as follows:

    java:global/Counter/CounterBean!com.bender.counterbean.CounterBean
    java:app/Counter/CounterBean!com.bender.counterbean.CounterBean
    java:module/CounterBean!com.bender.counterbean.CounterBean
    java:global/Counter/CounterBean
    java:app/Counter/CounterBean
    java:module/CounterBean

16:21:56,033 INFO  [stdout] (MSC service thread 1-7) In constructor of CounterBean.

16:21:56,033 INFO  [stdout] (MSC service thread 1-7) In post Construct of CounterBean, hits( 1 )

16:21:56,088 INFO  [org.jboss.web] (MSC service thread 1-15) JBAS018210: Registering web context: /Counter
16:21:56,093 INFO  [org.jboss.as] (MSC service thread 1-15) JBAS015874: JBoss AS 7.1.0.Final "Thunder" started in 2001ms - Started 185 of 258 services (72 services are passive or on-demand)
16:21:56,176 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "Counter.war"
16:22:11,455 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) In constructor of CounterBean.

我可以通过添加JNDI查找来解决这个问题。我必须在JBossAS7.1实例中设置一些不正确的内容,以便一个bean可以使用(默认)JNDI,而JSP页面具有(完整)JNDI访问权限。下面是更新(和清理)的代码

JSP

还有独生子豆

package com.bender.counterbean;

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;

@Startup
@Singleton
public class CounterBean {
    private int hits = 1;

    public CounterBean() {
        super();
        System.out.println("In constructor of CounterBean.");
    }

    @PostConstruct
    public void init(){
        System.out.format("In post Construct of CounterBean, hits( %d )%n", this.hits);
    }

    // Increment and return the number of hits
    public int getHits() {
        return hits++;
    }
}
package com.bender.counterbean;

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;


@Startup
@Singleton
public class CounterBean {
    private int hits = 1;

    public CounterBean() {
        super();
        System.out.println("In constructor of CounterBean.");
    }

    @PostConstruct
    public void init(){
        System.out.format("In post Construct of CounterBean, hits( %d )%n", this.hits);
    }

    // Increment and return the number of hits
    public int getHits() {
        return hits++;
    }
}
这是JBoss控制台的输出

16:21:55,740 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-12) JBAS015876: Starting deployment of "Counter.war"
16:21:55,888 INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-1) JNDI bindings for session bean named CounterBean in deployment unit deployment "Counter.war" are as follows:

    java:global/Counter/CounterBean!com.bender.counterbean.CounterBean
    java:app/Counter/CounterBean!com.bender.counterbean.CounterBean
    java:module/CounterBean!com.bender.counterbean.CounterBean
    java:global/Counter/CounterBean
    java:app/Counter/CounterBean
    java:module/CounterBean

16:21:56,033 INFO  [stdout] (MSC service thread 1-7) In constructor of CounterBean.

16:21:56,033 INFO  [stdout] (MSC service thread 1-7) In post Construct of CounterBean, hits( 1 )

16:21:56,088 INFO  [org.jboss.web] (MSC service thread 1-15) JBAS018210: Registering web context: /Counter
16:21:56,093 INFO  [org.jboss.as] (MSC service thread 1-15) JBAS015874: JBoss AS 7.1.0.Final "Thunder" started in 2001ms - Started 185 of 258 services (72 services are passive or on-demand)
16:21:56,176 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018559: Deployed "Counter.war"
16:22:11,455 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) In constructor of CounterBean.

如果我使用初始上下文查找bean以获得JNDI名称,我就能够解决这个问题。因此,我必须为我的jboss实例正确设置默认JNDI?private CounterBean CounterBean;{try{counterBean=(counterBean)new InitialContext().lookup(“java:global/Counter/counterBean”);}catch(NamingException e){e.printStackTrace();}}如果我使用初始上下文查找bean以获得JNDI名称,我就能够解决这个问题。因此,我必须为我的jboss实例正确设置默认JNDI?private CounterBean CounterBean;{try{counterBean=(counterBean)new InitialContext().lookup(“java:global/Counter/counterBean”);}catch(NamingException e){e.printStackTrace();}}我非常感谢您的回答。但是,我想你误解了我的问题。问题不在JSP页面中,我能够访问这两个bean而没有问题。问题在于会话bean(通过@EJB)获取对单例bean的引用。再次感谢您的回复,它提供了一些关于访问上下文进行更改的好主意。不,我没有误解您的问题。只有当CDI容器a)构造实例或b)您让它通过上面给出的方法注入时,@EJB注释才由CDI容器进行评估。它是null,因为没有注入发生,因为jsp:useBean标记的实现不知道CDI容器,因此不能要求它构造实例或让它进行注入。您可以从JNDI中查找bean,但这与JBoss容器看到EJB注释时所做的完全相同。请接受我的道歉,现在我更好地理解了您的答案。感谢您接受澄清。由于这是示例代码,而且我还处于学习阶段,所以我将切换到JSF以使CDI正常工作。我非常感谢您的回答。但是,我想你误解了我的问题。问题不在JSP页面中,我能够访问这两个bean而没有问题。问题在于会话bean(通过@EJB)获取对单例bean的引用。再次感谢您的回复,它提供了一些关于访问上下文进行更改的好主意。不,我没有误解您的问题。只有当CDI容器a)构造实例或b)您让它通过上面给出的方法注入时,@EJB注释才由CDI容器进行评估。它是null,因为没有注入发生,因为jsp:useBean标记的实现不知道CDI容器,因此不能要求它构造实例或让它进行注入。您可以从JNDI中查找bean,但这与JBoss容器看到EJB注释时所做的完全相同