通过构造器参数表示的未满足依赖-Spring中的UnsatisfiedDependencyException

xqkwcwgp  于 2023-03-28  发布在  Spring
关注(0)|答案(1)|浏览(181)

我一直在尝试创建一个Sping Boot 应用程序,通过使用Spring执行基本的CRUD操作。这个想法是我让我的FlightServiceImpl类执行最初在main方法中运行的请求,以获取其参数的值并将其存储在Map中,作为模拟数据库的一种方式,因为有一个Key/Value对组合。然而,我似乎无法通过这个异常,我不知道为什么。我复制了堆栈跟踪并粘贴到下面:
“线程“main”org.springframework.beans.factory.UnsatisfiedDependencyException:创建文件[**/AirlineTicketManager/AirlineFlightManager/target/classes/com/tbonegames/services/FlightServiceImpl.class]中定义的名为'flightServiceImpl'的bean时出错:通过构造函数参数0表示的未满足的依赖项:没有类型为“com.tbonegames.repository.FlightRepositoryImpl”的合格Bean可用:应至少有1个Bean符合自动连接候选项的条件。依赖关系注解:{}
如果你有什么建议。我会非常感激的。

package com.tbonegames.AirlineFlightManager;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.tbonegames.repository.FlightRepositoryImpl;
import com.tbonegames.services.FlightServiceImpl;

@SpringBootApplication
public class AirlineFlightManagerApplication {

    public static void main(String[] args) {
        SpringApplication.run(AirlineFlightManagerApplication.class, args);
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        //ApplicationContext context = new ApplicationContext();
        context.scan("com.tbonegames.services");
        context.refresh();

        FlightRepositoryImpl fri = context.getBean(FlightRepositoryImpl.class);

        FlightServiceImpl fsi = context.getBean(FlightServiceImpl.class);

        fsi.addFlight("1004", "American Airlines", "Chicago", "Miami", 125.00, 3);
    }

}

package com.tbonegames.services;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.tbonegames.flights.Flight;
import com.tbonegames.repository.FlightRepositoryImpl;

@Component
public class FlightServiceImpl extends Flight implements FlightService {

    //autowiring the reference of the FlightRepositoryImpl
    @Autowired
    private FlightRepositoryImpl fri;

    //constructor of the service class that takes in the FlightRepository
    //The @Autowired annotation is used for the constructor of a class. 
    @Autowired
    public FlightServiceImpl(FlightRepositoryImpl fri) {
        this.fri = fri;
    }
    
    ArrayList <FlightflightList = new ArrayList<>();
    Flight flight;
    
    /*Adding a flight must have the user's Details  
    private String flightId;
    private String airlines;
    private String source;
    private String destination;
    private Double fare;
    private LocalDate journeyDate;
    private Integer seatCount;
    */
        
    public void addFlight(String flightId) {

    }
    
    @Override
    @Bean
    public String generateFlightID(String flightId) {
        String currentFlight = flightId;
        if (flightId == null) {
            this.setFlightId("1001");
        else {
            int in = 1001;
            for (int i = 0; i < flightList.size(); i++) {
                int tempInt = Integer.parseInt(flightList.get(i).getFlightId());
                if (in == tempInt) {
                    in++;
                }

            }
            StringBuilder sb = new StringBuilder();
            sb.append(in);
            currentFlight = sb.toString();
        }
        return currentFlight;
    }
    
    @Bean
    public void searchFlight(String flightInfo) {
        fri.search(flightInfo);
    }

    @Override
    @Bean
    public void addFlight(String flightId, String airlines, String source, String destination, Double fare, Integer seatCount) {

        flight = new Flight();
        flight.setFlightId(generateFlightID(flightId));
        flight.setAirlines(airlines);
        flight.setSource(source);
        flight.setDestination(destination);
        flight.setFare(fare);
        flight.setSeatCount(seatCount);
        fri.store(flight);
    }

}

我期望程序运行并执行代码,将值存储在Map中,允许我围绕飞行数据模拟CRUD操作。

xghobddn

xghobddn1#

Edit:感谢您正确格式化您的代码。再次查看时,问题是您需要在应用程序代码中的某个地方使用@Component annotation定义FlightRepositoryImpl的实现。一旦您这样做,Spring将能够将组件自动连接到您的bean中,并且一切都应该正常工作。

相关问题