Reputation: 79
I'm trying create a web app using spring mvc with spring 3.1.2.RELEASE and I getting error when a try access application.
The app is running over tomcat 7.
DAO:
@Repository
public class CustomerDao extends JdbcDaoSupport{
@Autowired
public CustomerDao(DataSource dataSource){
super();
setDataSource(dataSource);
}
public void teste(){
System.out.println("teste");
}
}
Spring MVC Servlet
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org /2001/XMLSchema-instance"
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">
<mvc:annotation-driven />
<context:component-scan base-package="org.samples.example1" />
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
Application Context:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<context:annotation-config />
<context:component-scan base-package="org.samples.example1" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/exemplo" />
<property name="username" value="root" />
<property name="password" value="root"/>
</bean>
<bean id="customerDao" class="org.samples.example1.repository.CustomerDao">
<constructor-arg ref="dataSource" />
</bean>
</beans>
What could be wrong?
Error:
root cause:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
Upvotes: 1
Views: 2286
Reputation: 79
I realized that I had forgot to add the mapping of file applicationContext.xml on file web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Exemplo 01</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>exemplo1</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>exemplo1</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
The tags bellow was missing on web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Even with this change I had to follow the Biju's advice to change the web controller xml with:
<!-- The controllers are autodetected POJOs labeled with the @Controller annotation. -->
<context:component-scan base-package="org.samples.example1" use-default-filters="false">
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
On applicationContext.xml I let:
<context:component-scan base-package="org.samples.example1" />
Now the application is working :)
Upvotes: 0
Reputation: 49915
The reason probably is this line of your web application context(one registered against DispatcherServlet):
<context:component-scan base-package="org.samples.example1" />
which would try to create an instance of CustomerDao instance and Datasource is not available at the level of Web application context.
Instead, restrict the component scan in your web context to just @Controllers this way:
<context:component-scan base-package="org.samples.example1" >
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation" />
</context:component-scan>
Upvotes: 1