0

刚开始研究 spring 3 和 MVC。我得到以下异常......

org.springframework.beans.factory.BeanCreationException:创建名为“registerController”的bean时出错:注入自动装配的依赖项失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有 browniePoints.dao.UserDao browniePoints.web.RegisterController.userDao;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型为 [browniePoints.dao.UserDao] 的匹配 bean:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{@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>

谢谢。

4

1 回答 1

4

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

<context:component-scan base-package="x.z"/>
于 2011-05-12T17:40:35.853 回答