springboot框架在容器加载完成之后执行某个方法
2024-09-05 20:02:38
问题描述:
想在websocket实现的Handler中执行一些初始化操作,但是初始化操作使用到了@Autowired自动注入的变量,而且是保护类型。第一个想法是放到Handler构造函数中执行,但是@Autowired注入的变量为null,故此方法不可行。
问题解决
解决方法一
在Application启动类中实现ApplicationListener接口,这个接口支持的ApplicationReadyEvent事件在上下文已经准备完毕的时候触发。
代码如下
/*
* Copyright 2018 Kurento (https://www.kurento.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kurento.tutorial.rtpreceiver;
import org.kurento.client.KurentoClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;
/**
* Kurento Java Tutorial - Main Application class.
*/
@SpringBootApplication
@EnableWebSocket
@Configuration
public class Application implements WebSocketConfigurer
{ //, ApplicationListener<ApplicationReadyEvent>
@Bean
public Handler handler()
{
return new Handler();
}
@Bean
public KurentoClient kurentoClient()
{
return KurentoClient.create();
}
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry)
{
String[] allowsOrigins = {"*"};
registry.addHandler(handler(), "/rtpreceiver").setAllowedOrigins(allowsOrigins);
//registry.addHandler(handler(), "/rtpreceiver");
}
public static void main(String[] args) throws Exception
{
SpringApplication.run(Application.class, args);
}
@Override
public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
try {
System.out.println("all is ok");
// 在此处调用其他类中需要执行的方法
} catch (Exception e) {
System.out.println("[项目启动时调用g1] 发生错误,");
}
}
}
// 问是,我的Handler也是@Bean形式注入,不知道怎么调用Handler类中的方法……
解决方法二
直接在Handler类中利用@PostConstruct注解方式,PostConstruct注释用于在依赖关系注入完成之后需要执行的方法上,以执行任何初始化。
@PostConstruct
private void pushflow(){
// 执行操作即可。
}
最新文章
- CSS float 定位和缩放问题
- 神奇的margin之豆瓣豆瓣么么哒
- R语言学习
- WebForm水印照片
- Apache shiro之权限校验流程
- MVC如何在单独的类库中添加区域
- svcutil 生成代理类时的问题
- Hibernate 性能优化之懒加载
- WIN32_FIND_DATA 循环获取文件大小BUG
- 关于Android Launcher图标上面动态改变数字的实现
- [BZOJ]2017省队十连测推广赛1 T2.七彩树
- JVM基础系列开篇:为什么要学虚拟机?
- Connection open error . Connection Timeout Expired. The timeout period elapsed during the post-login phase.
- 4.9Python数据处理篇之Matplotlib系列(九)---子图分布
- day 7 - 1 集合、copy及基础数据类型汇总
- mysql连接报java.math.BigInteger cannot be cast to java.lang.Long异常
- javascript对属性的操作
- Servlet的几个关键知识点
- JQuery监听页面滚动总结
- Perl实战(一)
热门文章
- 不知道密码情况下 进行docker数据库可视化连接
- MongoDB分片,唯一索引与upsert
- mybatis + oracle,出现ORA-01461:仅能绑定要插入LONG列的LONG值
- 在现有的mysql主从基础上,搭建mycat实现数据的读写分离
- ShortKey – Visual Studio/VS Code etc.
- python实现文件批量编码转换
- Python paramiko安装报错
- VS Code + MinGW + Clang + OpenGL (vscode 配置 opengl环境)
- Flask-Script Manager
- 【转】大众点评Cat--架构分析