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


查看原文:https://www.huuinn.com/archives/385
更多技术干货:风匀坊
关注公众号:风匀坊
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值