Description
在一个 Maven结构的项目中,想使用 jdbcTemplate,但是就是找不到 Bean,报错:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
各项配置如下:
在 web.xml 中引入 src/main/resources/applicationContext/xxx-servlet.xml 和 /WEB-INF/applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 5 id="WebApp_ID" version="3.0"> 6 <display-name>xxx</display-name> 7 <welcome-file-list> 8 <welcome-file>index.html</welcome-file> 9 <welcome-file>index.htm</welcome-file> 10 <welcome-file>index.jsp</welcome-file> 11 </welcome-file-list> 12 13 <context-param> 14 <param-name>contextConfigLocation</param-name> 15 <param-value> 16 /WEB-INF/applicationContext.xml 17 </param-value> 18 </context-param> 19 20 <servlet> 21 <servlet-name>xxx</servlet-name> 22 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 23 <init-param> 24 <param-name>contextConfigLocation</param-name> 25 <param-value>classpath:applicationContext/xxx-servlet.xml</param-value> 26 </init-param> 27 <load-on-startup>1</load-on-startup> 28 </servlet> 29 30 <servlet-mapping> 31 <servlet-name>xxx</servlet-name> 32 <url-pattern>/servlet/*</url-pattern> 33 </servlet-mapping> 34 </web-app>
在 xxx-servlet.xml 中扫描包
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 4 xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 6 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 8 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 9 10 11 <context:component-scan base-package="com.xxx.web.controller" /> 12 <context:component-scan base-package="com.xxx.service" /> 13 <context:component-scan base-package="com.xxx.dao" /> 14 <aop:aspectj-autoproxy proxy-target-class="true" /> 15 </beans>
在 /WEB-INF/applicationContext.xml 再引入 src/main/resources/applicationContext/xxx-data.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> 5 6 <import resource="classpath:applicationContext/xxx-data.xml" /> 7 </beans>
在 xxx-data.xml 中加入 bean Datasource 和 jdbcTemplate
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 9 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 10 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd" 11 default-lazy-init="true"> 12 13 <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean" 14 lazy-init="true"> 15 <property name="jndiName"> 16 <value>xxxDatasource</value> 17 </property> 18 </bean> 19 20 <bean id="txManager" 21 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 22 <property name="dataSource" ref="dataSource" /> 23 </bean> 24 <tx:annotation-driven transaction-manager="txManager" /> 25 26 <bean id="jdbcTemplate" 27 class="org.springframework.jdbc.core.JdbcTemplate"> 28 <property name="dataSource" ref="dataSource" /> 29 </bean> 30 </beans>
一切都准备好了,调用的时候 jdbcTemplate 始终是 null。
Caused By & Solution
因为在 web.xml 中没有加入 ContextLoaderListener 来查找 /WEB-INF/applicationContext.xml 文件,所以里边引用的东西也找不到。
更新 web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 5 id="WebApp_ID" version="3.0"> 6 <display-name>ama</display-name> 7 <welcome-file-list> 8 <welcome-file>index.html</welcome-file> 9 <welcome-file>index.htm</welcome-file> 10 <welcome-file>index.jsp</welcome-file> 11 </welcome-file-list> 12 13 <context-param> 14 <param-name>contextConfigLocation</param-name> 15 <param-value> 16 /WEB-INF/applicationContext.xml 17 </param-value> 18 </context-param> 19 <!-- 加入ContextLoaderListener --> 20 <listener> 21 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 22 </listener> 23 24 <servlet> 25 <servlet-name>ama</servlet-name> 26 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 27 <init-param> 28 <param-name>contextConfigLocation</param-name> 29 <param-value>classpath:applicationContext/xxx-servlet.xml</param-value> 30 </init-param> 31 <load-on-startup>1</load-on-startup> 32 </servlet> 33 34 <servlet-mapping> 35 <servlet-name>xxx</servlet-name> 36 <url-pattern>/servlet/*</url-pattern> 37 </servlet-mapping> 38 </web-app>