java—当我运行spring引导程序时,任何填充数据库的方法都是第一件事

bqujaahr  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(173)

我有一个应用程序,它读取文件夹中的csv文件,然后读取这些csv文件并将它们放入数据库,但我无法在运行spring boot程序后立即找出如何填充数据库
我的flightservice.java

package com.package.service;

@Service
public class FlightService{
    File dir = new File("C:\\Users\\Akhil\\Desktop\\assignmentFour\\CSV");
    static FlightDao flightDao = (FlightDao) AppContextUtil.context.getBean("flightDao");

    public void readCSV() {
        File files[] = dir.listFiles();
        ArrayList<String> listofFileNames = new ArrayList<String>();
        for (File file : files) {
            Airline airline = FlightController.readFile(file);
            flightDao.saveAirline(airline);
        }

    }
}

flightcontroller.java文件

package com.package.controller;

public class FlightController {

    public static final SimpleDateFormat dateformat = new SimpleDateFormat("dd-MM-yyyy");

    public static Airline readFile(File file) {
        BufferedReader reader = null;
        Airline aObj = new Airline();
        aObj.setName(file.getName());
        HashSet<Flight> flight_Set = new HashSet<Flight>();
        try {
            reader = new BufferedReader(new FileReader(file));
            String line = reader.readLine();
            line = reader.readLine();

            while (line != null) {
                Flight f = manipulateLine(line,aObj);
                line = reader.readLine();
                flight_Set.add(f);
            }
        } catch (Exception e) {
            System.err.println("Could Not Read the File");
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (Exception e) {
                    System.err.println("Could Not Close the File");
                }
            }
        }
        aObj.setFlights(flight_Set); 
        return aObj;
    }

    private static Flight manipulateLine(String line, Airline aObj) {

        StringTokenizer st = new StringTokenizer(line, "|");

        String flightNo = st.nextToken();
        String depLoc = st.nextToken();
        String arrLoc = st.nextToken();

        String validTillDate = st.nextToken();
        Date validTill = new Date();
        try {
            validTill = dateformat.parse(validTillDate);
        } catch (ParseException e) {
            System.err.print("Date not in appropriate(dd-MM-yyyy) format");
        }

        int flightTime = Integer.parseInt(st.nextToken());
        Double flightDuration = Double.parseDouble(st.nextToken());
        int fare = Integer.parseInt(st.nextToken());

        String avail = st.nextToken();
        Boolean seatAvailability;
        if (avail.charAt(0) == 'Y')
            seatAvailability = true;
        else
            seatAvailability = false;

        String flightClass = st.nextToken();

        return new Flight(flightNo, depLoc, arrLoc, fare, validTill,
                flightTime, flightDuration, seatAvailability, flightClass,aObj);
        }
}

用于url

package com.package.controller;

@Controller
public class FlightCont {

     @RequestMapping(value ="/flightSearch" , method=RequestMethod.POST)
        public ModelAndView flightSearch(@Valid @ModelAttribute("flightDetails")FlightDetailsEntered flightDetails,BindingResult result){

            ModelAndView modelAndView =new ModelAndView("flightSearch");
            if(result.hasErrors())
            { 
                System.err.println(result);
                return modelAndView ;
            }
            List<Flight> listOfMatchingFlights= flightDetails.getListOfMatchingFlights();
            modelAndView = new ModelAndView("flightList");
            modelAndView.addObject("list", listOfMatchingFlights);
            return modelAndView ;
        }

}

当我运行程序时,我得到了这个错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flightService' defined in file [C:\Users\Akhil\Desktop\project\target\classes\com\package\service\FlightService.class]: Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError

我不知道我做错了什么任何建议都会很有帮助

fnvucqvd

fnvucqvd1#

您可以将文件保存在类路径(src/main/resources)中,并按如下所述加载文件,
包com.package.service;
@公务舱飞行服务{

public void readCSV() {
    InputStream stream = readerFile("CSV.txt");
    try (BufferedReader br = new BufferedReader(new InputStreamReader(stream))) {
        String line = br.readLine();
        System.out.println(line);
    } catch (IOException e) {
        //do nothing
    }
}

private InputStream readerFile(String fileName) {
    return getClass().getClassLoader().getResourceAsStream(fileName);
}

}

相关问题