简体   繁体   English

创建简单作业 spring 启动

[英]Create a simple job spring boot

I created a spring boot project.我创建了一个 spring 引导项目。 I use spring data with elastic search.我将 spring 数据与弹性搜索一起使用。 The whole pipeline: controller -> service -> repository is ready.整个管道:controller -> 服务 -> 存储库已准备就绪。

I now have a file that represents country objects (name and isoCode) and I want to create a job to insert them all in elastic search.我现在有一个代表国家对象(名称和 isoCode)的文件,我想创建一个作业以将它们全部插入弹性搜索中。 I read the spring documentation and I find that there's too much configuration for such a simple job.我阅读了 spring 文档,发现对于这么简单的工作来说配置太多了。 So I'm trying to do a simple main "job" that reads a csv, creates objects and insert them in elastic search.所以我正在尝试做一个简单的主要“工作”,它读取 csv,创建对象并将它们插入弹性搜索。

But I have a bit of trouble to understand how injection would work in this case:但是我很难理解在这种情况下注入是如何工作的:

@Component
public class InsertCountriesJob {

private static final String file = "D:path\\to\\countries.dat";
private static final Logger LOG = LoggerFactory.getLogger(InsertCountriesJob.class);

@Autowired
public CountryService service;

public static void main(String[] args) {
    LOG.info("Starting insert countries job");
    try {
        saveCountries();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

public static void saveCountries() throws Exception {
    try (CSVReader csvReader = new CSVReader(new FileReader(file))) {
        String[] values = null;
        while ((values = csvReader.readNext()) != null) {
            String name = values[0];
            String iso = values[1].equals("N") ? values[2] : values[1];
            Country country = new Country(iso, name);
            LOG.info("info: country: {}", country);
            //write in db;
            //service.save(country); <= can't do this because of the injection
        }
    }
}
}

based on Simon's comment.根据西蒙的评论。 Here's how I resolved my problem.这是我解决问题的方法。 Might help people that are getting into spring, and that are trying not to get lost.可能会帮助正在进入 spring 并且试图不迷路的人。 Basically, to inject anything in Spring, you'll need a SpringBootApplication基本上,要在 Spring 中注入任何东西,你需要一个 SpringBootApplication

public class InsertCountriesJob implements CommandLineRunner{

private static final String file = "D:path\\to\\countries.dat";
private static final Logger LOG = LoggerFactory.getLogger(InsertCountriesJob.class);

@Autowired
public CountryService service;

public static void main(String[] args) {
    LOG.info("STARTING THE APPLICATION");
    SpringApplication.run(InsertCountriesJob.class, args);
    LOG.info("APPLICATION FINISHED");
}

@Override
public void run(String... args) throws Exception {
    LOG.info("Starting insert countries job");
    try {
        saveCountry();
    } catch (Exception e) {
        e.printStackTrace();
    }
    LOG.info("job over");
}

public void saveCountry() throws Exception {
    try (CSVReader csvReader = new CSVReader(new FileReader(file))) {
        String[] values = null;
        while ((values = csvReader.readNext()) != null) {
            String name = values[0];
            String iso = values[1].equals("N") ? values[2] : values[1];
            Country country = new Country(iso, name);
            LOG.info("info: country: {}", country);
            //write in db;
            service.save(country);
        }
    }
}


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM