OrderController
@Controller
public class OrderController {
@Autowired
private OrderService orderService;
@RequestMapping("/order/P1234/2")
public String getRequestProcessed() {
orderService.processOrder("P1234", 3);
return "redirect:/products";
}
}
ProductController的
@Controller
public class ProductController {
@Autowired
ProductService productService;
@RequestMapping("/products")
public String getAllProducts(Model model) {
System.out.println(productService.getAllProducts());
model.addAttribute("products", productService.getAllProducts());
return "products";
}
OrderServiceImpl
public class OrderServiceImpl implements OrderService {
@Autowired
ProductRepositiry productRepositiry;
@Override
public void processOrder(String productId, long count) {
Product product = productRepositiry.getProductById(productId);
if (count > product.getUnitsInStock())
throw new IllegalArgumentException("Out Of Order " + productId);
else
product.setUnitsInStock(product.getUnitsInStock() - count);
}
堆栈跟踪
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productController': Unsatisfied dependency expressed through field 'productService': No qualifying bean of type [com.webstore.service.ProductService] found for dependency [com.webstore.service.ProductService]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.webstore.service.ProductService] found for dependency [com.webstore.service.ProductService]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:350)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:775)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1238)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1151)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1038)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5027)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5337)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1407)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1397)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.webstore.service.ProductService] found for dependency [com.webstore.service.ProductService]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1398)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1051)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1018)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:570)
... 31 more
我是春天的新手,所以如果您需要更多信息,我不确定会出现什么问题,请在我尝试直接从reopositiry
获取数据时帮助我解决这个问题,但是当我工作时我试图从@repositiry
- > @service
获取所有@controllers
我收到错误,
修改
产品服务
@Service
public interface ProductService {
List<Product> getAllProducts();
Product getProductById(String productId);
}
帮我解决这个问题
由于
答案 0 :(得分:1)
尝试使用OrderServiceImpl
软件包中的@Component
来注释您的实施,例如org.springframework.stereotype.Component
。
答案 1 :(得分:0)
在应用程序上下文中没有声明的bean适合用于自动装配标有productService
的{{1}}字段
您需要声明一个实现@Autowire
的bean,就像com.webstore.service.ProductService
OrderServiceImpl
{/ 1>}一样
答案 2 :(得分:0)
您缺少ProductService类
package com.packt.webstore.service;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.packt.webstore.domain.Product;
public interface ProductService {
public List<Product> getAllProducts();
public List<Product> getProductsByCategory(String category);
Set<Product> getProductsByFilter(Map<String, List<String>> filterParams);
Product getProductById(String productID);
Set<Product> getFilteredProducts(Map<String, List<String>> filterParams, String manufacturer);
void addProduct(Product product);
}
和ProductServiceImpl
package com.packt.webstore.service.impl;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.packt.webstore.domain.Product;
import com.packt.webstore.domain.repository.ProductRepository;
import com.packt.webstore.service.ProductService;
@Service
public class ProductServiceImpl implements ProductService {
@Autowired
ProductRepository productRepository;
public List<Product> getAllProducts(){
List<Product> list = productRepository.getAllProducts();
return list;
}
public List<Product> getProductsByCategory(String category){
return productRepository.getProductsByCategory(category);
}
public Set<Product> getProductsByFilter(Map<String, List<String>> filterParams){
return productRepository.getProductsByFilter(filterParams);
}
public Product getProductById(String productID){
return productRepository.getProductById(productID);
}
public Set<Product> getFilteredProducts(Map<String, List<String>> filterParams, String manufacturer){
return productRepository.getFilteredProducts(filterParams, manufacturer);
}
public void addProduct(Product product){
productRepository.addProduct(product);
}
}