WebServerInitializedEvent &ApplicationRunner
application.properties
app.name=yaoyuan2
app.dept.id=1
MyConfig.java
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class MyConfig {
@Value("${app.name}")
public String name;
@Value("${app.dept.id}")
public String id; @Bean
public Dept getDept() {
return new Dept(id);
}
@Bean
public User getUser() {
return new User(name,getDept());
}
}
@Data
@AllArgsConstructor
class User {
private String name;
private Dept dept;
}
@Data
@AllArgsConstructor
class Dept {
private String id;
}
初始化:
@EventListener(WebServerInitializedEvent.class)
public void onWebServerReady(WebServerInitializedEvent event) {
System.out.println("1.当前WebServer实现类为:"+event.getWebServer().getClass().getName());
Object obj = event.getApplicationContext().getBean(User.class);
System.out.println("获取user对象:"+obj);
}
/**
* 在spring boot应用启动后回调
* @param context
* @return
*/
@Bean
public ApplicationRunner runner(WebServerApplicationContext context) {
return args -> {
System.out.println("2.当前WebServer实现类为:"+context.getWebServer().getClass().getName());
Object obj = context.getBean(User.class);
System.out.println("获取user对象:"+obj);
};
}
输出:
2019-10-19 19:33:11.130 INFO 2312 --- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port(s): 8080
1.当前WebServer实现类为:org.springframework.boot.web.embedded.netty.NettyWebServer
获取user对象:User(name=yaoyuan2, dept=Dept(id=1))
2019-10-19 19:33:11.135 INFO 2312 --- [ main] c.e.s.App : Started App in 4.57 seconds (JVM running for 5.33)
2.当前WebServer实现类为:org.springframework.boot.web.embedded.netty.NettyWebServer
获取user对象:User(name=yaoyuan2, dept=Dept(id=1))
WebServerInitializedEvent &ApplicationRunner的更多相关文章
- Spring Boot 2 - 使用CommandLineRunner与ApplicationRunner
本篇文章我们将探讨CommandLineRunner和ApplicationRunner的使用. 在阅读本篇文章之前,你可以新建一个工程,写一些关于本篇内容代码,这样会加深你对本文内容的理解,关于如何 ...
- CommandLineRunner和ApplicationRunner的区别
CommandLineRunner和ApplicationRunner的区别 二者的功能和官方文档一模一样,都是在Spring容器初始化完毕之后执行起run方法 不同点在于,前者的run方法参数是St ...
- 使用CommandLineRunner或ApplicationRunner接口创建bean
在spring boot应用中,我们可以在程序启动之前执行任何任务.为了达到这个目的,我们需要使用CommandLineRunner或ApplicationRunner接口创建bean,spring ...
- SpringBoot启动加载类ApplicationRunner
SpringBoot启动加载类ApplicationRunner 有时希望项目在启动的时候加载一些系统参数,就要用到ApplicationRunner ApplicationRunner是一个接口,我 ...
- SpringBoot ApplicationRunner/CommandLineRunner
CommandLineRunner.ApplicationRunner 接口是在容器启动成功后的最后一步回调(类似开机自动启动). CommandLineRunner.ApplicationRunne ...
- SpringBoot的ApplicationRunner
Article1 在开发中可能会有这样的情景.需要在容器启动的时候执行一些内容.比如读取配置文件,数据库连接之类的.SpringBoot给我们提供了两个接口来帮助我们实现这种需求.这两个接口分别为Co ...
- SpringBoot之CommandLineRunner接口和ApplicationRunner接口
我们在开发中可能会有这样的情景.需要在容器启动的时候执行一些内容.比如读取配置文件,数据库连接之类的.SpringBoot给我们提供了两个接口来帮助我们实现这种需求.这两个接口分别为CommandLi ...
- SpringBoot扩展点之二:ApplicationRunner和CommandLineRunner的扩展
CommandLineRunner并不是Spring框架原有的概念,它属于SpringBoot应用特定的回调扩展接口: public interface CommandLineRunner { /** ...
- CommandLineRunner and ApplicationRunner
1. Run spring boot as a standalone application (non-web) <?xml version="1.0" encoding=& ...
随机推荐
- hdu 2222 Keywords Search 模板题
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HashMap ConcurrentHashMap解读
前言: 常见的关于HahsMap与ConcurrentHashMap的问题: 数据结构.线程安全.扩容.jdk1.7 HashMap死循环.jdk1.8 HashMap红黑树.容量必须是2的冥次 Ha ...
- 026、Java中改变运算优先级
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- BFC的基础理解及应用场景
最近学习了BFC,开始学习的时候,单纯看概念,有种云里雾里的感觉,字都认识,凑一起啥意思大致也知道,但是具体有什么用呢? 这个就有点迷迷糊糊的,经过老师的讲解,以及自己课后的代码实验与总结,就拨云见日 ...
- 嵊州普及Day2T1
题意:对于给出的数列,有多少数可表示为另两数的和. 思路:先排个序,桶排思路.以一个数组储蓄所有出现的和.最后循环判断是否b[i]>0. 见代码: #include<iostream> ...
- Notepad2&Notepad++
写在前面 几个礼拜前电脑自带的记事本抽风,打开文本后台有进程但就是不显示界面,网上搜的教程无非是重装.杀毒.换包;这些操作要不就是太繁琐要不就是没效果,于是乎我物色了两款十分强大且轻量的开源记事本No ...
- 解析underscore中的debounce
先奉上源码 取自Underscore.js 1.9.1的debounce _.debounce = function(func, wait, immediate) { var timeout, res ...
- QObject的timerEvent使用
其实在QT里,我们自己写的类一般通常需要继承QObject类,因为这一类里规定好的拿来即可用的方法. 比如信号,槽,以及前一个博文写的movetothread方法,这里就是介绍一个QObject的ti ...
- 快速进阶Vue3.0
在2019.10.5日发布了Vue3.0预览版源码,但是预计最早需要等到 2020 年第一季度才有可能发布 3.0 正式版. 可以直接看 github源码. 新版Vue 3.0计划并已实现的主要架构改 ...
- mysql 手动把字段设置为null
在根据经纬度计算距离的时候,发现有的视频点距离我当前位置的距离计算出来的为0,有的距离计算出来是几千公里,仔细看下数据库,发现了问题所在 计算出来几千公里的视屏点的经纬度是空不是null,然后我们手动 ...