2012-05-28 93 views
0

我想在應用程序中創建一個JNDI bean,但是錯誤「無法創建資源實例」在第一個代碼塊的第三行出現。我按照http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html中所示的步驟執行。創建JNDI Java 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()); 

類爲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; 
    } 

} 

類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被輸出到CATALINA_HOME/lib中在壓縮JAR。 服務器是Tomcat 6,IDE是Eclipse Indigo,系統(在開發機器中)是Windows Vista。 什麼應該出問題?

+0

定義「不起作用」。例外?沒有?還有別的嗎? –

+0

@JimGarrison,對不起,我用錯誤更新了問題,這只是「無法創建資源實例」,這是一個非常普遍的原因。它出現在第一個代碼塊的第三行。 – Alex

+0

請發佈**整個**堆棧跟蹤,包括所有「由...引起」部分。 –

回答

1

最後我發現了這個問題。我不知道這對任何人是否有用,但問題出在server.xml中的<context>標籤中。該標籤必須理解<Resource>標籤,我忘記了這一標籤。所以,上下文並不是「看到」資源。