1

我正在尝试使用带有 spring 3.1.2.RELEASE 的 spring mvc 创建一个 Web 应用程序,并且在尝试访问应用程序时出现错误。

该应用程序在 tomcat 7 上运行。

道:

@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: {}
4

2 回答 2

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>
于 2013-02-15T04:26:14.237 回答
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" />

现在应用程序正在运行:)

于 2013-02-16T02:32:22.893 回答