zoukankan      html  css  js  c++  java
  • 计时器与启动加载器

    计时器

    import org.junit.jupiter.api.Test;
    import org.springframework.util.StopWatch;
    
    public class StopWatchDemo {
        @Test
        public void test() throws InterruptedException {
            StopWatch stopWatch = new StopWatch();
            stopWatch.start("task1");
            Thread.sleep(2000L);
            stopWatch.stop();
            stopWatch.start("task2");
            Thread.sleep(3000L);
            stopWatch.stop();
            stopWatch.start("task3");
            Thread.sleep(1000L);
            stopWatch.stop();
            System.out.println(stopWatch.prettyPrint());
        }
    }
    
    
    StopWatch '': running time = 6008888191 ns
    ---------------------------------------------
    ns         %     Task name
    ---------------------------------------------
    2004276665  033%  task1
    3003999368  050%  task2
    1000612158  017%  task3
    

    启动加载器

    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
    
    /**
     * 启动加载器
     * @see SpringApplication#callRunners(org.springframework.context.ApplicationContext, org.springframework.boot.ApplicationArguments)
     * {@link CommandLineRunner}
     */
    @Component
    @Order
    public class FirstCommandLineRunner implements CommandLineRunner {
    
        @Override
        public void run(String... args) throws Exception {
            System.out.println("u001B[32m First CommandLineRunner");
        }
    }
    
    
    import org.springframework.boot.ApplicationArguments;
    import org.springframework.boot.ApplicationRunner;
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
    
    /**
     * 启动加载器
     * {@link ApplicationRunner}
     */
    @Order
    @Component
    public class FirstApplicationRunner implements ApplicationRunner{
        @Override
        public void run(ApplicationArguments args) throws Exception {
            System.out.println("u001B[32m First ApplicationRunner");
        }
    }
    

    源码

    	private void callRunners(ApplicationContext context, ApplicationArguments args) {
    		List<Object> runners = new ArrayList<>();
    		runners.addAll(context.getBeansOfType(ApplicationRunner.class).values()); //判断类型
    		runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());
    		AnnotationAwareOrderComparator.sort(runners); //排序 
    		for (Object runner : new LinkedHashSet<>(runners)) {
    			if (runner instanceof ApplicationRunner) {   //ApplicationRunner先执行
    				callRunner((ApplicationRunner) runner, args);
    			}
    			if (runner instanceof CommandLineRunner) {
    				callRunner((CommandLineRunner) runner, args);
    			}
    		}
    	}
    

    getBeansOfType

    	@Override
    	public <T> Map<String, T> getBeansOfType(@Nullable Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)
    			throws BeansException {
    
    		assertBeanFactoryActive();
    		return getBeanFactory().getBeansOfType(type, includeNonSingletons, allowEagerInit);
    	}
    

    assertBeanFactoryActive

    	protected void assertBeanFactoryActive() {
    		if (!this.active.get()) {  //是在refreshContext->org.springframework.context.support.AbstractApplicationContext#prepareRefresh赋值的
    			if (this.closed.get()) {
    				throw new IllegalStateException(getDisplayName() + " has been closed already");
    			}
    			else {
    				throw new IllegalStateException(getDisplayName() + " has not been refreshed yet");
    			}
    		}
    	}
    
    org.springframework.context.support.AbstractApplicationContex#prepareRefresh
    protected void prepareRefresh() {
    		// Switch to active.
    		this.startupDate = System.currentTimeMillis();
    		this.closed.set(false);
    		this.active.set(true);
    

    ApplicationRunner对比CommandLineRunner

    • callRunner
            //ApplicationRunner
            private void callRunner(ApplicationRunner runner, ApplicationArguments args) {
    		try {
    			(runner).run(args);
    		}
    		catch (Exception ex) {
    			throw new IllegalStateException("Failed to execute ApplicationRunner", ex);
    		}
    	}
            //CommandLineRunner
    	private void callRunner(CommandLineRunner runner, ApplicationArguments args) {
    		try {
    			(runner).run(args.getSourceArgs());
    		}
    		catch (Exception ex) {
    			throw new IllegalStateException("Failed to execute CommandLineRunner", ex);
    		}
    	}
    

    ApplicationArguments初始化

    	ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
    
    //最后执行的是org.springframework.core.env.SimpleCommandLineArgsParser#parse
    public CommandLineArgs parse(String... args) {
    		CommandLineArgs commandLineArgs = new CommandLineArgs();
    		for (String arg : args) {
    			if (arg.startsWith("--")) {
    				String optionText = arg.substring(2);
    				String optionName;
    				String optionValue = null;
    				int indexOfEqualsSign = optionText.indexOf('=');
    				if (indexOfEqualsSign > -1) {
    					optionName = optionText.substring(0, indexOfEqualsSign);
    					optionValue = optionText.substring(indexOfEqualsSign + 1);
    				}
    				else {
    					optionName = optionText;
    				}
    				if (optionName.isEmpty()) {
    					throw new IllegalArgumentException("Invalid argument syntax: " + arg);
    				}
    				commandLineArgs.addOptionArg(optionName, optionValue);
    			}
    			else {
    				commandLineArgs.addNonOptionArg(arg);
    			}
    		}
    		return commandLineArgs;
    	}
    
  • 相关阅读:
    Dapper的基本 理论 知识
    路由
    WebForm+一般处理程序+Ajax聊天
    Jquer + Ajax 制作上传图片文件
    (3.4)表相关操作之完整性约束
    (3.3)表相关操作之数据类型
    (3.2)表相关操作之表的增删改查
    (3.1)表相关操作之存储引擎介绍
    常用模块
    模块与包
  • 原文地址:https://www.cnblogs.com/fly-book/p/12703437.html
Copyright © 2011-2022 走看看