在tomcat中注入自动连接的依赖项失败

时间:2015-04-19 12:55:54

标签: java spring spring-mvc

在我尝试在Eclipse中运行程序时出现以下错误:

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)

以下是代码:

订购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);
}

通过以下xml我正在尝试运行orderService类。

默认-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>

3 个答案:

答案 0 :(得分:1)

您应该在OrderService中实施OrderServiceImpl界面,例如:

 public class OrderServiceImpl implements OrderService { 

当您尝试通过以下界面进行自动装配时:

@Autowired
private OrderService orderService;

因此Spring认为它无法找到任何可以实现此接口的bean。

答案 1 :(得分:0)

OrderService是一个接口,因此它应该由实现类实例化。当您按代码processOrder()调用orderService.processOrder("P1234", 2);函数时,OrderServiceImpl应该实现OrderService

答案 2 :(得分:0)

您的OrderServiceImpl未实现OrderService,因此您希望自己在OrderController无法找到它