I am trying to configure a Spring MVC application that uses the Spring JDBC template. I have only one problem: Spring doesn't seem to pick up my bean configuration for NamedParameterJdbcTemplate.
web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
WEB-INF/desk-servlet.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<display-name>Desk</display-name>
<servlet>
<servlet-name>desk</servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>desk</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
desktop servlet.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"/>
<property name="url" value="jdbc:jtds:sqlserver://localhost:1433/desk_db;instance=SQLEXPRESS"/>
<property name="username" value="user"/>
<property name="password" value="pass"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
<mvc:resources mapping="/resources/**" location="/resources/"/>
<context:component-scan base-package="com.myapplication.desk.mvc, com.myapplication.desk.persistence, com.myapplication.desk.service"/>
<context:annotation-config/>
<mvc:annotation-driven/>
</beans>
And in my code, I implemented a DAO class to handle the database level for my application that likes bean:
@Autowired
private NamedParameterJdbcTemplate jdbcTemplate;
When I test my application using "gradlew - bootRun", I get this exception:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate]
found for dependency: expected at least 1 bean which qualifies as autowire
candidate for this dependency. Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
I made all the other customization-based annotations that seem to be well matched. Therefore, I have two questions:
- How can I define this bean in my code?
- How can I make Spring pick up my configuration?
! , , .