zoukankan      html  css  js  c++  java
  • Spring Boot 启动时,让方法自动执行的 4 种方法!

    1、实现ServletContextAware接口并重写其setServletContext方法

    @Component
    public class TestStarted implements ServletContextAware {
        /**
         * 在填充普通bean属性之后但在初始化之前调用
         * 类似于initializingbean的afterpropertiesset或自定义init方法的回调
         *
         */
        @Override
        public void setServletContext(ServletContext servletContext) {
            System.out.println("setServletContext方法");
        }
    }

    注意:该方法会在填充完普通Bean的属性,但是还没有进行Bean的初始化之前执行。

    2、实现ServletContextListener接口

    /**
    * 在初始化Web应用程序中的任何过滤器或servlet之前,将通知所有servletContextListener上下文初始化。
    */
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        //ServletContext servletContext = sce.getServletContext();
        System.out.println("执行contextInitialized方法");
    }

    3、将要执行的方法所在的类交个spring容器扫描(@Component),并且在要执行的方法上添加@PostConstruct注解或者静态代码块执行

    @Component
    public class Test2 {
        //静态代码块会在依赖注入后自动执行,并优先执行
        static{
            System.out.println("---static--");
        }
        /**
         *  @Postcontruct’在依赖注入完成后自动调用
         */
        @PostConstruct
        public static void haha(){
            System.out.println("@Postcontruct’在依赖注入完成后自动调用");
        }
    }

    4、实现ApplicationRunner接口

    /**
    * 用于指示bean包含在SpringApplication中时应运行的接口。可以定义多个applicationrunner bean
    * 在同一应用程序上下文中,可以使用有序接口或@order注释对其进行排序。
    */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("ApplicationRunner的run方法");
    }

    5、实现CommandLineRunner接口

    /**
    * 用于指示bean包含在SpringApplication中时应运行的接口。可以在同一应用程序上下文中定义多个commandlinerunner bean,并且可以使用有序接口或@order注释对其进行排序。
    * 如果需要访问applicationArguments而不是原始字符串数组,请考虑使用applicationrunner。
    *
    */
    @Override
    public void run(String... ) throws Exception {
        System.out.println("CommandLineRunner的run方法");
    }
  • 相关阅读:
    luogu1117 优秀的拆分 (后缀数组)
    hdu5238 calculator (线段树+crt)
    [模板]中国剩余定理/扩展中国剩余定理
    [模板]欧几里得算法/扩展欧几里得
    cf1088E Ehab and a component choosing problem (树形dp)
    cf1088D Ehab and another another xor problem (构造)
    cf1088C Ehab and a 2-operation task (构造)
    luogu3292 幸运数字 (点分治+线性基)
    2017-03-10<Git版本回退>
    2017-03-09<AS目录结构>
  • 原文地址:https://www.cnblogs.com/zhaochunhui/p/14700371.html
Copyright © 2011-2022 走看看