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=& ...
随机推荐
- Vue - 定义使用组件
import Card from './components/Card.vue' Vue.component('m-card',Card) // component是注册全局组件,在实例化VUE前 ...
- Git闪退问题
打开Git 会一闪而过.并出现一个错误的日志文件.自己尝试安装了几个不同的版本Git还是解决不了问题.后来自己在网上找了一些办法,并进行总结 1. 进入git目录下的bin目录执行rebase -b ...
- android:imeOptions
转自:http://www.cnblogs.com/qiengo/archive/2012/05/03/2480857.html 默认情况下软键盘右下角的按钮为“下一个”,点击会到下一个输入框,保持软 ...
- Element-UI Table 实现筛选数据功能
最近产品提出了一个筛选数据的功能,要求在表头里实现一个下拉框进行筛选. 首先, Element-ui 的官方文档,el-table-column 下有一个 filters , 用于数据的筛选和过滤, ...
- Dive into re Module in Python
Dive into RE in Python Standard re module in python is powerful to handle text manipulation,such as ...
- IDEA快速升级模块版本号
使用场景 一个多模块的项目中,在功能用重大更新后,需要升级版本号,如果不使用工具,需要手动更改每个pom.xml文件,而使用工具,就可以非常快速的完成版本号的更改. 基本步骤 0. idea执行ma ...
- 关于可持久化Trie
我认为 可持久化Trie 主要指 可持久化01Trie 如洛谷P4735 将每个数的异或前缀和转化为二进制,添加前缀0至相同位数,然后从最高位开始插入,类似主席树,每一层都对需要更新的点加入一个新的点 ...
- LeetCode1029 两地调度(贪心+java自定义排序回顾)
题目: 公司计划面试 2N 人.第 i 人飞往 A 市的费用为 costs[i][0],飞往 B 市的费用为 costs[i][1]. 返回将每个人都飞到某座城市的最低费用,要求每个城市都有 N 人抵 ...
- Linux基础之防火墙
Linux基础之防火墙 Iptables 最初认识iptables还是在安卓手机上玩tiny的时候知道的,什么扫地僧.Jume等防跳脚本都基于iptables原理,一直觉得iptables的命令很 ...
- VS2012 ffmpeg 没有定义的lrint
在common.h #ifndef AVUTIL_COMMON_H#define AVUTIL_COMMON_H下添加 #define lrint(f) (f>=0?(int32_t)(f+(d ...