GVKun编程网logo

atom-grunt-runner 的配置(atom launcher)

16

对于想了解atom-grunt-runner的配置的读者,本文将提供新的信息,我们将详细介绍atomlauncher,并且为您提供关于CommandLineRunner与ApplicationRunn

对于想了解atom-grunt-runner 的配置的读者,本文将提供新的信息,我们将详细介绍atom launcher,并且为您提供关于CommandLineRunner 与 ApplicationRunner 接口的使用及源码解析、CommandLineRunner与ApplicationRunner的介绍、CommandLineRunner与ApplicationRunner的介绍ApplicationRunner、CommandLineRunner和ApplicationRunner的有价值信息。

本文目录一览:

atom-grunt-runner 的配置(atom launcher)

atom-grunt-runner 的配置(atom launcher)

在用 Atom 写项目,同时用到 grunt 做自动化,当然就想到在 atom 直接调用 grunt,有一个现成的 atom plugin 叫 grunt-runner,安装后,需要配置一下,而且对于 mac 最新的 OS X 10.10 还需要特别注意:

Find where grunt is in the Terminal:

$ which grunt /usr/local/bin/grunt Add /usr/local/bin to Grunt Paths in the grunt-runner settings.

If no path is returned after the which, you have to install Grunt globally through npm

Troubleshooting For Yosemite (OS X 10.10)

If not launched from command-line Atom currently has a bug where it''s unable to loads OS X''s PATH due to a change in the way Yosemite functions.

You may receive the following error:

Uncaught BufferedProcessError: Failed to spawn command grunt. Make sure grunt is installed and on your PATH

One workaround is to add the following line:

process.env.PATH = ["/usr/local/bin", process.env.PATH].join(":")

To the file:

~/.atom/init.coffee

CommandLineRunner 与 ApplicationRunner 接口的使用及源码解析

CommandLineRunner 与 ApplicationRunner 接口的使用及源码解析

引言

我们在使用 SpringBoot 搭建项目的时候,如果希望在项目启动完成之前,能够初始化一些操作,针对这种需求,可以考虑实现如下两个接口(任一个都可以)

org.springframework.boot.CommandLineRunnerorg.springframework.boot.ApplicationRunner

CommandLineRunner、ApplicationRunner 接口是在容器启动成功后的最后一步回调(类似开机自启动)。


CommandLineRunner 接口


官方 doc:Interface used to indicate that a bean should run when it is contained within a SpringApplication. Multiple CommandLineRunner beans can be defined within the same application context and can be ordered using the Ordered interface or Order @Order annotation.


接口被用作将其加入 spring 容器中时执行其 run 方法。多个 CommandLineRunner 可以被同时执行在同一个 spring 上下文中并且执行顺序是以 order 注解的参数顺序一致。


If you need access to ApplicationArguments instead of the raw String array consider using ApplicationRunner.


如果你需要访问 ApplicationArguments 去替换掉字符串数组,可以考虑使用 ApplicationRunner 类。


实例 demo

定义一个 ServerStartedReport 实现 CommandLineRunner,并纳入到 srping 容器中进行处理


import org.springframework.boot.CommandLineRunner;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;import java.time.LocalDateTime;
@Order(2)@Componentpublic class ServerStartedReport implements CommandLineRunner{ @Override public void run(String... args) throws Exception { System.out.println("===========ServerStartedReport启动====="+ LocalDateTime.now()); }}

定义一个 ServerSuccessReport 实现 CommandLineRunner,并纳入到 spring 容器处理

import org.springframework.boot.CommandLineRunner;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;import java.util.Arrays;
@Order(1)@Componentpublic class ServerSuccessReport implements CommandLineRunner{ @Override public void run(String... args) throws Exception { System.out.println("=====应用已经成功启动====="+ Arrays.asList(args)); }}

启动类测试,也可以直接在 spring 容器访问该值


