简体   繁体   English

创建名称为'empController'的bean时出错:自动连接的依赖项注入失败

[英]Error creating bean with name 'empController': Injection of autowired dependencies failed

Controller Class 控制器类

package com.beas.controller;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.ModelAttribute;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import com.beas.dto.EmpDto;

import com.beas.service.EmpService;

@Controller

public class EmpController {

    @Autowired

    EmpService empService;

    @RequestMapping(value = "/signUp")

    public String welcome(Model model) {

        model.addAttribute("empForm", new EmpDto());

        return "SignUpForm";
    }

    @RequestMapping(value = "/next", method = RequestMethod.POST)

    public String empFormSubmit(@ModelAttribute("empForm") EmpDto empDto) {

    empService.addEmpDetails(empDto);

        return "Done";
    }

}

Service Class 服务等级

package com.beas.service;

import com.beas.dto.EmpDto;

public interface EmpService {

 public void addEmpDetails(EmpDto empDto);  

}

ServiceImplement Class 服务实施类别

package com.beas.service;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import com.beas.dto.EmpDto;

import com.beas.repository.EmpRepository;

@Service("empService")

public class EmpServiceImpl implements EmpService{

    @Autowired

    EmpRepository empRepository;

    public void addEmpDetails(EmpDto empDto) {

        empRepository.addEmpDetails(empDto);

    }

}

Repository Class 储存库类

package com.beas.repository;

import com.beas.dto.EmpDto;

public interface EmpRepository {

    public void addEmpDetails(EmpDto empDto);   
}

Repository Implementation Class 仓库实现类

package com.beas.repository;

import org.hibernate.SessionFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Repository;

import com.beas.dto.EmpDto;

@Repository("empRepository")

public class EmpRepositoryImpl implements EmpRepository{

    @Autowired  
     private SessionFactory sessionFactory;

    public void addEmpDetails(EmpDto empDto) {

        sessionFactory.getCurrentSession().saveOrUpdate(empDto);    

    }

}

Pom.xml Pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.beas</groupId>

    <artifactId>SpringMvcForm</artifactId>

    <packaging>war</packaging>

    <version>0.0.1-SNAPSHOT</version>

    <name>SpringMvcForm Maven Webapp</name>

    <url>http://maven.apache.org</url>

    <dependencies>

        <dependency>

            <groupId>junit</groupId>

            <artifactId>junit</artifactId>

            <version>3.8.1</version>

            <scope>test</scope>

        </dependency>

        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>

            <artifactId>spring-core</artifactId>

            <version>3.0.5.RELEASE</version>

        </dependency>

        <dependency>


            <groupId>org.springframework</groupId>

            <artifactId>spring-web</artifactId>

            <version>3.0.5.RELEASE</version>

        </dependency>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-webmvc</artifactId>

            <version>3.0.5.RELEASE</version>

        </dependency>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-orm</artifactId>

            <version>3.0.5.RELEASE</version>

        </dependency>

        <dependency>

            <groupId>org.springframework</groupId>

            <artifactId>spring-tx</artifactId>

            <version>3.0.5.RELEASE</version>

        </dependency>

        <!-- hibernate -->

        <dependency>

            <groupId>org.hibernate</groupId>

            <artifactId>hibernate-core</artifactId>

            <version>3.6.10.Final</version>

        </dependency>


        <dependency>

            <groupId>org.hibernate</groupId>

            <artifactId>hibernate-validator</artifactId>

            <version>4.2.0.Final</version>

        </dependency>

        <dependency>

            <groupId>com.oracle</groupId>

            <artifactId>ojdbc6</artifactId>

            <version>11.2.0.3</version>

        </dependency>

        <dependency>

            <groupId>javax.validation</groupId>

            <artifactId>validation-api</artifactId>

            <version>1.1.0.Final</version>

        </dependency>

    </dependencies>

    <build>

        <finalName>SpringMvcForm</finalName>

    </build>

</project>

Web.xml Web.xml

Archetype Created Web Application 原型创建的Web应用程序

 <servlet-name>SpringMvc</servlet-name>

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

 <init-param>

        <param-name>contextConfigLocation</param-name><param-value>/WEB-INF/springForm-servlet.xml</param-value></init-param>

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

SpringMvc SpringMvc

/ /

springForm-servlet.xml springForm-servlet.xml

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

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

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

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

   <tx:annotation-driven transaction-manager="hibernateTransactionManager"/>


   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="jspViewResolver">

       <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>

      <property name="prefix" value="/WEB-INF/pages/"></property>

      <property name="suffix" value=".jsp"></property>

    </bean>

    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">

       <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver">

       </property>

         <property name="url" value="jdbc:oracle:thin:@192.168.1.7:1521:beasproj">

     </property>

        <property name="username" value="scott"></property>

        <property name="password" value="tiger"></property>

    </bean>

     <bean class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" id="sessionFactory">

      <property name="dataSource" ref="dataSource"></property>

     <property name="annotatedClasses">

       <list>

          <value>com.beas.dto.EmpDto</value>

       </list>

      </property>

    <property name="hibernateProperties">

       <props>

        <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>

        <prop key="hibernate.show_sql">true</prop>

        <prop key="hibernate.hbm2ddl.auto">update</prop>  

          </props>

         </property>

    </bean>

    <bean class="org.springframework.orm.hibernate3.HibernateTransactionManager" id="hibernateTransactionManager">

       <property name="sessionFactory" ref="sessionFactory"></property>

     </bean>

   </beans>

Please Help me to solve this error. 请帮助我解决此错误。

Thank you. 谢谢。

Please put the component-scan for service package as well. 请同时在component-scan放入service包。

<context:component-scan base-package="com.beas.repository"/>
<context:component-scan base-package="com.beas.service"/>
<context:component-scan base-package="com.beas.controller"/>

请编写empService,empRepository和sessionFactory的获取器和设置器。

暂无
暂无

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

相关问题 BeanCreationException:创建名称为&#39;userController&#39;的bean时出错:自动连接依赖项的注入失败 - BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies failed 创建名为“securityConfig”的 bean 时出错:注入自动装配的依赖项失败 - Error creating bean with name 'securityConfig': Injection of autowired dependencies failed 创建名称为&#39;initDbService&#39;的bean时出错:自动连接的依赖项注入失败 - Error creating bean with name 'initDbService': Injection of autowired dependencies failed 创建名称为&#39;reportsController&#39;的bean时出错:自动连接的依赖项注入失败; - Error creating bean with name 'reportsController': Injection of autowired dependencies failed; 创建名称为“ homeController”的bean时出错:自动连接的依赖项注入失败 - Error creating bean with name 'homeController': Injection of autowired dependencies failed 创建名为“countriesDao”的 bean 时出错:注入自动装配的依赖项失败; - Error creating bean with name 'countriesDao': Injection of autowired dependencies failed; 创建名称为&#39;serviceController&#39;的bean时出错:自动连接的依赖项注入失败; - Error creating bean with name 'serviceController': Injection of autowired dependencies failed; 创建名称为&#39;employeeController&#39;的bean时出错:自动连接依赖项的注入失败 - Error creating bean with name 'employeeController': Injection of autowired dependencies failed 创建名称为&#39;studentController&#39;的bean时出错:自动连接依赖项的注入失败 - Error creating bean with name 'studentController': Injection of autowired dependencies failed 创建名称为“ clientsDaoImpl”的bean时出错:自动连接依赖项的注入失败; - Error creating bean with name 'clientsDaoImpl': Injection of autowired dependencies failed;
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM