我正在尝试使用spring mvc和spring 3.1.2.RELEASE创建一个Web应用程序,并在尝试访问应用程序时收到错误。
该应用程序正在运行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>
应用程序上下文:
<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>
可能出现什么问题?
错误:
根本原因:
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: {}
答案 0 :(得分:1)
原因可能是您的Web应用程序上下文的这一行(一个针对DispatcherServlet注册):
<context:component-scan base-package="org.samples.example1" />
将尝试创建CustomerDao实例的实例,并且在Web应用程序上下文级别上无法使用Datasource。
相反,通过这种方式将Web上下文中的组件扫描限制为@Controllers:
<context:component-scan base-package="org.samples.example1" >
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation" />
</context:component-scan>
答案 1 :(得分:0)
我意识到我忘了在文件web.xml上添加文件applicationContext.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>
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>
即使有了这个改变,我也不得不遵循Biju的建议来改变web控制器xml:
<!-- 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>
在applicationContext.xml上,我让:
<context:component-scan base-package="org.samples.example1" />
现在应用程序正在运行:)