当前位置: 首页 > 知识库问答 >
问题:

tomcat上的Spring boot rest服务部署:找不到jdbc

锺玺
2023-03-14

以下是我根据StackOverflow上的其他帖子所做的工作。

  1. 我的context.xml中定义了javax.sql.DataSource资源和ResourceLink。下面粘贴的代码。
  2. 我的pom.xml有带有参考资料和插件的构建部分。下面粘贴的代码。
  3. My ApplicationContext.xml为类org.springframework.jndi.jndiObjectFactoryBean定义了bean,属性为jndiName pinting to context xml中定义的jdbc资源。
  4. 最初,我发现maven build没有将上下文xml包含在war中,因此我按照一些帖子的建议将meta-inf目录复制到main/java/resources文件夹下。现在,上下文xml确实包含在war中的meta-inf文件夹下。
  5. 当我部署war时,它无法启动,并在下面记录错误。由:javax.naming.NameNotFoundException:Name[jdbc/mydb]在此上下文中没有绑定。找不到[jdbc]。在org.apache.naming.namingContext.lookup(namingContext.java:817)

上下文xml==>

   <context>
    <Resource name="jdbc/MyDB" auth="Container" type="javax.sql.DataSource"
        username="uid" password="pwd" driverClassName="net.sourceforge.jtds.jdbc.Driver"
        url="jdbc:jtds:sqlserver://myserver:1433/mydatabase" maxActive="8"
        maxIdle="4" validationQuery="select 1" />

         <ResourceLink name="jdbc/MyDB"
             global="jdbc/MyDB"
              type="javax.sql.DataSource" />
      </context>
    <build>
                <resources>
                    <resource>
                        <!-- this is relative to the pom.xml directory -->
                        <directory>${project.basedir}/src/main/resources</directory>
                        <filtering>true</filtering>
                    </resource>
                </resources>
                <plugins>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <executions>
                        <execution>
                            <configuration>
                                <mainClass>com.passg.springboot.SpringBootRestApiApp</mainClass>
                            </configuration>
                        </execution>
                    </executions>
                    </plugin>
                </plugins>
            </build> 
    <?xml version="1.0" encoding="UTF-8"?>
        <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd">

            <bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
                <property name="jndiName" value="java:comp/env/jdbc/MyDB"/>
            </bean>
    Caused by: javax.naming.NameNotFoundException: Name [jdbc/MyDB] is not bound in this Context. Unable to find [jdbc].
        at org.apache.naming.NamingContext.lookup(NamingContext.java:817)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:159)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:827)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:159)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:827)
        at org.apache.naming.NamingContext.lookup(NamingContext.java:173)
        at org.apache.naming.SelectorContext.lookup(SelectorContext.java:163)
        at javax.naming.InitialContext.lookup(InitialContext.java:417)
        at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:155)
        at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87)
        at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152)
        at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:179)
        at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95)
        at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:106)
        at org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:231)
        at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:217)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1648)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1585)
        ... 110 more

我需要部署的tomcat应用程序能够识别上下文xml并建立jndi连接。任何帮助都将不胜感激。

共有1个答案

田翰林
2023-03-14

呜呜!多亏了StackOverflow上的另一篇帖子,我终于找到了答案!!我认为这是我所犯过的最愚蠢的错误。在我的上下文xml中,元素“context”应该是大写的“c”,即“context”!!

感谢这个链接中的家伙!

部署WAR文件时出现问题。启动组件[]失败

 类似资料:
  • 我正在编写一个简单的RESTful Web服务,使用Java、tomcat7、jersey和IDE eclipse。 当我使用eclipse(服务器)启动web服务时,它运行良好。我测试了GET和POST方法。但当我在WAR文件中导出应用程序并使用tomcat manage UI部署时。它返回404 not found状态。 下面是一个例子: 这里是网络。xml: 有人能解释在eclipse中启动

  • 我编写了一个maven web服务,它引用了另一个项目。web服务本身仅包含2个pom条目: jersey容器servlet:2.13 web服务POM: 项目1也是一个maven项目,只有一个pom条目,另一个本地项目(我们称之为项目2)。 项目2又是一个maven项目,其pom文件包含5个条目: lang-猜测: 0.0.7 Stanford-corenlp: 3.5.2 斯坦福-科伦坡: 3

  • 我正在尝试将Spring应用程序部署到运行Apache Tomcat的服务器。然而,我得到了这个错误: 严重:ContainerBase.AddChild:start:…org.apache.Catalina.LifecycleException:无法启动组件[StandardEngine[Catalina].StandardHost[localhost].StandardContext[/Rur

  • 您好,我是java开发的新手,我正在尝试修改一个同事开发的应用程序,但是当我试图部署它时,我得到了这个错误: servlet.ServletException:org.apache.ibatis.exceptions.PersistenceException: resource database.properties org.glassfish.jersey.servlet.webcomponen

  • 我试图部署一个JavaRest服务器tomcat,使用Maven tomcat插件,但Tomcat不能正常工作。 部署时(通过-e tomcat7:deploy或-e tomcat7:redeploy),Tomcat会回复: 我已经检查过tomcat正在运行。我在上一个堆栈溢出上找到的修复都不起作用。 这是我对tomcat插件的pom配置: 这是我的maven设置: 这就是网络。xml: 我的配置