# SpringBoot资源初始化的5种方式 **Repository Path**: fpfgitmy_admin/springboot-resource-init-five-method ## Basic Information - **Project Name**: SpringBoot资源初始化的5种方式 - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2021-04-28 - **Last Updated**: 2021-04-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### SpringBoot资源初始化的5种方式 + 需要了解SpringBoot启动过程 ![输入图片说明](https://images.gitee.com/uploads/images/2021/0428/153056_ce39a73d_1942182.png "屏幕截图.png") #### 方式一:实现ApplicationRunner接口 1. 创建ApplicationRunnerImpl类并实现ApplicationRunner接口,且实现其方法 2. 使用`@Component`标识该类是个组件 3. 代码如下 ``` @Component public class ApplicationRunnerImpl implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { // 已经写好了方法 TestCron.init(); } } ``` #### 方式二:实现CommandLineRunner接口 1. 创建CommandLineRunnerImpl类实现其接口,重写方法 2. 使用@Component注解标注该类为组件 3. 代码如下 ``` @Component @Order(2) public class CommandLineRunnerImpl implements CommandLineRunner { private Logger log = LoggerFactory.getLogger(CommandLineRunnerImpl.class); @Override public void run(String... args) throws Exception { log.info("资源初始化========================"); } } ``` #### 方式三:使用@PostConstruct注解 + 在具体Bean的实例化过程中执行,@PostConstruct注解的方法,会在构造方法之后执行,顺序为Constructor > @Autowired > @PostConstruct > 静态方法,所以这个注解就避免了一些需要在构造方法里使用依赖组件的尴尬 + 只有一个非静态方法能使用此注解 + 被注解的方法不得有任何参数 + 被注解的方法返回值必须为void + 被注解方法不得抛出已检查异常 + 此方法只会被执行一次 1. 创建TestRunner类,编写对应的方法 2. 使用@Component注解标注该类为组件 3. 代码如下 ``` @Component @Order(3) public class TestRunner { @PostConstruct private void init(){ System.out.println("使用PostConstruct方式资源初始化========"); } } ``` #### 方式四:实现InitializingBean接口 + InitializingBean 是 Spring 提供的一个接口,只包含一个方法 afterPropertiesSet()。凡是实现了该接口的类,当其对应的 Bean 交由 Spring 管理后,当其必要的属性全部设置完成后,Spring 会调用该 Bean 的 afterPropertiesSet()。在Bean在实例化的过程中执执行顺序为:Constructor > @PostConstruct > InitializingBean > init-method 1. 编写InitRunnerBean类实现InitializingBean接口 2. 使用@Component注解标注该类为组件 3. 代码如下 ``` @Component @Order(4) public class InitRunnerBean implements InitializingBean { @Override public void afterPropertiesSet() throws Exception { System.out.println("使用实现InitializingBean接口的方式进行资源初始化==============="); } } ``` #### 方式五:实现ApplicationListener接口 + ApplicationListener 就是spring的监听器,能够用来监听事件,典型的观察者模式。如果容器中有一个ApplicationListener Bean,每当ApplicationContext发布ApplicationEvent时,ApplicationListener Bean将自动被触发。这种事件机制都必须需要程序显示的触发。其中spring有一些内置的事件,当完成某种操作时会发出某些事件动作。比如监听ContextRefreshedEvent事件,当所有的bean都初始化完成并被成功装载后会触发该事件,实现ApplicationListener接口可以收到监听动作,然后可以写自己的逻辑。同样事件可以自定义、监听也可以自定义,完全根据自己的业务逻辑来处理。所以也能做到资源的初始化加载 + 如果有多个监听器的话会执行多次,不推荐 1. 编写InitListener类实现ApplicationListener接口,并重写其方法 2. 使用@Component注解标注该类为组件 3. 代码如下 ``` @Component @Order(5) public class InitListener implements ApplicationListener { @Override public void onApplicationEvent(ApplicationEvent event) { System.out.println("使用了实现ApplicationListener的方式尽心资源初始化========="); } } ```