javax.naming.NamingException:无法创建资源实例 - 尝试了许多不同的方法

时间:2014-09-24 23:36:43

标签: java tomcat cxf datasource jndi

我已经尝试了很多不同的方法来解决这个问题但是它会抛出错误,如下所示。我们使用CXF Framework编写Web服务并从context.xml文件中读取数据源。它适用于连接到数据库或其他系统源的数据源,但不适用于我现在尝试添加的数据源con / TestAPI。不知道这里有什么问题。

请帮忙。

FATAL Failed to init resources.
javax.naming.NamingException: Cannot create resource instance
    at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:146)
    at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:304)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:843)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:154)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:831)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:168)
    at com.us.test.ws.TestImpl.<init>(TestImpl.java:57)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
Sep 24, 2014 5:21:01 PM org.apache.cxf.endpoint.ServerImpl initDestination
INFO: Setting the server's publish address to be /WSTest/rest
Sep 24, 2014 5:21:01 PM org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
INFO: Creating Service {http://ws.test.us.com/}TestImplService from class com.idms.us.test.ws.TestImpl
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:100)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:61)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:877)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:839)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:440)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4887)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5381)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:977)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1655)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:619)

以下是我在context.xml文件中定义的源代码。只想提一下

<Resource SESSIONFACTORY_NAME="session/SessionFactory" 
           factory="com.dal.controlcenter.factory.ControlCenterFactory"
           name="session/ControlCenterFactory"
           type="SessionControlCenter"/>

    <Resource
        scheme="https" host="test-fe5.testnetdata.com" port="8291"
        username="user"
        password="password"
        name="con/TestAPI"
        type="java.util.HashMap"/>


<Resource name="jdbc/dataSource" auth="Container"
              type="javax.sql.DataSource"
              driverClassName="com.mysql.jdbc.Driver"
              url="jdbc:mysql://somehost:3306/db_database?useOldAliasMetadataBehavior=true"
              username="user"
              password="!password!"
              maxActive="10"
              maxIdle="1"
              maxWait="120000"
              testWhileIdle="true"
              timeBetweenEvictionRunsMillis="10000"
              minEvictableIdleTimeMillis="60000"
              removeAbandoned="true"
              removeAbandonedTimeout="300"
              noAccessToProcedureBodies="true"
              />

以下是我定义的项目web.xml。

<resource-ref>
  <description>
    some description
  </description>
  <res-ref-name>
    con/TestAPI
  </res-ref-name>
  <res-type>
    java.util.HashMap
  </res-type>
  <res-auth>
    Container
  </res-auth>
</resource-ref>

以下是我的java类的代码。

try{
    Context initCtx = new InitialContext();
    Context envCtx = (Context) initCtx.lookup("java:comp/env");
    splunkAPIConnectionMap = (HashMap) envCtx.lookup("con/TestAPI");
}catch (NamingException ex){
    log.fatal("Failed to init resources.", ex);
}        

0 个答案:

没有答案