TAG

RSS订阅

收藏本站

设为首页

当前位置:主页 > 服务端开发 > Java开发 > Spring开发学习 >

Error creating bean with name 'orderController': Injection o

发布时间:2016-12-21 21:34 类别:Spring开发学习

Error creating bean with name 'orderController': Injection of autowired dependencies failed;
 
root cause 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'orderController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.packt.webstore.OrderS.OrderService com.packt.webstore.controller.OrderController.orderService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.packt.webstore.OrderS.OrderService] 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)}
    org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:442)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458)
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:339)
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:306)
    org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
    javax.servlet.GenericServlet.init(GenericServlet.java:158)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:662)
 
 
Here is the code:
 
Order Controller.java
 
 package com.packt.webstore.controller;
 
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
 
    import com.packt.webstore.OrderS.OrderService;
 
    @Controller
    public class OrderController {
        @Autowired
        private OrderService orderService;
 
        /*
         * public OrderController(OrderService orderService){ this.orderService=
         * orderService; }
         */
 
        @RequestMapping("/order/P1234/2")
        public String process() {
            orderService.processOrder("P1234", 2);
            return "redirect:/products";
        }
    }
ProductController.java
 
package com.packt.webstore.controller;
 
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.packt.webstore.domain.repository.ProductRepository;
 
@Controller
public class ProductController {
@Autowired
private ProductRepository productRepository;    
@RequestMapping("/products")
public String list(Model model)
{
model.addAttribute("product",productRepository.getAllProducts());
return "products";
}
 
 
}
OrderServiceImpl.java
 
package com.packt.webstore.domain.OrderService.impl;
 
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;
 
 
@Service
public class OrderServiceImpl {
    @Autowired
      private ProductRepository productRepository;
 
      public void processOrder(String productId, int quantity) {
        Product productById = productRepository.getProductById(productId);
 
        if(productById.getUnitInStock() < quantity){
          throw new IllegalArgumentException("Out of Stock. Available Units in stock"+ productById.getUnitInStock());
        }
 
        productById.setUnitInStock(productById.getUnitInStock());
        //productById.setUnitInStock(productById.getUnitInStock() - quantity);
      }
}
 
 
**Product Repository.java**
 
package com.packt.webstore.domain.repository;
 
import java.util.List;
 
import com.packt.webstore.domain.Product;
 
public interface ProductRepository {
List<Product>getAllProducts();
 
Product getProductById(String productId);
}
OrderService.java
 
package com.packt.webstore.OrderS;
 
public interface OrderService {
    void processOrder(String  productId, int count);
}
Through the following xml i am trying run orderService class.
 
Default-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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
 
 
<!-- <context:annotation-config/> -->
<!-- <mvc:annotation-driven/> -->
    <context:component-scan base-package="com.packt.webstore.controller" />
<bean id="orderService" class="com.packt.webstore.domain.OrderService.impl.OrderServiceImpl"/>
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>
 
 



You should implement OrderService interface in your OrderServiceImpl like:

 public class OrderServiceImpl implements OrderService { 

As you are trying to autowire by interface like:

@Autowired
private OrderService orderService;

Hence spring is compaining that it can't find any beans to inject which implements this interface.

猜你会喜欢....

Copyright © 2015 www.wahenzan.com 哇!很赞 版权所有 浙ICP备14030256号-1 Power by DedeCms

浙公网安备 33010602005986号

声明:本站所有文章除标明原创外,均来自网络转载,版权归原作者所有,如果有侵犯到您的权益,请联系本站删除 网站管理员:758763728

360网站安全检测平台