gpt4 book ai didi

spring - javax.naming.NameNotFoundException : Name [comp/env] is not bound in this Context

转载 作者:行者123 更新时间:2023-12-04 17:21:59 33 4
gpt4 key购买 nike

我正在开发一个使用 tomcat 服务器托管连接池的 spring 应用程序。

我在tomcat的context.xml中的标签中定义了数据源:(我使用的是spring 2.0.7)在context.xml中:

<context>
<Resource name="jdbc/irb_prod"
auth="Container"
type="javax.sql.DataSource"
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
username="xxx"
password="xxx"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@xxx:1521:xxx"
maxWait="1000"
removeAbandoned="true"
maxActive="5"
maxIdle="5"
removeAbandonedTimeout="60"
logAbandoned="true"/>
</context>

在我的 ApplicationContext.xml(即在 spring 配置文件中) 中,代码是:
     <bean id="dataSource" 
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/irb_prod</value>
</property>
</bean>


<!-- Transaction manager for a single JDBC DataSource -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource"><ref local="dataSource"/></property>
</bean>

当应用程序启动时,我收到以下错误:
javax.naming.NameNotFoundException: Name java:/comp/env/mypool is not bound in this              Context
org.apache.naming.NamingContext.lookup(NamingContext.java:803)
org.apache.naming.NamingContext.lookup(NamingContext.java:159)
org.apache.naming.SelectorContext.lookup(SelectorContext.java:158)
javax.naming.InitialContext.lookup(Unknown Source)
org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:132)
org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:88)
org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:130)
org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:155)
org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:93)
org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:105)
org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java :197)
org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:184)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:563)
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInit ialization(AbstractApplicationContext.java:736)
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractAppli cationContext.java:369)
org.springframework.context.support.ClassPathXmlApplicationContext.<init> (ClassPathXmlApplicationContext.java:123)
org.springframework.context.support.ClassPathXmlApplicationContext.<init> (ClassPathXmlApplicationContext.java:66)
MainServlet.init(MainServlet.java:21)
javax.servlet.GenericServlet.init(GenericServlet.java:160)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:541)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:383)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java :188)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:288)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)

请帮我解决这个问题。

最佳答案

我在使用 tomcat 时遇到了类似的问题,并在 eclipse 中执行了以下操作来解决它:

  • 在服务器的 context.xml 中,包含 jdbc 连接详细信息。
  • 停止服务器
  • 清理项目( Project --> Clean )、tomcat 服务器( Server--> Right Click --> Clean )和 Tomcat 工作目录( Server--> Right Click--> Clean Tomcat Work Directory )。
  • 启动服务器并运行。

  • 即使在 context.xml 中维护了详细信息,有时它也可能不会得到反射(reflect)。
    在这种情况下,请按照第 3 步操作,希望对您有所帮助。

    关于spring - javax.naming.NameNotFoundException : Name [comp/env] is not bound in this Context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21527714/

    33 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com