创建JNDI Java Bean错误

时间:2012-05-28 19:40:05

标签: java tomcat jndi

我正在尝试在应用程序中创建JNDI bean,但是错误“无法创建资源实例”会在下面第一个代码块的第三行引发。我正按照http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html中所示的步骤进行操作。

以下是代码:

Bean实例化:

            Context initCtx = new InitialContext();
            Context envCtx = (Context) initCtx.lookup("java:comp/env");
            MyBean bean = (MyBean) envCtx.lookup("bean/MyBeanFactory");

            System.out.println("foo = " + bean.getFoo() + ", bar = " +
                           bean.getBar());

Class MyBean:

package com.company.intrasc.funcionarios;
public class MyBean {
  private String foo = "Default Foo";

  public String getFoo() {
    return (this.foo);
  }

  public void setFoo(String foo) {
    this.foo = foo;
  }

  private int bar = 0;

  public int getBar() {
    return (this.bar);
  }

  public void setBar(int bar) {
    this.bar = bar;
  }

}

Class MyBeanFactory:

package com.company.intrasc.factory;

import java.util.Enumeration;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.NamingException;
import javax.naming.RefAddr;
import javax.naming.Reference;
import javax.naming.spi.ObjectFactory;
import com.company.intrasc.funcionarios.MyBean;

public class MyBeanFactory implements ObjectFactory {

  public Object getObjectInstance(Object obj,
      Name name, Context nameCtx, Hashtable environment)
      throws NamingException {

      // Acquire an instance of our specified bean class
      MyBean bean = new MyBean();

      // Customize the bean properties from our attributes
      Reference ref = (Reference) obj;
      Enumeration addrs = ref.getAll();
      while (addrs.hasMoreElements()) {
          RefAddr addr = (RefAddr) addrs.nextElement();
          String name1 = addr.getType();
          String value = (String) addr.getContent();
          if (name1.equals("foo")) {
              bean.setFoo(value);
          } else if (name1.equals("bar")) {
              try {
                  bean.setBar(Integer.parseInt(value));
              } catch (NumberFormatException e) {
                  throw new NamingException("Invalid 'bar' value " + value);
              }
          }
      }

      // Return the customized instance
      return (bean);

  }

}

应用程序文件夹中的web.xml:

<resource-env-ref>
  <description>Object factory for MyBean instances.</description>
  <resource-env-ref-name>bean/MyBeanFactory</resource-env-ref-name>
  <resource-env-ref-type>com.company.intrasc.funcionarios.MyBean</resource-env-ref-type>
</resource-env-ref>

server.xml上下文配置:

  <Resource name="bean/MyBeanFactory" auth="Container" type="com.company.intrasc.funcionarios.MyBean" factory="com.company.intrasc.factory.MyBeanFactory" bar="23"/>

堆栈跟踪:

javax.naming.NamingException: Cannot create resource instance
    at org.apache.naming.factory.ResourceEnvFactory.getObjectInstance(ResourceEnvFactory.java:114)
    at javax.naming.spi.NamingManager.getObjectInstance(Unknown Source)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:793)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:140)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:781)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
    at com.company.intrasc.servlet.Logon.executa(Logon.java:81)
    at com.company.intrasc.servlet.Controller.service(Controller.java:80)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Unknown Source)

MyBean和MyBeanFactory类在压缩JAR中导出到catalina_home / lib中。 服务器是Tomcat 6,IDE是Eclipse Indigo,系统(在开发机器中)是Windows Vista。 应该出什么问题?

1 个答案:

答案 0 :(得分:0)

最后我发现了这个问题。我不知道它对任何人都有用,但问题出在server.xml的<context>标签中。此标记必须包含<Resource>标记,我忘了将其添加到标记中。因此,上下文并没有“看到”资源。