Java 创建一个简单的作业spring引导
我创建了一个springboot项目。 我使用弹性搜索的spring数据。 整个管道:控制器->服务->存储库已准备就绪 我现在有一个表示国家对象(名称和等位代码)的文件,我想创建一个作业,将它们全部插入弹性搜索中。 我阅读了spring文档,发现对于这样一个简单的工作来说,配置太多了。 所以我试着做一个简单的主“工作”,读取csv,创建对象,并在弹性搜索中插入它们 但我有点难以理解在这种情况下注射是如何工作的:Java 创建一个简单的作业spring引导,java,spring,spring-boot,elasticsearch,jobs,Java,Spring,Spring Boot,elasticsearch,Jobs,我创建了一个springboot项目。 我使用弹性搜索的spring数据。 整个管道:控制器->服务->存储库已准备就绪 我现在有一个表示国家对象(名称和等位代码)的文件,我想创建一个作业,将它们全部插入弹性搜索中。 我阅读了spring文档,发现对于这样一个简单的工作来说,配置太多了。 所以我试着做一个简单的主“工作”,读取csv,创建对象,并在弹性搜索中插入它们 但我有点难以理解在这种情况下注射是如何工作的: @Component public class InsertCountriesJo
@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
}
}
}
}
@组件
公共类InsertCountriesJob{
私有静态最终字符串文件=“D:path\\to\\countries.dat”;
私有静态最终记录器LOG=LoggerFactory.getLogger(InsertCountriesJob.class);
@自动连线
公共服务;
公共静态void main(字符串[]args){
LOG.info(“开始插入国家/地区作业”);
试一试{
储蓄国();
}捕获(例外e){
e、 printStackTrace();
}
}
public static void saveCountries()引发异常{
try(CSVReader CSVReader=new CSVReader(new FileReader(file))){
字符串[]值=null;
而((value=csvReader.readNext())!=null){
字符串名称=值[0];
字符串iso=值[1]。等于(“N”)?值[2]:值[1];
国家=新国家(iso,名称);
LOG.info(“info:country:{}”,country);
//在数据库中写入;
//service.save(country);基于Simon的评论。以下是我如何解决我的问题的。可能会帮助那些进入春天的人,以及那些努力不迷路的人。
基本上,要在Spring中注入任何东西,您需要一个SpringBoot应用程序
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);
}
}
}
}
基于Simon的评论。以下是我如何解决我的问题。可能会帮助那些进入春天的人,以及那些努力不迷路的人。
基本上,要在Spring中注入任何东西,您需要一个SpringBoot应用程序
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);
}
}
}
}
注入将不起作用,因为您没有Spring Boot应用程序类,并且无法以这种方式使用main。您需要使用命令行运行程序。请阅读此内容。注入将不起作用,因为您没有Spring Boot应用程序类,并且无法以这种方式使用main。您将需要使用命令行运行程序。请阅读此内容。