0

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);
    }

}

HappyProgrammer
  • 139
  • 1
  • 10
  • 2
    You call `csvConverter.saveDataInFile();` before Spring is initialized (by `SpringApplication.run(GhTestApplication.class, args);` – Mark Rotteveel Jun 01 '22 at 10:38
  • 2
    `static` and `@Autowired` doesn't make sense. Generally you should *never* use these two together – Lino Jun 01 '22 at 10:43
  • As an aside, if you want to make this a commandline application, see [here](https://www.baeldung.com/spring-boot-console-app) for an example using the `CommandLineRunner` interface. – Mark Rotteveel Jun 01 '22 at 10:44
  • You can have your method call as part of PostConstruct phase rather than in Main ``@PostConstruct private void postConstruct() {}``, have a look at example at https://www.baeldung.com/spring-postconstruct-predestroy – Gopinath Radhakrishnan Jun 01 '22 at 11:23

0 Answers0