2013-02-15 30 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" /> 

現在,應用程序正在:)

相關問題