@functionalinterface
public interface commandlinerunner {
/**
* callback used to run the bean.
* @param args incoming main method arguments
* @throws exception on error
*/
void run(string... args) throws exception;
}
1.2 使用:
package com.example.consoleapplication;
import org.springframework.boot.commandlinerunner;
import org.springframework.stereotype.component;
@component
public class testrunner implements commandlinerunner {
@override
public void run(string... args) {
// do something...
for(string arg: args){
system.out.println(arg);
}
system.out.print("test command runner");
}
}
2019-03-16 17:31:56.544 info 18679 --- [ main] c.e.consoleapplication.demoapplication : no active profile set, falling back to default profiles: default
2019-03-16 17:31:57.195 info 18679 --- [ main] c.e.consoleapplication.demoapplication : started demoapplication in 16.172 seconds (jvm running for 16.65)
-sdfsaf
sdfas
test command runner%
2. applicationrunner
2.1 声明
/**
* interface used to indicate that a bean should <em>run</em> when it is contained within
* a {@link springapplication}. multiple {@link applicationrunner} beans can be defined
* within the same application context and can be ordered using the {@link ordered}
* interface or {@link order @order} annotation.
*
* @author phillip webb
* @since 1.3.0
* @see commandlinerunner
*/
@functionalinterface
public interface applicationrunner {
/**
* callback used to run the bean.
* @param args incoming application arguments
* @throws exception on error
*/
void run(applicationarguments args) throws exception;
}
2019-03-16 18:08:08.528 info 19778 --- [ main] c.e.consoleapplication.demoapplication : no active profile set, falling back to default profiles: default
2019-03-16 18:08:09.166 info 19778 --- [ main] c.e.consoleapplication.demoapplication : started demoapplication in 16.059 seconds (jvm running for 16.56)
test
option arg names[option]
non option+[-non1, non2]-non1
non2
--option=1
test%