import org.springframework.boot.ApplicationArguments;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplicationpublic class Application { public static void main(String[] args) { ConfigurableApplicationContext context =SpringApplication.run(Application.class,args); ApplicationArguments applicationArguments = context.getBean(ApplicationArguments.class); System.out.println("============"); System.out.println("name="+applicationArguments.getOptionNames()); System.out.println("values===="+applicationArguments.getOptionValues("developer.name")); }}


配置参数,然后执行启动类


打印结果



ApplicationRunner 接口



发现二者的官方 javadoc 一样,区别在于接收的参数不一样。CommandLineRunner的参数是最原始的参数,没有做任何处理。ApplicationRunner的参数是ApplicationArguments,是对原始参数做了进一步的封装。


ApplicationArguments是对参数(main 方法)做了进一步的处理,可以解析 --name=value 的,我们就可以通过 name 来获取 value(而 CommandLineRunner 只是获取 --name=value)



可以接收--foo=bar这样的参数。


  • getOptionNames () 方法可以得到 foo 这样的 key 的集合。

  • getOptionValues (String name) 方法可以得到 bar 这样的集合的 value。


实例 demo


定义 MyApplicationRunner 类继承 ApplicationRunner 接口

import org.springframework.boot.ApplicationArguments;import org.springframework.boot.ApplicationRunner;import org.springframework.stereotype.Component;import java.util.Arrays;
@Componentpublic class MyApplicationRunner implements ApplicationRunner{ @Override public void run(ApplicationArguments args) throws Exception { System.out.println("===MyApplicationRunner==="+ Arrays.asList(args.getSourceArgs())); System.out.println("===getOptionNames========"+args.getOptionNames()); System.out.println("===getOptionValues======="+args.getOptionValues("foo")); System.out.println("==getOptionValues========"+args.getOptionValues("developer.name")); }}

启动类

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); }}

配置参数启动


打印结果



总结

用户使用 CommandLineRunner 或者 ApplicationRunner 接口均可实现应用启动初始化某些功能的需求,如果希望对参数有更多的操作,则可以选择实现 ApplicationRunner 接口。


扩展阅读 CommandLineRunner、ApplicationRunner 执行流程源码分析

用户只要实现这两个接口,其中的 run 方法就会在项目启动时候被自动调用,那么究竟是在什么时候调用的呢?下面可以看一下 Application 的启动流程

SpringApplication.run(args)




this.afterRefresh (context, applicationArguments) 方法



跟踪 context.getBeansOfType()方法,具体实现在类 DefaultListableBeanFactory 中





总结:通过以上分析可知,实现这两个接口的类,在ApplicationContext.run()方法里被执行。




本文分享自微信公众号 - 猿天地(cxytiandi)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与 “OSC 源创计划”,欢迎正在阅读的你也加入,一起分享。

CommandLineRunner与ApplicationRunner的介绍

CommandLineRunner与ApplicationRunner的介绍

本篇文章给大家带来的内容是关于commandlinerunner与applicationrunner的介绍,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

CommandLineRunner和ApplicationRunner是Spring Boot所提供的接口,他们都有一个run()方法。所有实现他们的Bean都会在Spring Boot服务启动之后自动地被调用。

由于这个特性,它们是一个理想地方去做一些初始化的工作,或者写一些测试代码。

CommandLineRunner

使用Application实现

在我们新建好工程后,为了简单我们直接使用Application类实现CommandLineRunner接口,这个类的注解@SpringBootApplication会为我们自动配置。

