Use main args with SpingBoot & velocity

127 views Asked by At

I am making a program and I need to read from main args but I don't know how to proceed.

my code

public class Application {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}

controller

@Controller
public class SampleControler {

    @Autowired
    private Checker checker;

    @RequestMapping("/monitoring")
    String home(Model map)
    {
        try {
            map.addAttribute("donnee", checker.run(/*My main args has to be there*/);

            return "index" ;
        }catch (Exception e) {
            return "error";
        }
    }
}

Can you anyone help?

1

There are 1 answers

4
ksokol On BEST ANSWER

As described in the manual you'll need to add command line arguments like

java -jar yourapp.jar --yourParam1=value1 --yourParam2=value2

to your application.

In your SampleControler you can access those arguments through the Environment:

@Controller
public class SampleControler {

    @Autowired
    private Checker checker;

    @Autowired
    private Environment environment;

    @RequestMapping("/monitoring")
    String home(Model map)
    {
        try {
            map.addAttribute("donnee", checker.run(environment.getProperty("yourParam1", "defaultValue"));

            return "index" ;
        }catch (Exception e) {
            return "error";
        }
    }
}