Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use the ApplicationArguments in spring-boot

Tags:

spring-boot

I am learning the Spring-Boot(I am new to it), reading the Spring Boot Document. In the 23.6 Accessing application arguments, It talk about the ApplicationArguments, and the code is:

package com.example.project;

import org.springframework.boot.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;

import java.util.*;

@Component
public class MyBean {
  @Autowired
  public MyBean(ApplicationArguments args) {
    boolean debug = args.containsOption("debug");
    List<String> files = args.getNonOptionArgs();
    System.out.println(debug);
    System.out.println(files);
  }
}

It says if run with "--debug logfile.txt" debug=true, files=["logfile.txt"].

But in my project, I don't know how to run it. I create the spring-boot using Maven: The Project Structure the

like image 1000
voler Avatar asked Aug 06 '16 08:08

voler


People also ask

What is ApplicationArguments?

public interface ApplicationArguments. Provides access to the arguments that were used to run a SpringApplication .

How do you specify file path in application properties in spring boot?

properties in default location. Spring Boot loads the application. properties file automatically from the project classpath. All you have to do is to create a new file under the src/main/resources directory.

What does @SpringBootApplication will do?

Spring Boot @SpringBootApplication annotation is used to mark a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning. It's same as declaring a class with @Configuration, @EnableAutoConfiguration and @ComponentScan annotations.


2 Answers

In Spring Boot doc ApplicationArguments is autowired in a bean. Here is a more hands on example where it's used in a Main method.

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application implements ApplicationRunner {

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

    @Override
    public void run(ApplicationArguments args) throws Exception {
        boolean debug = args.containsOption("debug");
        List<String> files = args.getNonOptionArgs();
        System.out.println(debug);
        System.out.println(files);
    }
}
like image 100
a.b.d Avatar answered Nov 16 '22 01:11

a.b.d


Assuming that you have an Application class with annotation @SpringBootApplication like in the answer provided by a.b.d.

To be able to provide the arguments within IntelliJ IDEA environment you will need to first Run the main method and then Edit 'Run/Debug Configurations' and under Main Class fill Program arguments field with "--debug logfile.txt":

enter image description here

like image 23
harmonious Avatar answered Nov 16 '22 02:11

harmonious