环境 netbeans 6.1 + glassfish V2
在该环境下做了一个EJB3的测试例子
然后,我新建了一个JAVA应用程序并在JAVA应用程序中通过JNDI调用EJB的一个Remote方法 getMessage(),成功通过
现在我想通过用 注解 "@EJB" 来调用该方法,可是没成功.
把代码贴出来, 在运行时, testEJB为null 也就是说,该接口(TestEJBRemote)并没有获得实例. 我想是因为EJB 和应用程序没有运行在同一个JVM下,所以得不到该方法, 那怎么解决呢?
我又在EJB的工程下面,做了一个WEB进行测试,用注解 "@EJB", 结果成功调用了getMessage()方法//JAVA APPLICATION
package testejbclient;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import stateless.TestEJBRemote;
import javax.ejb.EJB;public class Main {
    @EJB
    private static TestEJBRemote testEJB;
    public static void main(String[] args) throws IOException, NamingException {
        // TODO code application logic here
        Properties props = new Properties();
        props.load(new FileInputStream("jndi.properties"));
        InitialContext ctx = new InitialContext(props);
        TestEJBRemote testEJB = (TestEJBRemote) ctx.lookup("stateless.TestEJBRemote");
        System.out.println(testEJB.getMessage());        
    }
    */
        public static void main(String[] args) throws NamingException{
        System.out.println(testEJB.getMessage());
    }//WEB
package testservlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import stateless.TestEJBRemote;
public class TestServlet extends HttpServlet {
   @EJB
   private TestEJBRemote testEjb;
    // <editor-fold defaultstate="collapsed" desc="HttpServlet 方法。单击左侧的 + 号以编辑代码。">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    //@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        //processRequest(request, response);
        System.out.println(testEjb.getMessage());
    } 
}