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接口");
    
    	}
    
    }
    
  • 相关阅读:
    python
    HTTP和HTTPS协议,详解
    常用加密算法之非对称加密算法
    Docker容器数据卷-Volume详解
    使用nsenter进入docker容器后端报错 mesg: ttyname failed: No such file or directory
    Docker 查看容器 IP 地址
    Docker容器数据卷volumes-from
    Docker 进入容器的4种方法
    Jmeter之Bean shell使用(二)
    Jmeter之Bean shell使用(一)
  • 原文地址:https://www.cnblogs.com/KylinBlog/p/13527669.html
Copyright © 2011-2022 走看看