gpt4 book ai didi

java - 创建 bean 时出错 - 注入(inject) Autowiring 依赖项失败

转载 作者:行者123 更新时间:2023-12-01 10:41:19 24 4
gpt4 key购买 nike

我就是想不通!

我收到此错误

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 方法

这是我的客户存储库

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

这是 CustomerController

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

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

最佳答案

您的CustomerRepository接口(interface)应该扩展诸如CRUDRepository之类的东西,或者具有注释@RepositoryDe​​finition,否则Spring将不会拾取它。

关于java - 创建 bean 时出错 - 注入(inject) Autowiring 依赖项失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34400958/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com