在创建spring mvc mongodb项目时,注入依赖关系的autowire失败

问题描述 投票:0回答:3

Currently I am facing issue while running the spring application in autowire configuration.

我附上了所有必要的文件

这是错误

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'carController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.practice.service.CarService com.practice.controller.CarController.carService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'carService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.practice.dao.CarDao com.practice.serviceimpl.CarServiceImpl.carDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.practice.dao.CarDao] 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)}

这是My Controller类

        package com.practice.controller;

        import java.util.ArrayList;
        import java.util.List;

        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.context.annotation.ComponentScan;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RequestMethod;
        import org.springframework.web.bind.annotation.ResponseBody;
        import org.springframework.web.bind.annotation.RestController;

        import com.mindtree.entity.Car;
        import com.mindtree.service.CarService;



        @RestController
        @RequestMapping("/car")
        public class CarController {

            @Autowired(required=true)
            CarService carService;

            @RequestMapping(value="/details",method = RequestMethod.GET)
            public @ResponseBody
            List<Car> getShopInJSON() {

                    Car polo = new Car("Volkswagen", "Polo");
                    carService.create(polo);

                    Car jetta = new Car("Volkswagen", "Jetta");
                    carService.create(jetta);

                    Car swift = new Car("Maruti Suzuki", "Swift");
                    carService.create(swift);

                    Car ertiga = new Car("Maruti Suzuki", "Ertiga");
                    carService.create(ertiga);

                    Car i10 = new Car("Hyundai", "i10");
                    carService.create(i10);

                    Car i20 = new Car("Hyundai", "i20");
                    carService.create(i20);

                    System.out.println("Find One:- " + carService.find(swift));

                    System.out.println("Find All!!");

                    List < Car > cars = carService.findAll();
                    for (Car car: cars) {
                        System.out.println(car);
                    }
                    System.out.println();
                    carService.delete(swift);

                    System.out.println();
                    i10.setModel("i10 Nxt");
                    carService.update(i10);

                    System.out.println("Find All After Update!!");

                    cars = carService.findAll();
                    for (Car car: cars) {
                        System.out.println(car);
                    }



                return cars;


            }

        }

我的dao界面

    package com.practice.dao;

    import java.util.List;

    import com.mindtree.entity.Car;


    public interface CarDao {

        public void create(Car car);

        public void update(Car car);

        public void delete(Car car);

        public void deleteAll();

        public Car find(Car car);

        public List < Car > findAll();

    }

我的道教课程

    package com.practice.daoimpl;

    import java.util.List;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.data.mongodb.core.MongoTemplate;
    import org.springframework.data.mongodb.core.query.Criteria;
    import org.springframework.data.mongodb.core.query.Query;
    import org.springframework.stereotype.Repository;

    import com.mindtree.entity.Car;

    @Repository("carDao")
    public class CarDaoImpl {

        @Autowired(required=true)
        MongoTemplate mongoTemplate;

        final String COLLECTION = "cars";

        public void create(Car car) {
            mongoTemplate.insert(car);
        }

        public void update(Car car) {
            mongoTemplate.save(car);
        }

        public void delete(Car car) {
            mongoTemplate.remove(car);
        }

        public void deleteAll() {
            mongoTemplate.remove(new Query(), COLLECTION);
        }

        public Car find(Car car) {
            Query query = new Query(Criteria.where("_id").is(car.getId()));
            return mongoTemplate.findOne(query, Car.class, COLLECTION);
        }

        public List < Car > findAll() {
            return (List < Car > ) mongoTemplate.findAll(Car.class);
        }

    }

我的实体类

    package com.practice.entity;

    import org.springframework.data.annotation.Id;
    import org.springframework.data.mongodb.core.mapping.Document;

    @Document(collection = "cars")
    public class Car {

        @Id
        private String id;
        private String brand;
        private String model;

        public Car(String brand, String model) {
            super();
            this.brand = brand;
            this.model = model;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getBrand() {
            return brand;
        }

        public void setBrand(String brand) {
            this.brand = brand;
        }

        public String getModel() {
            return model;
        }

        public void setModel(String model) {
            this.model = model;
        }

        @Override
        public String toString() {
            StringBuilder str = new StringBuilder();
            str.append("Id:- " + getId() + ", Brand:- " + getBrand() + ", Model:- " + getModel());
            return str.toString();
        }

    }

我的服务界面

    package com.practice.service;

    import java.util.List;

    import com.mindtree.entity.Car;
    public interface CarService {

        public void create(Car car);

        public void update(Car car);

        public void delete(Car car);

        public void deleteAll();

        public Car find(Car car);

        public List < Car > findAll();

    }

我的服务类

    package com.practice.serviceimpl;

    import java.util.List;

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

    import com.mindtree.dao.CarDao;
    import com.mindtree.entity.Car;
    import com.mindtree.service.CarService;

    @Service("carService")
    public class CarServiceImpl implements CarService {

        @Autowired(required=true)
        CarDao carDao;

        public void create(Car car) {
            carDao.create(car);
        }

        public void update(Car car) {
            carDao.update(car);
        }

        public void delete(Car car) {
            carDao.delete(car);
        }

        public List < Car > findAll() {
            return carDao.findAll();
        }

        public Car find(Car car) {
            return carDao.find(car);
        }

        public void deleteAll() {
            carDao.deleteAll();
        }

    }

我的applicationContext.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"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:p="http://www.springframework.org/schema/p"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context-3.1.xsd">



        <!-- Activates various annotations to be detected in bean classes -->
        <context:annotation-config/>



    </beans>

我的webmv-dispatcher-servlet.xml

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


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




        <mvc:annotation-driven />
    </beans>

我的web.xml

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

    <display-name>ProjectServerCode</display-name>



    <servlet>
        <servlet-name>webmvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-config/webmvc-dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>webmvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-config/applicationContext.xml</param-value>
     </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    </web-app>
mongodb hibernate spring-mvc
3个回答
0
投票

我认为这是因为你的CarDaoImpl没有实现CarDao界面。

(它缺乏implements CarDao


0
投票

这是因为你的CarDaoImpl没有实现CarDao接口。更新它,然后尝试


0
投票

使自动装配可选:

@Autowired(required=false)

这解决了这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.