I've chcecked many tasks about that topic and there are two ways: my knowledge about SpringBoot is too poor or the first option + my problem is a little bit different than another.
My problem is :
Firstly : my @Autowired gives me null (in every place).
Someone told me in comment that I should use CommandLineRunner - but it still doesn't work - now my method isn't called - nothting happens.
When I tried to change @Autowired to new in main method then i had null pointer ex on Autowired GHService
My code :
@SpringBootApplication
public class GhTestApplication implements CommandLineRunner{
@Autowired
public CsvConverter csvConverter;
public static void main(String[] args) throws Exception {
SpringApplication.run(GhTestApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
csvConverter.saveDataInFile();
}
}
@Service
public class GHService {
@Value("${ghUrl}")
private String ghUrl;
@Value("${ghMaxRegionSearchCar}")
int maxRegionSearchCar;
@Value("${ghMaxRegionSearchTruck}")
int maxRegionSearchTruck;
@Value("${ghMaxRegionSearchDefault}")
int maxRegionSearchDefault;
@Value("${isMaxRegionPerClient}")
boolean isMaxRegionPerClient;
@Value("${clientsForAreaSettings}")
String[] clientsForAreaSettings;
private OkHttpClient okHttpClient = new OkHttpClient();
public String getPath(List<String> points) {
String vehicle = "car";
String instructions = "true";
String calc_points = "true";
String details = "";
String avoid = "";
String points_encoded = "false";
String locale = "";
String maxRegSearch = "";
Object clientId = "test";
String url = createUrl(points, instructions, vehicle.replace("-", "_"), calc_points, details, avoid, points_encoded, locale, maxRegSearch, clientId);
Request request = new Request.Builder().url(url).build();
String responseGh = "";
try {
Response response = this.okHttpClient.newCall(request).execute();
responseGh = response.body().string();
} catch (Exception e) {
return responseGh;
}
return responseGh;
}
@Configuration
@PropertySource("classpath:/application.properties")
@ComponentScan("com.example")
public class AppConfig {
}
@Service
public class CsvConverter {
@Autowired
private GHService ghService;
public List<TestDataImport> getPointsFromFile () throws IOException {
//do propertek
InputStream fileInputStream = new FileInputStream("C:\\Users\\test2.csv");
return getImportedGRAData(fileInputStream);
}
public String convertToCSV(String[] data) {
return String.join(";", data);
}
}