2013-02-15 87 views
0

我正在尝试使用spring 3.1.2.RELEASE使用spring mvc创建一个web应用程序,并且在尝试访问应用程序时出现错误。spring 3.1.2无法使用jdbcdaosupport自动装载数据源

应用程序是运行在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

你能分享完整的错误登录? – 2013-02-15 03:32:57

+0

这真的是根本原因吗?你可能无法创建一个数据源 - 因此发生错误? – gerrytan 2013-02-15 04:24:46

回答

1

其原因可能是这一行你的web应用程序上下文的(一个对DispatcherServlet的注册):

<context:component-scan base-package="org.samples.example1" /> 

这将尝试创建CustomerDao实例和Datasource的实例在Web应用程序上下文级别不可用。

相反,限制在Web上下文中的组件扫描只是@Controllers这样:

<context:component-scan base-package="org.samples.example1" > 
    <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation" /> 
</context:component-scan> 
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> 

即使这种改变我遵循六必居的建议与改变网络控制器的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" /> 

现在,应用程序正在:)

相关问题