Spring 3 newbie - NoSuchBeanDefinitionException - 我错过了什么?

时间:2011-05-12 16:27:10

标签: spring autowired spring-3

刚开始看春季3和MVC。我得到以下异常......

org.springframework.beans.factory.BeanCreationException:创建名为'registerController'的bean时出错:注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private browniePoints.dao.UserDao browniePoints.web.RegisterController.userDao;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型为[browniePoints.dao.UserDao]的匹配bean:预期至少有一个bean可以作为此依赖项的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

从谷歌的所有例子来看,这就是所需要的。我错过了什么?

界面......

package browniePoints.dao;

public interface UserDao
{

实施......

package browniePoints.dao.impl;

@Component
public class UserDaoImpl implements UserDao
{
@Autowired
private DataSource dataSource;

控制器......

package browniePoints.web;

@Controller
public class RegisterController
{
@Autowired
private UserDao userDao;

web.xml有一个指向以下xml配置的servlet ...

  <?xml version="1.0" encoding="UTF-8"?>
  <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    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">

    <!-- Scans the classpath of this application for @Components to deploy as beans -->
    <context:component-scan base-package="browniePoints.web" />

    <!-- Configures the @Controller programming model -->
    <mvc:annotation-driven />

    <!-- Forwards requests to the "/" resource to the "index" view -->
    <mvc:view-controller path="/" view-name="index"/>

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources/ directory -->
    <mvc:resources mapping="/resources/**" location="/resources/" />

    <!--  point URLs ending .jsp to views in /WEB-INF/views -->
    <bean id="viewResolver"       class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="viewClass" value="org.springframework.web.servlet.view.JstlView">      </property>
      <property name="prefix" value="/WEB-INF/views/"></property>
      <property name="suffix" value=".jsp"></property>        
    </bean>

    <!-- load properties from config.properites  -->
    <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    
      <property name="location" value="config.properties" /> 
    </bean>

    <!-- Define a SQL Server datasource -->
    <bean id="dataSource" destroy-method="close"       class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="${jdbc.driverClassName}"/>
      <property name="url" value="${jdbc.url}"/>
      <property name="username" value="${jdbc.username}"/>
      <property name="password" value="${jdbc.password}"/>
    </bean>

  </beans>

感谢。

1 个答案:

答案 0 :(得分:4)

在黑暗中拍摄;您在组件扫描指令中缺少UserDaoImpl的包:

<context:component-scan base-package="x.z"/>