我在JBOSS 5服务器中部署了ejb 2.1。 jndi名称是ejb/DemoEJB,它与jboss中的jmx-console tree匹配。当我尝试按以下方式在struts动作类中查找时,它会抛出ClassCastException。请告知我是否有其他查找方法。 ejb jar是单独部署的,不在耳朵文件中。

   Properties env = new Properties();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
        env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
        env.put(Context.PROVIDER_URL, url);
        InitialContext ctx = new InitialContext(env);
DemoEJB demoEjb = (DemoEJB)ctx.lookup("ejb/DemoEJB");


ejb-jar.xml

<?xml version = '1.0' encoding = 'windows-1252'?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
<ejb-jar>
  <enterprise-beans>
    <session>
      <description>Session Bean ( Stateless )</description>
      <display-name>DemoEJB</display-name>
      <ejb-name>DemoEJB</ejb-name>
      <home>DemoEJB.DemoEJBHome</home>
      <remote>DemoEJB.DemoEJB</remote>
      <ejb-class>DemoEJB.impl.DemoEJBBean</ejb-class>
      <session-type>Stateless</session-type>
      <transaction-type>Container</transaction-type>
    </session>


jboss.xml

<?xml version = '1.0' encoding = 'UTF-8'?>
<!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 5.0//EN" "http://www.jboss.org/j2ee/dtd/jboss_5_0.dtd">
<jboss>
    <session>
      <ejb-name >DemoEJB</ejb-name>
      <jndi-name>ejb/DemoEJB</jndi-name>
      <local-jndi-name >ejb/DemoEJB</local-jndi-name>
      <ejb-ref>DemoEJB.DemoEJB </ejb-ref>
      <ejb-local-ref>DemoEJB.DemoEJBHome</ejb-local-ref>
    </session>

</jboss>


堆栈跟踪

最佳答案

我认为jndi调用将返回DemoEJB.DemoEJBHome类,而不是DemoEJB类,因此返回类广播异常。

尝试:

DemoEJBHome home =  (DemoEJBHome)ctx.lookup("ejb/DemoEJB");
DemoEJB demoEjb = home.create();

09-28 03:23