В настоящее время я сталкиваюсь с проблемой при запуске приложения Spring в конфигурации с автопроводом.
Я вложил все необходимые файлы
Это ошибка
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)}
Вот мой класс контроллера
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;
}
}
Мой дао интерфейс
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();
}
}
My 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>