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

时间:2014-11-18 11:35:07

标签: spring jdbc autowired spring-jdbc

我对弹簧技术有新问题

创建了一个控制器 服务接口 服务实施 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";
    }

我的服务和我的impl

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>

任何人都可以帮助我

2 个答案:

答案 0 :(得分:0)

尝试使用

将包含taskDetailService bean定义的xml配置导入到主xml配置文件中
<import resource="classpath:my.xml" />

答案 1 :(得分:0)

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

@Component
public interface TaskDetailService {

    public void saveTaskDetails(TaskDetails taskDetails);

}

或在applicationcontext.xml中创建xml配置,如

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

如果没有解决,请将applicationContext.xml链接到web.xml。