package cn.examplecode.sb2runner;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Sb2runnerApplication implements CommandLineRunner {

    private static Logger logger = LoggerFactory.getLogger(Sb2runnerApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(Sb2runnerApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        logger.info("服务已启动,执行command line runner。");

        for (int i = 0; i <p>接下来我们直接启动服务,查看日志如下,发现run()方法被正常地执行了:</p><pre>Tomcat started on port(s): 8080 (http) with context path ''''
Started Sb2runnerApplication in 2.204 seconds (JVM running for 3.161)
服务已启动,执行command line runner。
登录后复制

参数传递

run()方法有个可变参数args,这个参数是用来接收命令行参数的,我们下面来加入参数来测试一下:

3441260030-5c942465b95bc_articlex.jpg

然后重启服务,观察日志,可以看到参数被正常地接收到了:

Tomcat started on port(s): 8080 (http) with context path ''''
Started Sb2runnerApplication in 1.888 seconds (JVM running for 2.41)
服务已启动,执行command line runner。
args[0]: --param=sth
登录后复制

命令行参数传递

之前我们说过使用Spring Boot的一大优势就是可以将工程直接打包成一个jar包而不需要单独部署。打包成jar包后可以直接执行该jar包进行服务的启动,这样在执行jar包时我们就可以传入命令行参数,让CommandLineRunner接收参数。

这种场景在服务器上特别常用。比如我们想执行某个操作,又不想对外部暴露,此时可以使用CommandLineRunner作为该操作的入口。

下面我们就打成jar包来演示一下。

  1. 进入终端界面,开始打包

690739553-5c9424659ebfa_articlex.jpg

  1. 打包完成后,执行该jar包,记得先把IDE的服务停掉。

3864140867-5c942466cc43c_articlex.jpg

可以从日志中看到我们也正常地获取到了参数。通过传递参数,在业务逻辑上我们可以根据不同的参数而执行不同的操作。

上面我们提到的只是一个CommandLineRunner,如果我们有多个CommandLineRunner怎么办呢?怎么控制它们执行的顺序呢?

下面我们就来介绍如何指定执行的顺序。

指定执行顺序

Spring Boot为我们提供了一个注解"@Order",可以用来指定执行的顺序,比如我们工程里面有三个CommandLineRunner:

@Component
@Order(1)
public class CommandRunner1 implements CommandLineRunner {

    private static Logger logger = LoggerFactory.getLogger(CommandRunner1.class);

    @Override
    public void run(String... args) throws Exception {
        logger.info("执行第一个command line runner...");
    }

}


@Component
@Order(2)
public class CommandRunner2 implements CommandLineRunner {

    private static Logger logger = LoggerFactory.getLogger(CommandRunner2.class);

    @Override
    public void run(String... args) throws Exception {
        logger.info("执行第二个command line runner...");
    }
    
}

@Component
@Order(3)
public class CommandRunner3 implements CommandLineRunner {

    private static Logger logger = LoggerFactory.getLogger(CommandRunner3.class);

    @Override
    public void run(String... args) throws Exception {
        logger.info("执行第三个command line runner...");
    }
    
}
登录后复制

我们可以在该类的上面直接加入@Order注解,然后Spring Boot就会按照我们注解指定的顺序从小到大的执行了。很简单,是不是?

Tomcat started on port(s): 8080 (http) with context path ''''
Started Sb2runnerApplication in 1.764 seconds (JVM running for 2.292)
执行第一个command line runner...
执行第二个command line runner...
执行第三个command line runner...
登录后复制

ApplicationRunner

ApplicationRunner与CommandLineRunner做的事情是一样的,也是在服务启动之后其run()方法会被自动地调用,唯一不同的是ApplicationRunner会封装命令行参数,可以很方便地获取到命令行参数和参数值。

@Component
public class ApplicationRunner1 implements ApplicationRunner {

    private static Logger logger = LoggerFactory.getLogger(ApplicationRunner1.class);

    @Override
    public void run(ApplicationArguments args) throws Exception {
        logger.info("执行application runner...");
        logger.info("获取到参数: " + args.getOptionValues("param"));
    }
}
登录后复制

执行结果:

3467155383-5c9424659a73f_articlex.jpg

我们可以发现,通过run()方法的参数ApplicationArguments可以很方便地获取到命令行参数的值。

所以如果你的工程需要获取命令行参数的话,建议你使用ApplicationRunner。

总结

无论是CommandLineRunner还是ApplicationRunner,它们的目的都是在服务启动之后执行一些操作。如果需要获取命令行参数时则建议使用ApplicationRunner。

另一种场景是我们在服务器上需要执行某个操作,比如修正数据库用户的数据,而又找不到合适的执行入口,那么这就是它们理想的使用场景了。

以上就是CommandLineRunner与ApplicationRunner的介绍的详细内容,更多请关注php中文网其它相关文章!

CommandLineRunner与ApplicationRunner的介绍ApplicationRunner

CommandLineRunner与ApplicationRunner的介绍ApplicationRunner

本篇文章给大家带来的内容是关于CommandLineRunner与ApplicationRunner的介绍,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

CommandLineRunner和ApplicationRunner是Spring Boot所提供的接口,他们都有一个run()方法。所有实现他们的Bean都会在Spring Boot服务启动之后自动地被调用。

由于这个特性,它们是一个理想地方去做一些初始化的工作,或者写一些测试代码。

CommandLineRunner

使用Application实现

在我们新建好工程后,为了简单我们直接使用Application类实现CommandLineRunner接口,这个类的注解@SpringBootApplication会为我们自动配置。

package cn.examplecode.sb2runner;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Sb2runnerApplication implements CommandLineRunner {

    private static Logger logger = LoggerFactory.getLogger(Sb2runnerApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(Sb2runnerApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        logger.info(服务已启动,执行command line runner。);

        for (int i = 0; i < args.length; ++i) {
            logger.info(args[{}]: {}, i, args[i]);
        }
    }
}

接下来我们直接启动服务,查看日志如下,发现run()方法被正常地执行了:

Tomcat started on port(s): 8080 (http) with context path ''
Started Sb2runnerApplication in 2.204 seconds (JVM running for 3.161)
服务已启动,执行command line runner。

参数传递

run()方法有个可变参数args,这个参数是用来接收命令行参数的,我们下面来加入参数来测试一下:

3441260030-5c942465b95bc_articlex.jpg

然后重启服务,观察日志,可以看到参数被正常地接收到了:

Tomcat started on port(s): 8080 (http) with context path ''
Started Sb2runnerApplication in 1.888 seconds (JVM running for 2.41)
服务已启动,执行command line runner。
args[0]: --param=sth

命令行参数传递

之前我们说过使用Spring Boot的一大优势就是可以将工程直接打包成一个jar包而不需要单独部署。打包成jar包后可以直接执行该jar包进行服务的启动,这样在执行jar包时我们就可以传入命令行参数,让CommandLineRunner接收参数。

这种场景在服务器上特别常用。比如我们想执行某个操作,又不想对外部暴露,此时可以使用CommandLineRunner作为该操作的入口。

下面我们就打成jar包来演示一下。

  1. 进入终端界面,开始打包

690739553-5c9424659ebfa_articlex.jpg

  1. 打包完成后,执行该jar包,记得先把IDE的服务停掉。

3864140867-5c942466cc43c_articlex.jpg

可以从日志中看到我们也正常地获取到了参数。通过传递参数,在业务逻辑上我们可以根据不同的参数而执行不同的操作。

上面我们提到的只是一个CommandLineRunner,如果我们有多个CommandLineRunner怎么办呢?怎么控制它们执行的顺序呢?

下面我们就来介绍如何指定执行的顺序。

指定执行顺序

Spring Boot为我们提供了一个注解@Order,可以用来指定执行的顺序,比如我们工程里面有三个CommandLineRunner:

@Component
@Order(1)
public class CommandRunner1 implements CommandLineRunner {

    private static Logger logger = LoggerFactory.getLogger(CommandRunner1.class);

    @Override
    public void run(String... args) throws Exception {
        logger.info(执行第一个command line runner...);
    }

}


@Component
@Order(2)
public class CommandRunner2 implements CommandLineRunner {

    private static Logger logger = LoggerFactory.getLogger(CommandRunner2.class);

    @Override
    public void run(String... args) throws Exception {
        logger.info(执行第二个command line runner...);
    }
    
}

@Component
@Order(3)
public class CommandRunner3 implements CommandLineRunner {

    private static Logger logger = LoggerFactory.getLogger(CommandRunner3.class);

    @Override
    public void run(String... args) throws Exception {
        logger.info(执行第三个command line runner...);
    }
    
}

我们可以在该类的上面直接加入@Order注解,然后Spring Boot就会按照我们注解指定的顺序从小到大的执行了。很简单,是不是?

Tomcat started on port(s): 8080 (http) with context path ''
Started Sb2runnerApplication in 1.764 seconds (JVM running for 2.292)
执行第一个command line runner...
执行第二个command line runner...
执行第三个command line runner...

ApplicationRunner

ApplicationRunner与CommandLineRunner做的事情是一样的,也是在服务启动之后其run()方法会被自动地调用,唯一不同的是ApplicationRunner会封装命令行参数,可以很方便地获取到命令行参数和参数值。

@Component
public class ApplicationRunner1 implements ApplicationRunner {

    private static Logger logger = LoggerFactory.getLogger(ApplicationRunner1.class);

    @Override
    public void run(ApplicationArguments args) throws Exception {
        logger.info(执行application runner...);
        logger.info(获取到参数:  + args.getoptionValues(param));
    }
}

执行结果:

3467155383-5c9424659a73f_articlex.jpg

我们可以发现,通过run()方法的参数ApplicationArguments可以很方便地获取到命令行参数的值。

所以如果你的工程需要获取命令行参数的话,建议你使用ApplicationRunner。

总结

无论是CommandLineRunner还是ApplicationRunner,它们的目的都是在服务启动之后执行一些操作。如果需要获取命令行参数时则建议使用ApplicationRunner。

另一种场景是我们在服务器上需要执行某个操作,比如修正数据库用户的数据,而又找不到合适的执行入口,那么这就是它们理想的使用场景了。

CommandLineRunner和ApplicationRunner

CommandLineRunner和ApplicationRunner

使用场景

我们在开发过程中会有这样的场景:需要在容器启动的时候执行一些内容,比如:读取配置文件信息,数据库连接,删除临时文件,清除缓存信息,在Spring框架下是通过ApplicationListener监听器来实现的。在Spring Boot中给我们提供了两个接口来帮助我们实现这样的需求。这两个接口就是我们今天要讲的CommandLineRunner和ApplicationRunner,他们的执行时机为容器启动完成的时候。

不同点

共同点:其一执行时机都是在容器启动完成的时候进行执行;其二这两个接口中都有一个run()方法;

不同点:ApplicationRunner中run方法的参数为ApplicationArguments,而CommandLineRunner接口中run方法的参数为String数组。

官方说明:

  • Interface used to indicate that a bean should run when it is contained within   
  • a SpringApplication. Multiple CommandLineRunner beans can be defined   
  • within the same application context and can be ordered using the Ordered   
  • interface or @Order annotation.   
  • If you need access to ApplicationArguments instead of the raw String array consider using ApplicationRunner. 

使用Ordered接口或者Order注解修改执行顺序

问题提出: 如果有多个实现类,而我们需要按照一定的顺序执行的话,那么应该怎么办呢?

解决方案:其一可以在实现类上加上@Order注解指定执行的顺序;其二可以在实现类上实现Ordered接口来标识。

需要注意:数字越小,优先级越高,也就是@Order(1)注解的类会在@Order(2)注解的类之前执行。

 

今天的关于atom-grunt-runner 的配置atom launcher的分享已经结束,谢谢您的关注,如果想了解更多关于CommandLineRunner 与 ApplicationRunner 接口的使用及源码解析、CommandLineRunner与ApplicationRunner的介绍、CommandLineRunner与ApplicationRunner的介绍ApplicationRunner、CommandLineRunner和ApplicationRunner的相关知识,请在本站进行查询。

本文标签:

上一篇Ubuntu 安装 Docker CE(ubuntu安装dockerce)

下一篇nginx for ubuntu