CommandLineRunner Interface

CommandLineRunner Interface:

⚫It is interface.
⚫It is provided by the Spring Framework.


package org.springframework.boot;

@FunctionalInterface
public interface CommandLineRunner extends Runner {
void run(String... args) throws Exception;
}
package org.springframework.boot;

interface Runner {
}

Example I:

CommandLineRunnerInterfaceIApplication.java
package com.unfaithful.commandlinerunner;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;


@SpringBootApplication
public class CommandLineRunnerInterfaceIApplication {

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


@Bean
public CommandLineRunner commandLineRunner(String[] args) {

return runner -> { //java lambda expression
System.out.println("CommandLineRunner is executed after the Spring Boot application has fully started.");
};
}
}
EnglishDictionary.java
package com.unfaithful.commandlinerunner;

import org.springframework.stereotype.Component;

@Component
public class EnglishDictionary {
public EnglishDictionary() {
System.out.println("EnglishDictionary: Constructor");
}
}
Output:
2024-04-18T07:54:59.303+03:00  INFO 14916 --- [CommandLineRunner Interface I] [           main] c.CommandLineRunnerInterfaceIApplication : Starting CommandLineRunnerInterfaceIApplication using Java 21.0.1 with PID 14916 (E:\Computer Science\Java\Spring Boot\Spring Boot Extra\CommandLineRunner Interface I\target\classes started by Gürkan Gökmen in E:\Computer Science\Java\Spring Boot\Spring Boot Extra\CommandLineRunner Interface I)
2024-04-18T07:54:59.309+03:00 INFO 14916 --- [CommandLineRunner Interface I] [ main] c.CommandLineRunnerInterfaceIApplication : No active profile set, falling back to 1 default profile: "default"
2024-04-18T07:55:01.156+03:00 INFO 14916 --- [CommandLineRunner Interface I] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http)
2024-04-18T07:55:01.180+03:00 INFO 14916 --- [CommandLineRunner Interface I] [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2024-04-18T07:55:01.181+03:00 INFO 14916 --- [CommandLineRunner Interface I] [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.19]
2024-04-18T07:55:01.275+03:00 INFO 14916 --- [CommandLineRunner Interface I] [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2024-04-18T07:55:01.277+03:00 INFO 14916 --- [CommandLineRunner Interface I] [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1850 ms
EnglishDictionary: Constructor
2024-04-18T07:55:01.838+03:00 INFO 14916 --- [CommandLineRunner Interface I] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path ''
2024-04-18T07:55:01.854+03:00 INFO 14916 --- [CommandLineRunner Interface I] [ main] c.CommandLineRunnerInterfaceIApplication : Started CommandLineRunnerInterfaceIApplication in 3.387 seconds (process running for 4.344)
CommandLineRunner is executed after the Spring Boot application has fully started.

Example II:

CommandLineRunnerInterfaceIiApplication.java
package com.unfaithful.commandlinerunner;

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

@SpringBootApplication
public class CommandLineRunnerInterfaceIiApplication {

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

}
EnglishDictionary.java
package com.unfaithful.commandlinerunner;

import org.springframework.stereotype.Component;

@Component
public class EnglishDictionary {
public EnglishDictionary() {
System.out.println("EnglishDictionary: Constructor");
}
}
MyCommandLineRunner.java
package com.unfaithful.commandlinerunner;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class MyCommandLineRunner implements CommandLineRunner {

@Override
public void run(String... args) throws Exception {
System.out.println("CommandLineRunner is executed after the Spring Boot application has fully started.");
}
}
Output:
2024-04-18T08:12:45.265+03:00  INFO 12544 --- [CommandLineRunner Interface II] [           main] .CommandLineRunnerInterfaceIiApplication : Starting CommandLineRunnerInterfaceIiApplication using Java 21.0.1 with PID 12544 (E:\Computer Science\Java\Spring Boot\Spring Boot Extra\CommandLineRunner Interface II\target\classes started by Gürkan Gökmen in E:\Computer Science\Java\Spring Boot\Spring Boot Extra\CommandLineRunner Interface II)
2024-04-18T08:12:45.271+03:00 INFO 12544 --- [CommandLineRunner Interface II] [ main] .CommandLineRunnerInterfaceIiApplication : No active profile set, falling back to 1 default profile: "default"
2024-04-18T08:12:47.140+03:00 INFO 12544 --- [CommandLineRunner Interface II] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http)
2024-04-18T08:12:47.159+03:00 INFO 12544 --- [CommandLineRunner Interface II] [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2024-04-18T08:12:47.160+03:00 INFO 12544 --- [CommandLineRunner Interface II] [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.19]
2024-04-18T08:12:47.255+03:00 INFO 12544 --- [CommandLineRunner Interface II] [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2024-04-18T08:12:47.257+03:00 INFO 12544 --- [CommandLineRunner Interface II] [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1882 ms
EnglishDictionary: Constructor
2024-04-18T08:12:47.816+03:00 INFO 12544 --- [CommandLineRunner Interface II] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path ''
2024-04-18T08:12:47.840+03:00 INFO 12544 --- [CommandLineRunner Interface II] [ main] .CommandLineRunnerInterfaceIiApplication : Started CommandLineRunnerInterfaceIiApplication in 3.275 seconds (process running for 3.945)
CommandLineRunner is executed after the Spring Boot application has fully started.






































































Comments