我使用 TomEE 作为应用程序服务器。TomEE 使用 Tomcat 7。我有以下代码:
package com.jndi;
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;
public class GitObjectFactory implements ObjectFactory {
public GitObjectFactory() {
}
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment) throws NamingException {
GitConnection gitConnection = new GitConnection();
// Customize the bean properties from our attributes
Reference ref = (Reference) obj;
RefAddr refAddr = ref.get("gitLocation");
String value = (String) refAddr.getContent();
gitConnection.setGitLocation(value);
return gitConnection;
}
}
public class GitConnection {
private String gitLocation;
public GitConnection() {
}
public String getGitLocation() {
return gitLocation;
}
public void setGitLocation(String gitLocation) {
this.gitLocation = gitLocation;
}
}
in context.xml
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource name='git/GitConnectionFactory' auth='Container'
type='com.jndi.GitConnection'
factory='com.jndi.GitObjectFactory'
gitLocation='C:\jGitClone\.git'/>
在 web.xml http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd" version="2.5">
<context-param>
<!-- Specifies the list of Spring Configuration files in comma separated format.-->
/WEB-INF/spring/email.xml
</context-param>
<listener>
<!-- Loads your Configuration Files-->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>i</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>i</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<resource-env-ref>
<description>Object Factory for GitConnection instances.</description>
<resource-env-ref-name>git/GitConnectionFactory</resource-env-ref-name>
<resource-env-ref-type>com.jndi.GitConnection</resource-env-ref-type>
</resource-env-ref>
</web-app>
When I load Tomcat, I can see successfully that the resource git/GitConnectionFactory is registered:
INFO: Configuring Service(id=git/GitConnectionFactory, type=Resource, provider-id=ProvidedByTomcat)
2013 年 4 月 6 日 17:54:25 org.apache.openejb.assembler.classic.Assembler createRecipe
一旦我部署应用程序,我就会得到以下异常:
org.apache.openejb.OpenEJBException: No provider available for resource-env-ref 'git/GitConnectionFactory' of type 'com.jndi.GitConnection' for 'i'.
任何想法问题出在哪里?