“应用程序无法启动” ...“字段...。需要一个类型为'......的bean,无法找到。”

时间:2019-03-29 08:03:26

标签: java spring

我正在使用Spring Tool Suite创建航班预订应用程序...我遇到以下错误...

  

申请无法开始

     

说明:

     

字段保留服务       com.siva.flightreservation.controllers.ReservationController需要一个       类型为com.siva.flightreservation.services.ReservationService的bean       找不到。

     

注入点具有以下注释:

     

@org.springframework.beans.factory.annotation.Autowired(required=true)

     

操作:

     

考虑定义一个类型的bean       配置中的“ com.siva.flightreservation.services.ReservationService”。

我尝试将Reservation.service注入到预订控制器中。...但不知道具体如何做...

这是我的预订控制文件...

package com.siva.flightreservation.controllers;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import 
org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.siva.flightreservation.dto.ReservationRequest;
import com.siva.flightreservation.entities.Flight;
import com.siva.flightreservation.entities.Reservation;
import com.siva.flightreservation.repos.FlightRepository;
import com.siva.flightreservation.services.ReservationService;


@Controller
public class ReservationController {

@Autowired
FlightRepository flightRepository;


@Autowired

ReservationService reservationService;

@RequestMapping("/showCompleteReservation")
public String showCompleteReservation(@RequestParam("flightId") Long 
flightId, ModelMap modelMap) {
    Optional<Flight> flight = flightRepository.findById(flightId);
    if (flight.isPresent()) {
        modelMap.addAttribute("flight", flight.get());
    } else {
        // ERROR?
    }                   
    return "completeReservation";

}

 @RequestMapping(value="/completeReservation",method=RequestMethod.POST)    
 public String completeReservation(ReservationRequest request, ModelMap  modelMap) {

    Reservation reservation = reservationService.bookFlight(request);
    modelMap.addAttribute("msg", "Reservation created successfully and  the id is " + reservation.getId());
    return "reservationConfirmation";   
    }
}

这是我的ReservationService接口:

@Service

public class ReservationServiceImpl implements ReservationService {

@Autowired
FlightRepository flightRepository;

@Autowired
PassengerRepository passengerRepository;

@Autowired
ReservationRepository reservationRepository;

@Override
public Reservation bookFlight(ReservationRequest request) {

    Long flightId = request.getFlightId();
    Optional<Flight> flight = flightRepository.findById(flightId);

    Passenger passenger = new Passenger();
    passenger.setFirstName(request.getPassengerFirstName());
    passenger.setLastName(request.getPassengerLastName());
    passenger.setPhone(request.getPassengerPhone());
    passenger.setEmail(request.getPassengerEmail());
    Passenger savedPassenger = passengerRepository.save(passenger);

    Reservation reservation = new Reservation();
    reservation.setFlight(flight);
    reservation.setPassenger(savedPassenger);
    reservation.setCheckedIn(false);

    Reservation savedReservation = 
reservationRepository.save(reservation);

    return savedReservation;
    }
}

ReservationConfirmation.jsp

`<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Reservation Confirmation</title>
</head>
<body>
${msg}
</body>
</html>`

和Reservation类:     `包com.siva.flightreservation.entities;

import java.util.Optional;

import javax.persistence.Entity;
import javax.persistence.OneToOne;

@Entity
public class Reservation extends AbstractEntity{

private Boolean checkedIn;
private int numberOfBags;
@OneToOne
private Passenger passenger;
@OneToOne
private Flight flight;


public Boolean getCheckedIn() {
    return checkedIn;
}

public void setCheckedIn(Boolean checkedIn) {
    this.checkedIn = checkedIn;
}

public int getNumberOfBags() {
    return numberOfBags;
}

public void setNumberOfBags(int numberOfBags) {
    this.numberOfBags = numberOfBags;
}

public Passenger getPassenger() {
    return passenger;
}

public void setPassenger(Passenger passenger) {
    this.passenger = passenger;
}

public Flight getFlight() {
    return flight;
}

public void setFlight(Flight flight) {
    this.flight = flight;
}

public boolean isPresent() {
    // TODO Auto-generated method stub
    return false;
}

public void setFlight(Optional<Flight> flight2) {
    // TODO Auto-generated method stub

   }
}

我希望将预订信息存储在数据库中,并附带一条简单的确认消息以及ID ...非常感谢

0 个答案:

没有答案