spring boot定制个性化命令

前言

使用过lumen(php)框架的朋友,大概都接触过artisan工具。按框架所给过的规范,可以轻松定制自己业务所用到的命令集成到框架里。如下图汉字部分所示
lumen artisan
当想执行想要用到的业务命令时,只需要执行对应的命令

php artisan build.es.plazas.config //刷新全部广场配置

是不是很直观,那么在spring boot中,我们要如何实现类似功能呢?

实现步骤

  • 使用CommandLineRunner,由于CommandLineRunner#run(String… args) 在Application启动完成之前就已经被调用,通过子类重写方法,用execute方法替换run来实现。
  • 多条命令时,通过@ConditionalOnProperty(name = "command", havingValue = "xxx")来区分

代码范例

package com.ffan.apps;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Service;

@Slf4j
@SpringBootApplication
public class EchoApplication {

    public abstract static class Command implements CommandLineRunner {
        protected final Logger logger = LoggerFactory.getLogger(this.getClass());
        public final void run (final String... args) throws Exception {
            logger.info("Initialize started on {}", getClass());
            //CommandLineRunner#run(String... args) 在`Application`启动完成之前就已经被调用
            init(args);
        }

        void init(final String... args) throws Exception {

        }

        //子类中封装
        abstract void execute() throws Exception;
    }

    public static void main(final String... args) throws Exception {
        try (ConfigurableApplicationContext context = SpringApplication.run(EchoApplication.class, args)) {
            context.getBean(Command.class).execute();
        }
    }

    @Service
    @ConditionalOnProperty(name = "command", havingValue = "foo")
    public static class FooFeature extends Command {
        @Override
        void execute () throws Exception {
            logger.info("Foo");
        }
    }

    @Service
    @ConditionalOnProperty(name = "command", havingValue = "bar")
    public static class BarFeature extends Command {
        @Override
        void execute() throws Exception {
            logger.info("Bar");
        }
    }
}

执行结果

java -jar target/SpringLearning-1.0-SNAPSHOT.jar --command=foo
2017-10-27 23:37:39.208  INFO 1002 --- [           main] c.ffan.apps.EchoApplication$FooFeature   : Initialize started on class com.ffan.apps.EchoApplication$FooFeature
2017-10-27 23:37:39.216  INFO 1002 --- [           main] com.ffan.apps.EchoApplication            : Started EchoApplication in 8.651 seconds (JVM running for 9.724)
2017-10-27 23:37:39.217  INFO 1002 --- [           main] c.ffan.apps.EchoApplication$FooFeature   : Foo

java -jar target/SpringLearning-1.0-SNAPSHOT.jar --command=bar
2017-10-27 23:41:04.336  INFO 1165 --- [           main] c.ffan.apps.EchoApplication$BarFeature   : Initialize started on class com.ffan.apps.EchoApplication$BarFeature
2017-10-27 23:41:04.346  INFO 1165 --- [           main] com.ffan.apps.EchoApplication            : Started EchoApplication in 12.956 seconds (JVM running for 14.144)
2017-10-27 23:41:04.348  INFO 1165 --- [           main] c.ffan.apps.EchoApplication$BarFeature   : Bar

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注