Вы находитесь на странице: 1из 2

Date : 29/04/2020 (Spring Boot 6PM)

Spring Boot Runners


------------------------------------------------------------------------------
Duration : 1:15Min 1:30 min
Start timing : 6:10 PM
Payment and Queries :+91- 6302 968 665 (Srikanth)

javabyraghu@gmail.com
https://www.facebook.com/groups/thejavatemple/
------------------------------------------------------------------------

Runners : These are used to execute any logic only one time
while starting application.

Ex: Database Connection (or) Connection Pool,


Security Setup logic, Template Object(HibernateTemplate/JdbcTemplate),
EmailConfig ..etc

=> Any java Logic we can write in Runner


=> This logic executed one time only
=> This logic executed while starting application.
=> This is auto-called by Starter.
**) Runners are interfaces. Those are two types:
a. CommandLineRunner (I)
b. ApplicationRunner (I)

============================================================
a. CommandLineRunner (I) :
This is a functional Interface [Interface contains only one abstract method].
It is added in Spring boot 1.0 Version.
it is having abstract method => run(String... args):void

*) For this interface, programmer has to define child class


*) For our class object created by Spring container
*) Executed/called by Starter class

*)Note:
@Component : is a StereoType Annotation, this annotation indicates to
container create object to current class.

--Steps for Coding ------------------


i. Write one public class
ii. implement interface CommandLineRunner
iii. Override method run(..) and provide logic
iv. Add @Component on top of class
----Eclipse(STS) Shortcuts--------------------------------
ctrl+shift+O => To get imports
ctrl+space => for possible code generation

type:sout => It will generate System.out.println


press: ctrl+space

ctrl+F11 => To run main method class/execute code


-----------Example Application-----------------------------------------------
#1. Create new Project
> File > new > Spring Starter project
> Enter Details
name : SpringBootRunnerExOne
groupId : in.nit
version : 1.0
package : in.nit

#2. Create new class under src/main/java folder


> Right click on src/main/java folder > new > class
> Enter details
package : in.nit.runner
Name : MyMessageRunner
> Finish
---
package in.nit.runner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
//ctrl+shift+O(imports)
@Component //=>Tell to container Creating object
public class MyMessageRunner
implements CommandLineRunner
{

//ctrl+space > choose run > press enter


@Override
public void run(String... args) throws Exception {
// syout > ctrl+space
System.out.println("Welcome to First Runner Ex!");
}
}

#3 Execute Code
> open starter class(class having main method)
> Right click on code > Run As > Spring Boot Application

** Check output on console


==============================================================
*)Note:
=> In Spring boot Project all our classes must be under
starter class package or its sub package, but not outside or different
is allowed.

Ex: Starter class(main method class) package : in.nit


RunnerClass#1 package : in.nit.abc (vaild and executed)
RunnerClass#2 package : in.nit (vaild and executed)
RunnerClass#3 package : nit.abc.in (Invaild and NotExecuted)
RunnerClass#4 package : nit.in (Invaild and NotExecuted)
RunnerClass#5 package : com.app (Invaild and NotExecuted)
RunnerClass#6 package : in.nit.abc.xyz.test.one (Vaild and Executed)
---------------------------------------------------------------------------

Вам также может понравиться