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. vs2010编译C++ 友元函数

    // CTest.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include &l ...

  2. HihoCoder第二周与POJ3630:Trie树的建立

    这又是两道一样的题,都是建立trie树的过程. HihoCoder第二周: 这里其实逻辑都很简单,主要在于数据结构struct的使用. #include <iostream> #inclu ...

  3. 微信小程序自定义组件-下拉框

    这个是网址https://www.cnblogs.com/zjjDaily/p/9548433.html 微信小程序之自定义select下拉选项框组件 知识点:组件,animation,获取当前点击元 ...

  4. java 接口 2.19

    接口中所有的方法都是抽象的和public的,所有的属性都是public,static,final的.

  5. mysql使用的坑

    一: mysql默认是安装记录的物理顺序取数据的,如果不加order by 排序,可能得不到预期的结果. (1) 获取 两个时间点的 id  (很快) $sql = ‘select id from a ...

  6. JAVAWEB limit 分页 (转载)

    原文来自于      https://www.jianshu.com/p/553fc76bb8eb  作者写的很不错 只是为了自己方便学习转载的  代码我就不贴了 我是 Oracle 要改一些代码 原 ...

  7. vCenter组件和服务

    1).随VMware Platform Services Controller一起安装的服务 a. vCenter Single Sign-On身份验证服务 b. vSphere 许可证服务 c. V ...

  8. pandas中na_values与keep_default_na

    我们在使用pandas读取文件时,常会遇到某个字段为NaN. 一般情况下,这时因为文件中包含空值导致的,因为pandas默认会将 '-1.#IND', '1.#QNAN', '1.#IND', '-1 ...

  9. 034-PHP简单定义一个匿名函数

    <?php /* 简单定义一个匿名函数 */ # 把匿名函数赋值给一个变量,也叫临时函数 $demo = function ($txt) { echo $txt; }; # 调用测试下 $dem ...

  10. Swift 闭包使用(循环引用...)

    class networkTool: NSObject { //定义一个可选类型的闭包,用小括号()?括起闭包 var finishedCallBack2:((_ jsonData:String)-& ...