方法概览
1-2 属于java自身的启动时加载方式 3-7属于spring的加载方式
-
static代码块
-
构造方法
-
@PostConstruct注解
-
ApplicationRunner
-
CommandLineRunner
-
实现ServletContextListener接口
-
ApplicationListener<ApplicationStartedEvent>
代码实现-都以启动服务端的servlet为demo
1.static
2.构造方法
3.TestPostConstruct
代码一起展示:注意加入component注解,不然可以自行测试一下,启动不会初始化:
package com.example.initmethoddemo;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* @Description: 第一种启动的方法
* @author: LiuYC
* @date: 2021年04月23日 10:16
*/
@Component
public class TestPostConstruct {
//way 1 静态代码块
static {
System.out.println("static");
}
// way 2 构造方法
public TestPostConstruct() {
System.out.println("constructer");
}
//way 3 PostConstruct
@PostConstruct
public void init() {
System.out.println("PostConstruct");
}
}
4.implements ApplicationRunner
package com.example.initmethoddemo;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
/**
* @Description: 基于spring启动的加载的方式demo
* @author: LiuYC
* @date: 2021年04月23日 10:19
*/
@Component
public class TestApplicationRunner implements ApplicationRunner {
/**
* 在填充普通bean属性之后但在初始化之前调用
* 类似于initializingbean的afterpropertiesset或自定义init方法的回调
*/
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("hello application runner demo");
}
}
5.implements CommandLineRunner
package com.example.initmethoddemo;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
/**
* @Description: commondline
* @author: LiuYC
* @date: 2021年04月23日 10:23
*/
@Component
public class TestCommondLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("hello init-method CommandLineRunner success");
}
}
6.implements ServletContextListener 同时相关的依赖注意
注意 启动类加上注解 @ServletComponentScan
package com.example.initmethoddemo;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
/**
* @Description: Lisnert
* @author: LiuYC
* 注意 启动类加上注解 @ServletComponentScan
* @date: 2021年04月23日 10:27
*/
@WebListener
public class TestListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("【监听器 StartListener】 contextDestroyed method %%%%%%%%%%%%%%%%");
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("【监听器 StartListener】StartListener contextInitialized method %%%%%%%%%%%%%%");
System.out.println("在监听器中做一些预处理..........");
}
}
7.implements ApplicationListener<ApplicationStartedEvent>
package com.example.initmethoddemo;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;
import java.sql.SQLOutput;
/**
* @Description: ServletContextAware
* @author: LiuYC
* @date: 2021年04月23日 10:49
*/
@Component
public class ApplicationRunListener implements ApplicationListener<ApplicationStartedEvent> {
// 选择方法继承或者实现的idea快捷键
// Ctrl + I Ctrl + o Alt + Insert 图形化操作
@Override
public void onApplicationEvent(ApplicationStartedEvent applicationStartedEvent) {
System.out.println("application run listener ");
}
}
对应的代码demo获取:
https://gitee.com/daka1004/demo.git
参考文章: