@EJB null zurück

Ich bin neu in JBoss und J2EE. Ich bin versucht zu Kontaktieren, eine Singleton-Bean eine Session bean. Langfristig möchte ich, um Informationen zu Cachen, die in der Singleton-bean, und wenn es nicht verfügbar ist, Suche die relevanten Informationen in einer Datenbank. Aber zuerst, ich ' m erstellen der einfachste Anwendungsfall möglich.

Allerdings, wenn ich dieses einfache use-case (eine Anwendung Zähler) in Eclipse erhalte ich eine null-Referenz auf das EJB und ich bin mir nicht sicher, wo Sie wiederum weiter.

Ich bin mit Eclipse Keplar, JDK1.7, JBoss AS 7.1, EJB 3.1

Hier ist der jsp-Datei.

<?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>

Hier ist die erste session-bean

package com.bender.counter;

import com.bender.counterbean.CounterBean;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;

@SessionScoped
public class Counter {
    @EJB 
    CounterBean counterBean;

    private int hitCount;

    public Counter() {
        this.hitCount = 0;
    }

    public int getHitCount() {
        if (counterBean == null) {
            System.out.println("Failure");
            hitCount = -1;
        } else {
            hitCount = counterBean.getHits();
        }
        return hitCount;
    }

    public void setHitCount(int newHits) {
        this.hitCount = newHits;
    }
}

Hier ist die Singleton-Bean

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++;
    }
}

Hier ist die Ausgabe vom server.log. Beachten Sie, dass die counterbean erscheint, um sich mit jndi. Beachten Sie auch die auskommentiert in der jsp. Dies war ein Versuch an den CounterBean aus der jsp. Dieser Versuch war erfolgreich, so dass ich denke, dass die CounterBean erfolgreich läuft in der server.

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

Vielen Dank für Ihre Hilfe!

Ich bin in der Lage dieses Problem zu beheben, wenn ich den lookup der bean mit der Initiale Kontext, um die JNDI-Namen. So, ich muss die standardmäßige JNDI-setup nicht richtig für mein jboss-Instanz?
private CounterBean counterBean; { try { counterBean = (CounterBean) new InitialContext().lookup("java:global/Counter/CounterBean"); } catch (NamingException e) { e.printStackTrace(); } }

InformationsquelleAutor Robert Bender | 2013-10-09

Schreibe einen Kommentar