ApplicationContext konnte nicht geladen werden. Ursache: org.springframework.beans.factory.UnsatisfiedDependencyExceptioJava

Java-Forum
Guest
 ApplicationContext konnte nicht geladen werden. Ursache: org.springframework.beans.factory.UnsatisfiedDependencyExceptio

Post by Guest »

Ich habe eine Spring Boot-Anwendung mit der folgenden Struktur.

Code: Select all

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
Es gibt CustomerController.java und CustomerService.java in Controller- bzw. Servicepaketen. Die Codes für beide werden unten hinzugefügt.
CustomerController.java

Code: Select all

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 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 = 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 = 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
}
}
CustomerService.java

Code: Select all

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 getAllCustomers() {
return customerRepository.findAll();
}

public Optional  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);
}
}
Die Anwendungsklasse ist unten.

Code: Select all

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

}
Die Anwendung kann aufgrund eines Bean-Abhängigkeitsproblems nicht gestartet werden.

Code: Select all

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customerController' defined in file [C:\Users\Debaditya Bhuyan\OneDrive\Downloads\Consultancy\Open Source Development\ja
va\banking-application\target\classes\com\bank\bankingapp\controller\CustomerController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.bank.bankingapp.service.CustomerService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.bank.bankingapp.service.CustomerService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post