问题:我在Spring整合SpringMVC中遇到no bean named ‘jdbctemplate’ available错误如图

原因:在web.xml配置文件中只配置了前端控制器,没有配置ContextLoaderListener(ServletContext)监听器,因此applicationContext.xml也就是Spring的配置文件在服务器启动的时候没有被加载,Spring容器没有被创建出来,加上即可
web.xml配置文件代码整体如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Archetype Created Web Application</display-name>
<!--配置Spring的监听器,默认只加载WEB-INF目录下的applicationContext.xml配置文件-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--中文乱码过滤器-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置前端控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMvc.xml</param-value>
</init-param>
<!--服务器启动加载该servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
本文详细解析了在Spring整合SpringMVC过程中出现的NoBeanNamed‘jdbctemplate’错误原因及解决方法。指出web.xml配置中缺少ContextLoaderListener导致Spring配置文件未被加载,从而无法创建Spring容器。通过正确配置ContextLoaderListener和DispatcherServlet,确保服务器启动时能够加载Spring和SpringMVC的配置。

被折叠的 条评论
为什么被折叠?



