Giter Site home page Giter Site logo

java-springboot2-console-application's Introduction

Command Line Application

How to run

/gradlew bootRun -Pargs=--spring.main.banner-mode=off,--customArgument=yrdyii,--dateFrom=2018-07-01,--dateTo=2018-08-02

Spring Boot provides two interfaces, CommandLineRunner and ApplicationRunner to run some logic after the application context is loaded.

CommandLineRunner

Provides access to arguments as string array.

@Component
public class AppCommandLineRunner implements CommandLineRunner {
    private static final Logger log = LoggerFactory.getLogger(AppCommandLineRunner.class);
    
    @Override
    public void run(String...args) throws Exception {
        log.info("Application started with arguments: {}", Arrays.toString(args));
    }
}

ApplicationRunner

ApplicationRunner wraps the raw application arguments and exposes the ApplicationArguments interface, which has many convinent methods to get arguments.

@Component
public class AppRunner implements ApplicationRunner {
    private static final Logger log = LoggerFactory.getLogger(AppRunner.class);
    
    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("Application started with option names : {}", args.getOptionNames());
    }
}

Passing Command Line arguments

Configure bootRun in order to run command-line arguments as follows:

bootRun {
	if (project.hasProperty('args')) {
		args project.args.split(',')
	}
}

Now you can pass arguments like this

./gradlew bootRun -Pargs=--spring.main.banner-mode=off,--customArgument=custom

Overriding system properties

F.ex. application.properties

server.port=8081

We can pass system

  ./gradlew bootRun -Dspring-boot.run.arguments=--server.port=8888

Spring Boot converts command-line arguments to properties and adds them as environment variables.

java-springboot2-console-application's People

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.