开发者

Spring 3 newbie - NoSuchBeanDefinitionException - what have I missed?

Just started looking at spring 3 and MVC. I get the following exception...

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'registerController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private browniePoints.dao.UserDao browniePoints.web.RegisterController.userDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [browniePoints.dao.UserDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

From all the examples I have google this is all that is needed. What have I missed?

The interface...

package browniePoints.dao;

public interface UserDao
{

The implementation...

package browniePoints.dao.impl;

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

The controller...

package browniePoints.web;

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

web.xml has one servlet that points to following xml config...

  <?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="vi开发者_开发知识库ewResolver"       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>

Thanks.


Shot in the dark; you are missing the package of UserDaoImpl in your component-scan directive:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