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=& ...
随机推荐
- GCPC 2018
C.Coolest Ski Route 题意:n个点,m条边组成的有向图,求任意两点之间的最长路径 dfs记忆化搜索 #include<iostream> #include<stri ...
- git 的一些基本命令
基本命令 1.返回上一级目录:cd ../ 2.进入某一目录:cd git (进入 git 目录) 3. 显示当前路径:pwd 4.显示当前文件目录的文件 : dir 5.新建文件夹:mkdir +文 ...
- Java提升三:函数式接口
1. 定义 函数式接口即是有且仅有一个抽象方法的接口. 注意: (1)函数式接口只对于抽象方法有要求,对于接口中的默认方法,静态方法,私有方法数量并不作特殊要求. (2)既然函数式接口定义了抽象方法, ...
- java面试题汇总,不断更新中。。。
JVM,并发,锁相关: 1.请你谈谈对volatile的理解,volatile是否存在伪共享问题. 2.cas你知道吗? 3.原子类AtomicInteger的ABA问题谈谈?原子更新引用知道吗? 4 ...
- Java生鲜电商平台-优惠券系统设计详解
Java生鲜电商平台-优惠券系统设计详解 优惠券作为电商最常用的营销手段,对于商家而言可以起到拉新.促活.提高转化的作用,对用户而言也可以获得实惠,今天就来谈谈优惠券系统的设计逻辑. 我对于优惠券系统 ...
- NO11 SSH故障排查思路和netstat命令
本章知识相关考试:1.企业场景面试题:Linux系统如何优化?2.企业场景面试题:SSH服务连不上,如何排查?记住回答技巧: 1 ping 2 telnet 客户端ssh工具:SecureCRT,x ...
- 洛谷P1002——过河卒
又是洛谷题,要不是有小姐姐不会,我才不想动脑子.先贴一下题目地址https://www.luogu.org/problem/P1002 再贴一下题目: 我们读一下题目,这可不比学校的**算法题,读完一 ...
- POJ 1195:Mobile phones 二维树状数组
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 16893 Accepted: 7789 De ...
- wamp修改端口localhost
一.修改Apache端口 1.在界面中选Apache,弹出隐藏菜单选项,打开配置文件httpd.conf: 2.找到 Listen 80: ServerName localhost:80; 3.将 8 ...
- 【pwnable.kr】 unlink
pwnable.kr 第一阶段的最后一题! 这道题目就是堆溢出的经典利用题目,不过是把堆块的分配与释放操作用C++重新写了一遍,可参考<C和C++安全编码一书>//不是广告 #includ ...