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的更多相关文章

  1. Spring Boot 2 - 使用CommandLineRunner与ApplicationRunner

    本篇文章我们将探讨CommandLineRunner和ApplicationRunner的使用. 在阅读本篇文章之前,你可以新建一个工程,写一些关于本篇内容代码,这样会加深你对本文内容的理解,关于如何 ...

  2. CommandLineRunner和ApplicationRunner的区别

    CommandLineRunner和ApplicationRunner的区别 二者的功能和官方文档一模一样,都是在Spring容器初始化完毕之后执行起run方法 不同点在于,前者的run方法参数是St ...

  3. 使用CommandLineRunner或ApplicationRunner接口创建bean

    在spring boot应用中,我们可以在程序启动之前执行任何任务.为了达到这个目的,我们需要使用CommandLineRunner或ApplicationRunner接口创建bean,spring ...

  4. SpringBoot启动加载类ApplicationRunner

    SpringBoot启动加载类ApplicationRunner 有时希望项目在启动的时候加载一些系统参数,就要用到ApplicationRunner ApplicationRunner是一个接口,我 ...

  5. SpringBoot ApplicationRunner/CommandLineRunner

    CommandLineRunner.ApplicationRunner 接口是在容器启动成功后的最后一步回调(类似开机自动启动). CommandLineRunner.ApplicationRunne ...

  6. SpringBoot的ApplicationRunner

    Article1 在开发中可能会有这样的情景.需要在容器启动的时候执行一些内容.比如读取配置文件,数据库连接之类的.SpringBoot给我们提供了两个接口来帮助我们实现这种需求.这两个接口分别为Co ...

  7. SpringBoot之CommandLineRunner接口和ApplicationRunner接口

    我们在开发中可能会有这样的情景.需要在容器启动的时候执行一些内容.比如读取配置文件,数据库连接之类的.SpringBoot给我们提供了两个接口来帮助我们实现这种需求.这两个接口分别为CommandLi ...

  8. SpringBoot扩展点之二:ApplicationRunner和CommandLineRunner的扩展

    CommandLineRunner并不是Spring框架原有的概念,它属于SpringBoot应用特定的回调扩展接口: public interface CommandLineRunner { /** ...

  9. CommandLineRunner and ApplicationRunner

    1. Run spring boot as a standalone application (non-web) <?xml version="1.0" encoding=& ...

随机推荐

  1. ElasticSearch入门了解

    什么是Elasticsearch: Elasticsearch,分布式,高性能,高可用,可伸缩的搜索和分析系统 1.什么是搜索? 搜索,就是在任何场景下,找寻你想要的信息,这个时候,会输入一段你要搜索 ...

  2. 01.Delphi最简单的接口

    我想学习一个插件框架,但是那个框架里面大量用到了接口,于是不得不把接口看一下了.总感觉接口编程这一块非常的绕,每一行都注释了. unit Unit1; interface uses Windows, ...

  3. 006-PHP检测是否为整数

    <?php function checkInteger($Number) { if ($Number > 1) { /* 整数减1仍然是整数 */ return (checkInteger ...

  4. UVA 12511/CSU 1120 virus 最长公共上升子序列

    第一次接触一个这最长公共上升子序列 不过其实搞清楚了跟最长公共子序列和 最长上升子序列如出一辙 两重循环,对于当前不相等的,等于前一个的值,相等的,等于比当前A[i]小的最大值+1.弄个临时变量记录最 ...

  5. Codeforces 459E Roland and Rose

    本以为是个树形DP,按照树形DP的方法在那里dfs,结果WA到死,因为它存在有向环,不是树,凡是存在环的情况切记不要用树形的方法去做 题目的突破点在于将边排完序之后,用点表示以该点为边结尾的最大长度, ...

  6. Numpy中np.random.randn与np.random.rand的区别,及np.mgrid与np.ogrid的理解

    np.random.randn是基于标准正态分布产生的随机数,np.random.rand是基于均匀分布产生的随机数,其值在[0,1). np.mgrid 与np.ogrid的理解及区别:np.mgr ...

  7. pythonfile的知识点

    1. file=open("/test/case1.txt","w") #open(路径+文件名,读写模式) #读写模式: r:只读(默认) rb:读二进制文件 ...

  8. 完整版excel上传导入读写批量数据并将反馈结果写入远程exel中

    思路:excel的读写借助于poi框架,在写入远程的时候,是不能直接写入的,本博主将传入的文件再次拉下来写到项目临时文件中,然后,在临时文件中写入,然后,以同样的名称路径覆盖掉远程的就可以了,稍微有点 ...

  9. C++面试常见问题——09static关键字

    static关键字 局部变量 局部变量 局部变量是最常见的量,编译器不会对其进行初始化,除非对其显式赋值,不然初始值是随机的. 普通局部变量存储在栈空间,使用完毕后会立即被释放. 静态局部变量 静态局 ...

  10. docker 后台运行和进入后台运行的容器

    先创建并进入一个新的被命名为newos的新容器    docker run -it --name newos docker.io/centos #创建并指定端口号映射 docker run -d -p ...