我正在尝试在应用程序中创建 JNDI bean,但在下面第一个代码块的第三行出现错误“无法创建资源实例”。我正在按照http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html中所示的步骤进行操作。
代码如下:
豆实例化:
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。应该有什么问题?