无法加载ApplicationContext,原因是:org.springframework.beans.factory.UnsatisfiedDependencyException:创建bean时出错

问题描述 投票:0回答:0

我有一个具有以下结构的 Spring Boot 应用程序。

banking-app
├── backend
│   ├── src
│   │   ├── main
│   │   │   ├── java
│   │   │   │   └── com
│   │   │   │       └── bank
│   │   │   │           ├── controller
│   │   │   │           ├── model
│   │   │   │           ├── repository
│   │   │   │           ├── service
│   │   │   │           └── BankingAppApplication.java
│   │   │   ├── resources
│   │   │   │   ├── application.properties
│   │   │   │   └── static
│   │   │   │       ├── css
│   │   │   │       ├── js
│   │   │   │       └── images
│   │   │   └── templates
│   │   │       └── index.html (or Thymeleaf template)
├── pom.xml
└── README.md

控制器包和服务包中分别有CustomerController.javaCustomerService.java。下面添加了两者的代码。 CustomerController.java

package com.bank.bankingapp.controller;

import com.bank.bankingapp.model.Customer;
import com.bank.bankingapp.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@Controller
@RequestMapping("/customers")
public class CustomerController {

    private final CustomerService customerService;

    @Autowired
    public CustomerController(CustomerService customerService) {
        this.customerService = customerService;
    }

    @GetMapping
    public String getAllCustomers(Model model) {
        List<Customer> customers = customerService.getAllCustomers();
        model.addAttribute("customers", customers);
        return "customer/customer-list";  // Returns the Thymeleaf template for the customer list
    }

    @GetMapping("/create")
    public String showCreateForm(Model model) {
        model.addAttribute("customer", new Customer());  // Empty form object to bind to
        return "customer/customer-create";  // Thymeleaf template to render the form
    }

    @PostMapping
    public String createCustomer(@ModelAttribute Customer customer) {
        customerService.createCustomer(customer);  // Create the new customer
        return "redirect:/customers";  // Redirect to the list of customers after creation
    }

    @GetMapping("/{id}")
    public String showCustomerDetail(@PathVariable String id, Model model) {
        Optional<Customer> customer = customerService.getCustomerById(id);
        if (customer.isPresent()) {
            model.addAttribute("customer", customer.get());
            return "customer/customer-detail";  // Thymeleaf template for displaying customer details
        }
        return "redirect:/customers";  // Redirect if the customer is not found
    }

    @GetMapping("/{id}/edit")
    public String showEditForm(@PathVariable String id, Model model) {
        Optional<Customer> customer = customerService.getCustomerById(id);
        if (customer.isPresent()) {
            model.addAttribute("customer", customer.get());
            return "customer/customer-edit";  // Thymeleaf template for editing the customer
        }
        return "redirect:/customers";  // Redirect if the customer is not found
    }

    @PostMapping("/{id}/edit")
    public String updateCustomer(@PathVariable String id, @ModelAttribute Customer customer) {
        customerService.updateCustomer(id, customer);  // Update the customer
        return "redirect:/customers";  // Redirect to the list of customers after updating
    }

    @GetMapping("/{id}/delete")
    public String deleteCustomer(@PathVariable String id) {
        customerService.deleteCustomer(id);  // Delete the customer
        return "redirect:/customers";  // Redirect to the list of customers after deletion
    }
}

客户服务.java

package com.bank.bankingapp.service;

import com.bank.bankingapp.model.Customer;
import com.bank.bankingapp.repository.CustomerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class CustomerService {

    private final CustomerRepository customerRepository;

    @Autowired
    public CustomerService(CustomerRepository customerRepository) {
        this.customerRepository = customerRepository;
    }

    public List<Customer> getAllCustomers() {
        return customerRepository.findAll();
    }

    public Optional<Customer> getCustomerById(String id) {
        return customerRepository.findById(id);
    }

    public Customer createCustomer(Customer customer) {
        return customerRepository.save(customer);
    }

    public Customer updateCustomer(String id, Customer customer) {
        customer.setId(id);
        return customerRepository.save(customer);
    }

    public void deleteCustomer(String id) {
        customerRepository.deleteById(id);
    }
}

Application 类如下。

package com.bank.bankingapp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = {"com.bank.bankingapp","com.bank.bankingapp.service","com.bank.bankingapp.controller","com.bank.bankingapp.model","com.bank.bankingapp.repository"})
public class BankingApplication {

    public static void main(String[] args) {
        SpringApplication.run(BankingApplication.class, args);
    }

}

应用程序由于 Bean 依赖问题而失败。

java.lang.IllegalStateException:无法加载ApplicationContext 引起原因:org.springframework.beans.factory.UnsatisfiedDependencyException:创建文件中定义的名称为“customerController”的bean时出错[C:\Users\Debaditya Bhuyan\OneDrive\Downloads\Consultancy\Open Source Development\ja va anking-应用程序

java spring spring-boot spring-mvc
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.