gpt4 book ai didi

javax.naming.NameNotFoundException : Name [jdbc/spitterDS] is not bound in this Context. 无法找到 [jdbc]

转载 作者:可可西里 更新时间:2023-11-01 07:27:32 25 4
gpt4 key购买 nike

我正在尝试使用 Spring 的 jee jndi-lookup 标记访问 tomcat 中的 JNDI datasource

异常表明我没有正确注册我的数据源,但我无法弄清楚为什么不正确。

这是我的代码:-

service-context.xml:-

<?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:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd">

<context:component-scan base-package="spitter" />
<mvc:annotation-driven />

<jee:jndi-lookup id="dataSource" jndi-name="/jdbc/spitterDS" resource-ref="true" />

</beans>

webapp/META-INF/context.xml:-

<Context path="/spitter" reloadable="true" cachingAllowed="false" antiResourceLocking="true">

<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>

<Resource name="jdbc/spitterDS" auth="Container" type="org.apache.commons.dbcp.BasicDataSource"
maxActive="100" maxIdle="30" maxWait="10000" username="root" password="password"
driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/spitter" />
</Context>

web.xml:-

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

<display-name>Spitter</display-name>

<resource-ref>
<description>Spitter DS</description>
<res-ref-name>jdbc/spitterDS</res-ref-name>
<res-type>org.apache.commons.dbcp.BasicDataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:service-context.xml
</param-value>
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

最后,

MainController.java:-

package spitter.mvc;

import java.sql.Connection;
import java.sql.SQLException;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
@RequestMapping("/")
public class MainController {
@Resource(name="dataSource")
private DataSource dataSource;

@RequestMapping(method = RequestMethod.GET)
public String showHome(Model model) throws SQLException{
Connection con = dataSource.getConnection();

return "spitter";
}
}

我在 Eclipse kepler 中为 JEE 开发人员使用 Tomcat 7。当我在 eclipse 中启动 tomcat 服务器时出现异常:-

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mainController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [jdbc/spitterDS] is not bound in this Context. Unable to find [jdbc].

我假设我在某个地方打错了字,但找不到它。请帮忙!

最佳答案

"/jdbc/spitterDS" 不等于 "jdbc/spitterDS"

service-context.xml文件中,
改变

<jee:jndi-lookup id="dataSource" jndi-name="/jdbc/spitterDS" resource-ref="true" />

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/spitterDS" resource-ref="true" />

编辑:

Error creating bean with name 'dataSource':

根据文档:

... in the @Resource(name="jdbc/Foo") DataSource ds; annotation, the global JNDI name is jdbc/Foo.
...
The @Resource annotation in the application code looks like this:
@Resource(name="jdbc/helloDbDs") javax.sql.DataSource ds;

改变:

@Resource(name="dataSource")
private DataSource dataSource;

收件人:

@Resource(name="jdbc/spitterDS")
private DataSource dataSource;

引用:Using the Java Naming and Directory Interface .

关于javax.naming.NameNotFoundException : Name [jdbc/spitterDS] is not bound in this Context. 无法找到 [jdbc],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21934989/

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