DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4
  • How Spring Boot Starters Integrate With Your Project
  • A Practical Guide to Creating a Spring Modulith Project
  • Structured Logging in Spring Boot 3.4 for Improved Logs

Trending

  • Designing for Sustainability: The Rise of Green Software
  • Proactive Security in Distributed Systems: A Developer’s Approach
  • My Favorite Interview Question
  • Endpoint Security Controls: Designing a Secure Endpoint Architecture, Part 2
  1. DZone
  2. Coding
  3. Frameworks
  4. Runners in Spring Boot

Runners in Spring Boot

Know about the Runners in Spring Boot and how to implement them.

By 
SrinivasulaReddy Varimadugu user avatar
SrinivasulaReddy Varimadugu
·
Updated Aug. 04, 21 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
10.4K Views

Join the DZone community and get the full member experience.

Join For Free

Runners is a java class/spring beans of Spring boot Application implement the XXXRunner(I) directly or indirectly, and its auto-executable component is called by Container.

Runner classes are used to deal with one-time executing logics and those logics will be executed where SpringApplication.run(-) is about to complete all of its startup activities.

There are 2 Types of Runners 

  1.  CommandLineRunner  
  2.  ApplicationRunner

CommandLineRunner

  • It's a legacy runner which was introduced in SpringBoot 1.0 version.
  • It has only one abstract method “run(String… args): void”.
  • It is a Functional Interface having only one abstract method i.e "void run(String… args)".
  • Add one stereotype Annotation over Implementation class level (Ex:- @Component). So that container can detect the class and create the object.

ApplicationRunner(I) 

  • It is a new type of runner added in Spring boot 1.3 which makes it easy to access arguments.
  • This will separate Option Arguments (as Map<String, List<String>>) and Non-Option Arguments (<List<String>)

NOTE:--

  1. If Spring Boot Application contains multiple runners like AlertRunner, EmailRunner, SecurityRunner, CloudEnvRunner, DatabaseRunner, etc…
  2. Spring Boot provides default execution order. i.e ApplicationRunner implemented classes will execute first based on alphabetical order(A-Z) after that CommandLineArgument implemented classes will be executed based on alphabetical order.

In case If the programmer wants to specify custom order then use the following 

i)  Interface: Ordered

Example:

Java
 
@Component
public class CustomApplicationRunner implements ApplicationRunner,Ordered {

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("CustomApplicationRunner.run() arguments");
    }

    @Override
    public int getOrder() {
        return -15;
    }

}

ii) Annotation: @Order

Example:

Java
 
@Component
@Order(-1)
public class AlertCammandLineRunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        System.out.println("AlertCammandLineRunner.run() arguments");
    }
}

Here if the Order value is high then the priority is low, if the Order Value is low then priority is high

Note: Negative numbers are also allowed.

Note: If You added both @Order and Ordered(I)  then the Ordered interface getOrder() return value will be taken as the priority value.

Example: 

Java
 
@Component
@Order(12)
public class CustomApplicationRunner implements ApplicationRunner,Ordered {

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("CustomApplicationRunner.run() arguments");
    }

    @Override
    public int getOrder() {
        // TODO Auto-generated method stub
        return -15;
    }

}

For the above example, the priority value is -15

Please refer to the attached Image for Runners Example.

How To Pass Data to Runners:

We Programmers can pass data using Command Line Arguments, in two formats Option and Non-option arguments.

Syntax:

--Key  = Val for optional arguments.

value for non Optional arguments.

Example's for Optional arguments:

--db=oracle, --env=prod

Example's for NonOptional arguments:

Test clean execute etc...

Arguments data will be converted to String[] and send to CommandLineRunner Classes. We can access the data based on index format.

Difference Between CommandLineRunner and ApplicationRunner

The working of both CommandLineRunner and ApplicationRunner processes is the same, but CommandLineRunner holds the data in the String[] format whereas Application (AR) holds data as ApllicationArguments as Option/Non-Option format.

Spring Framework Spring Boot

Opinions expressed by DZone contributors are their own.

Related

  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4
  • How Spring Boot Starters Integrate With Your Project
  • A Practical Guide to Creating a Spring Modulith Project
  • Structured Logging in Spring Boot 3.4 for Improved Logs

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!