创建spring mvc mongodb项目时,注入依赖项的autowire失败 目前,我在autowire配置中运行spring应用程序时遇到了一个问题。
我已附上所有必要的文件创建spring mvc mongodb项目时,注入依赖项的autowire失败 目前,我在autowire配置中运行spring应用程序时遇到了一个问题。,mongodb,hibernate,spring-mvc,Mongodb,Hibernate,Spring Mvc,我已附上所有必要的文件 package com.practice.controller; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Com
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.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.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();
}
}
这是错误
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.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.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.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.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();
}
}
我的服务界面
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.service;
导入java.util.List;
导入com.mindtree.entity.Car;
公共接口服务{
公共空间创建(汽车);
公共空间更新(汽车);
公共空间删除(汽车);
public void deleteAll();
公共汽车查找(Car Car);
公共列表findAll();
}
我的服务级别
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.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.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();
}
}
package com.practice.serviceinpl;
导入java.util.List;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Service;
导入com.mindtree.dao.CarDao;
导入com.mindtree.entity.Car;
导入com.mindtree.service.CarService;
@服务(“carService”)
公共类CarServiceImpl实现了CarService{
@自动连线(必需=真)
卡道卡道;
创建公共空间(汽车){
创造(汽车);
}
公共空间更新(汽车){
更新卡道(car);
}
公共空间删除(汽车){
删除(car);
}
公共列表findAll(){
返回carDao.findAll();
}
公共汽车查找(汽车){
返回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>
<?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>
<?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>
我的webmv dispatcher servlet.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>
<?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>
<?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>
My web.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>
<?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>
<?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>
ProjectServerCode
webmvc调度程序
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/spring config/webmvc-dispatcher-servlet.xml
1.
webmvc调度程序
/
上下文配置位置
/WEB-INF/spring config/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
我认为这是因为您的CarDaoImpl
没有实现CarDao
接口
(它缺少
实现CarDao
)这是因为您的CarDaoImpl没有实现CarDao接口。更新它,然后尝试将自动布线设置为可选:
@Autowired(required=false)
这就解决了问题。可能是因为另一个问题?虽然这段代码片段可能是解决方案,但确实有助于提高您的文章质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。