繁体   English   中英

春豆无法自动接线

[英]spring bean could not autowire

我正在研究Spring 4 MVC项目,以学习Spring Rest框架。 这是我正在创建Web应用程序的一个课堂项目。 我设法从各种在线教程中编写了一些春季代码。 我遇到了bean autowiring问题。

错误

Could not autowire field: taxApp.dao.daoImpl.userDaoImpl taxApp.controller.loginController.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [taxApp.dao.daoImpl.userDaoImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency

这些是样本文件

loginController.java

@RestController
public class loginController {
    @Autowired
    userDAO userService;  //Service which will do all data retrieval/manipulation work
    //other methods
}

userDAO.java

public interface userDAO {
    public void insert(user _user);
    public user findUserByEmail(String email);
}

userDaoImpl.java

@Service("userDAO")
public class userDaoImpl implements userDAO{
    @Autowired
    private DataSource dataSource;    

    //other methods
}

我已经创建了配置文件,但不确定它们是否放置正确。 例如,我的dispatcher.xml位于web-inf文件夹中,而其他xml文件位于资源中。 还要确保所有类路径都是正确的。

dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 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.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

  <mvc:annotation-driven/>
  <context:component-scan base-package="taxApp" />
  <context:annotation-config />

  <bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
      <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
      <value>.jsp</value>
    </property>
  </bean>

</beans>

spring-user.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    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-2.5.xsd">

    <bean id="userDAO" class="dao.impl.userDaoImpl">
        <property name="dataSource" ref="dataSource" />
    </bean>

</beans>

spring-module.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  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.xsd">

  <!-- Using Oracle datasource -->
  <import resource="database/data-source-cfg.xml" />
  <import resource="dao/spring-user.xml" />

</beans>

web.xml

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <!-- Spring MVC -->
  <servlet>
      <servlet-name>dispatcher</servlet-name>

      <servlet-class>
          org.springframework.web.servlet.DispatcherServlet
      </servlet-class>

      <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
      <servlet-name>dispatcher</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

我的春季项目的结构看起来像

src
   main
       java
           taxApp
                 controller
                           loginController.java
                 model
                      user.java
                 dao
                    userDaoImpl.java
                    userDAO.java
       resources
                database
                        data-source-cfg.xml
                user 
                    spring-user.xml
                spring-module.xml
       webapp
             web-inf
                    dispatcher-servlet.xml
                    web.xml
             index.jsp

我在分发程序servlet.xml中看不到已导入spring-module.xml的任何地方。 这就是您的bean未在sprint上下文中加载的原因。

由于您是春季新手,因此建议您编写完整的Java配置。 这样,您可以更轻松地调试错误。 观看以下视频以了解更多信息

具有Intellij Full Java Config的Spring MVC。 没有web.xml或Servlet.xml

还有一个建议,请先为类名称和服务/组件名称提供资金

您的课程路径中没有以下课程,

<bean id="userDAO" class="dao.impl.userDaoImpl">

应该是什么

<bean id="userDAO" class="taxApp.dao.userDaoImpl">

这是因为xml配置优先于Java配置,并且xml配置已损坏。

另外,如果您从xml中删除了以上userDAO bean声明,它将使用@Service("userDAO")自动进行选择,然后它应该可以工作。

但是,在不了解Spring的情况下, 不应同时使用Java和xml配置。 而且您的命名转换不太可能是Java。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM