【问题标题】:Error creating bean- Injection of autowired dependencies failed创建 bean 时出错 - 自动装配依赖项的注入失败
【发布时间】:2015-12-21 17:10:07
【问题描述】:

我就是想不通!

我收到了这个错误

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();
}

这是我的 InMemoryCustomerRepository 和一些数据

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;
}

这是我的客户服务

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

这是我的 CustomerServiceImpl

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

这里是客户控制器

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

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

【问题讨论】:

  • 发布整个堆栈跟踪。你切断了所有重要的部分。

标签: java spring spring-mvc dependency-injection


【解决方案1】:

你的 CustomerRepository 接口应该要么扩展类似 CRUDRepository 的东西,要么有注解 @RepositoryDe​​finition,否则它不会被 spring 拾取。

【讨论】:

    猜你喜欢
    • 2017-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-11
    • 2015-12-06
    • 2019-06-10
    • 2014-06-09
    • 2012-04-25
    相关资源
    最近更新 更多