2015-12-21 49 views
0

我似乎无法弄清楚!创建bean时出错 - 自动布线依赖关系注入失败

我收到此错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.packt.webstore.service.CustomerService com.packt.webstore.controller.CustomerController.customerService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.packt.webstore.domain.repository.CustomerRepository com.packt.webstore.service.impl.CustomerServiceImpl.customerRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.packt.webstore.domain.repository.CustomerRepository] 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)} 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) 
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304) 
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) 
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) 
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703) 
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760) 
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) 
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658) 
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624) 
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672) 
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543) 
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484) 
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:1284) 
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197) 
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:864) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:134) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) 
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) 
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) 
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957) 
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423) 
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079) 
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620) 
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
at java.lang.Thread.run(Unknown Source) 

这里是我的客户域

public class Customer { 
private String customerId; 
private String name; 
private String address; 
private int noOfOrderMade; 

public Customer(String customerId, String name){ 
    this.customerId = customerId; 
    this.name = name; 
} 

它已经在它的getter和setter方法

这里是我的CustomerRepository

public interface CustomerRepository { 
List<Customer> getAllCustomers(); 
} 

下面是我的一些数据

public class InMemoryCustomerRepository implements CustomerRepository { 
private List<Customer> listOfCustomers = new ArrayList <Customer>(); 

public InMemoryCustomerRepository(){ 
    Customer c1 = new Customer("1", "Ashesh Tuladhar"); 
    c1.setAddress("Ason"); 
    c1.setNoOfOrderMade(5); 

    Customer c2 = new Customer("2", "Ismaran Duwadi"); 
    c2.setAddress("Thankot"); 
    c2.setNoOfOrderMade(10); 

    Customer c3 = new Customer("3", "Siddhartha Bhatta"); 
    c3.setAddress("Hattisar"); 
    c3.setNoOfOrderMade(15); 

    listOfCustomers.add(c1); 
    listOfCustomers.add(c2); 
    listOfCustomers.add(c3); 
} 

public List<Customer> getAllCustomers(){ 
    return listOfCustomers; 
} 

这里InMemoryCustomerRepository是我的CustomerService

public interface CustomerService { 
    List<Customer> getAllCustomers(); 
} 

这里是我的CustomerServiceImpl

@Service 
public class CustomerServiceImpl implements CustomerService { 
@Autowired 
private CustomerRepository customerRepository; 
public List<Customer> getAllCustomers(){ 
    return customerRepository.getAllCustomers(); 
} 
} 

这里是CustomerController

@Controller 
public class CustomerController { 
    @Autowired 
    private CustomerService customerService; 

    @RequestMapping("/customer") 
    public String list(Model model){ 
     model.addAttribute("customers",customerService.getAllCustomers()); 
     return "customers"; 
    } 
    } 
+3

发布整个堆栈跟踪。你切断了所有重要的位。 – chrylis

回答

1

呦你的CustomerRepository接口应该扩展CRUDRepository之类的东西,或者注解@RepositoryDe​​finition,否则它不会在春天被选中。

+0

感谢人愚蠢的错误! – user3127109

相关问题