Spring框架的新手收到此错误:无法自动装配字段:

Posted

技术标签:

【中文标题】Spring框架的新手收到此错误:无法自动装配字段:【英文标题】:new to Spring frame work getting this error : Could not autowire field: 【发布时间】:2014-11-18 11:35:06 【问题描述】:

我遇到了新的弹簧技术问题

创建了一个控制器 服务接口 服务实施 xml文件 我们在哪里有 jdbc 信息

请做必要的事情

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mainController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.jdbc.service.TaskDetailService com.controller.MainController.taskDetailService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.jdbc.service.TaskDetailService] 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)

我的控制器

@Autowired
    TaskDetailService taskDetailService; //this inject's your bean here.

/**
     * Simply selects the home view to render by returning its name.
     */
    @RequestMapping(value = "/saveTaskDetails", method = RequestMethod.POST)
    public String saveTaskDetails(
            @ModelAttribute("taskDetails") TaskDetails taskDetails, Model model) 
        logger.info("TaskDetails save the information");



        System.out.println("------------->" + taskDetails.getTaskDuration());

        taskDetails.setLoginId(1);
        taskDetails.setPriorityId(1);
        taskDetailService.saveTaskDetails(taskDetails);

        return "sucess";
    

我的服务和我的实现

public interface TaskDetailService 

        public void saveTaskDetails(TaskDetails taskDetails);



private TaskDetailDao taskDetailDao;

public void setTaskDetailDao(TaskDetailDao taskDetailDao) 
    this.taskDetailDao = taskDetailDao;


@Override
@Transactional
public void saveTaskDetails(TaskDetails taskDetails) 
    taskDetailDao.saveTaskDetails(taskDetails);

现在我的 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">

    <!-- Root Context: defines shared resources visible to all other web components -->

    <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/xyz" />
        <property name="username" value="root" />
        <property name="password" value="xyz" />
    </bean>

    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
    </bean>

    <bean id="transactionTemplate"
        class="org.springframework.transaction.support.TransactionTemplate">
        <property name="transactionManager">
            <ref bean="transactionManager" />
        </property>
    </bean>
    <bean id="simpleNativeJdbcExtractor"
        class="org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor">
        <property name="nativeConnectionNecessaryForNativeCallableStatements"
            value="true" />
    </bean>


    <bean id="nativeAwareJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
        <property name="nativeJdbcExtractor" ref="simpleNativeJdbcExtractor" />
    </bean>

    <bean id="userDao" class="com.school.dao.UserDaoImpl">
        <!--<property name="dataSource" ref="dataSource" /> <property name ="transactionTemplate" 
            > <ref bean ="transactionTemplate" /> </property> -->
        <property name="jdbcTemplate" ref="nativeAwareJdbcTemplate" />
    </bean>

    <bean id="taskDetailDao" class="com.jdbc.dao.TaskDetailDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
        <property name="jdbcTemplate" ref="nativeAwareJdbcTemplate" />
    </bean>


    <bean id="taskDetailService" class="com.jdbc.service.TaskDetailServiceImpl">
        <property name="taskDetailDao" ref="taskDetailDao"></property>
    </bean>

</beans>

我的主要 xml 文件

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

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

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

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="com.xyz.xyz" />



</beans:beans>

我的 web.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>


    <!--   <welcome-file-list>
    <welcome-file>
      index.jsp
    </welcome-file>
  </welcome-file-list>  -->


</web-app>

谁能帮帮我

【问题讨论】:

请告诉我们您是如何在 Servlet 上下文中定义控制器的 添加了主xml文件 你确定TaskDetailServiceImpl类实现了TaskDetailService吗? 是的,它做得很好 请给我们看看 web.xml 【参考方案1】:

尝试将包含taskDetailService bean定义的xml配置导入到主xml配置文件中

<import resource="classpath:my.xml" />

【讨论】:

您不需要这样做,因为 taskDetailService 位于根上下文中,默认情况下在 servlet 上下文中可见(但不是相反) 它在某种程度上是不可见的(由于配置),因此 spring 抛出 BeanCreationException。如果 taskDetailService 的 bean 定义是可见的并且 TaskDetailServiceImpl 实现了 TaskDetailService,那么就没有这个异常的原因。此处配置错误。【参考方案2】:

在控制器类中,您正在自动装配 TaskDetailService 。但是您没有为 TaskDetailService 创建 bean 配置来自动装配。向 TaskDetailService 添加一个构造型注释(@Component、@Service、@Controller),如

@Component
public interface TaskDetailService 

    public void saveTaskDetails(TaskDetails taskDetails);


或在applicationcontext.xml中创建xml配置

  <bean id="taskDetailService" class="/package/TaskDetailService" />

如果不解决,控制将applicationContext.xml链接到web.xml中。

【讨论】:

以上是关于Spring框架的新手收到此错误:无法自动装配字段:的主要内容,如果未能解决你的问题,请参考以下文章

Thymeleaf-Spring4 无法自动装配 TemplateEngine

单元测试 Spring MVC web-app:无法自动装配字段:私有 javax.servlet.ServletContext

注入自动装配的依赖项失败,无法自动装配字段

这是 MVC DAO 的正确方法吗?我收到类似自动装配依赖项注入失败的错误

创建 bean 时出错。注入自动装配的依赖项失败。无法自动装配字段

如何进行 Spring DAO 自动装配