zoukankan      html  css  js  c++  java
  • SpringBoot启动时加载方法

    方式一:实现ServletContextListener接口

          @Component
          public class SpringBootInitialization1 implements ServletContextListener {
                @Override
    	    public void contextInitialized(ServletContextEvent sce) {
    		System.out.println("方式一:实现ServletContextListener接口");
    	    }
          }
    

    方式二:方法上加注解@PostConstruct

          @Component
          public class SpringBootInitialization2 {
                @PostConstruct
                public static void init() {
    		System.out.println("方式二:方法上加注解@PostConstruct");
                }
    
          }
    

    方式三:实现ServletContextAware接口

    @Component
    public class SpringBootInitialization3 implements ServletContextAware {
    
    	@Override
    	public void setServletContext(ServletContext servletContext) {
    		System.out.println("方式三:实现ServletContextAware接口");
    	}
    
    }
    

    方式四:实现ApplicationListener接口

    @Component
    public class SpringBootInitialization4 implements ApplicationListener<ContextRefreshedEvent> {
    
    	@Override
    	public void onApplicationEvent(ContextRefreshedEvent event) {
    		System.out.println("方式四:实现ApplicationListener<ContextRefreshedEvent>接口");
    	}
    
    }
    

    方式五:实现ApplicationRunner接口

    @Component
    public class SpringBootInitialization5 implements ApplicationRunner {
    
    	@Override
    	public void run(ApplicationArguments args) throws Exception {
    		System.out.println("方式五:实现ApplicationRunner接口");
    	}
    
    }
    

    方式六:实现CommandLineRunner接口

    @Component
    public class SpringBootInitialization6 implements CommandLineRunner {
    
    	@Override
    	public void run(String... args) throws Exception {
    		System.out.println("方式六:实现CommandLineRunner接口");
    
    	}
    
    }
    
  • 相关阅读:
    用VSTS进行网站压力测试
    紧急求助!powerdesigner 12的问题!
    决定你是富人还是穷人的12法则(转)
    LINQ to SQL公共基类
    Web.config详解(转)
    向iframe中的页面传递参数
    [转]mysql 乱码问题解决终结
    [转]mysql多次调用存储过程的问题
    未能加载类型“System.Web.Mvc.ViewPage<String>”
    javascript之典型高阶函数
  • 原文地址:https://www.cnblogs.com/KylinBlog/p/13527669.html
Copyright © 2011-2022 走看看