com.jl.runner.JCommandLineRunner Maven / Gradle / Ivy
The newest version!
package com.jl.runner;
import com.jl.JReflect;
import com.jl.JTuple;
import com.jl.runner.annotation.JRunner;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.List;
/**
* 启动完成执行任务
*/
@Component
@AllArgsConstructor
public class JCommandLineRunner implements CommandLineRunner {
private ApplicationContext appContext;
@Override
@SneakyThrows
public void run(String... args) {
String[] beans = appContext.getBeanDefinitionNames();
for (String bean : beans) {
runner(appContext.getBean(bean));
}
}
public void runner(Object bean) {
Class> clazz = bean.getClass();
List> methods = JReflect.MethodReflect.getMethod(clazz, JRunner.class);
for (JTuple.Tuple2 methodObj : methods) {
Method method = methodObj.getV1();
JRunner jlRunner = methodObj.getV2();
Object[] value = jlRunner.value();
Object[] param = JReflect.MethodReflect.checkParamType(method.getParameters(), value);
try {
method.invoke(bean, param);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}