问题描述:

想在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(){
// 执行操作即可。
}

springboot框架在容器加载完成之后执行某个方法的更多相关文章

  1. spring 容器加载完成后执行某个方法

    理论 刚好再开发过程中遇到了要在项目启动后自动开启某个服务,由于使用了spring,我在使用了spring的listener,它有onApplicationEvent()方法,在Spring容器将所有 ...

  2. spring启动容器加载成功后执行调用方法

    需求: 由于在微服务架构中各服务之间都是通过接口调用来进行交互的,像很多的基础服务,类似字典信息其实并不需每次需要的时候再去请求接口.所以我的想法是每次启动项目的时候,容器初始化完成,就去调用一下基础 ...

  3. 基于DOMContentLoaded实现文档加载完成后执行的方法

    我们有时可能需要一些在页面加载完成之后执行的方法,其实js原生就提供了onload方法,所以我们最简单的办法就是直接给onload赋值一个函数,在页面加载完成之后就会自动执行 widnow.onloa ...

  4. spring boot容器加载完后执行特定操作

    有时候我们需要在spring boot容器启动并加载完后,开一些线程或者一些程序来干某些事情.这时候我们需要配置ContextRefreshedEvent事件来实现我们要做的事情 1.Applicat ...

  5. JS框架设计之加载器所在路径的探知一模块加载系统

    1.要加载一个模块,我们需要一个URL作为加载地址,一个script作为加载媒介,但用户在require是都用ID,我们需要一个将ID转换为URL的方法,思路很简单,强加个约定,URL的合成规则是为: ...

  6. 动态加载机Servlet容器加载器

    动态加载是Servlet 3.0中的新特性,它可以实现在 不重启Web应用的情况下加载新的Web对象(Servlet. Filter.Listener).Servlet容器加载器也是Servlet 3 ...

  7. 动态加载及Servlet容器加载

    动态加载 动态加载是 Servlet 3.0 中的新特性,它可以实现在不重启 Web 应用的情况下加载新的 Web 对象(Servlet.Filter.Listener). 为了实现动态加载的第一种方 ...

  8. springMVC容器加载源码分析

    springmvc是一个基于servlet容器的轻量灵活的mvc框架,在它整个请求过程中,为了能够灵活定制各种需求,所以提供了一系列的组件完成整个请求的映射,响应等等处理.这里我们来分析下spring ...

  9. 如果你还不知道如何控制springboot中bean的加载顺序,那你一定要看此篇

    1.为什么需要控制加载顺序 springboot遵从约定大于配置的原则,极大程度的解决了配置繁琐的问题.在此基础上,又提供了spi机制,用spring.factories可以完成一个小组件的自动装配功 ...

随机推荐

  1. map字典,储存cookie,切换账户,展示购物车不同商品

    1:首页 1,静态html5+css做好基本样式 html5,css,jQery, sass 2,jsonp的方式src引入模拟的json数据//这里用的jsonp方式拿数据,详情有使用ajax 遍历 ...

  2. layui 表格中实现照片预览,点击查看原图

    人员表格中实现照片预览,并且可点击放大.查看原图 <table id="dutyInfoTable" class="layui-hide">< ...

  3. 【案例】如何让阀门制造提高排产效率?APS系统帮你实现

    随着公司业务发展,苏州纽威阀门公司将承接来自各个国家的更多产品业务,越来越多的客户要求对产品进行精确的交期预估和管理.而目前对产线的产能管理仅限于人工静态产能计算. 由于产品繁多,生产流程各异,不同产 ...

  4. TP5日志打印方法封装

    需求:在开发及测试过程中需要打印日志,并且可以将日志内容打印到指定文件,这样方便查看. 过程: 1. 打开think\Log文件,在该文件中添加一个静态方法,代码如下: /** * @param $m ...

  5. Apache Kafka是数据库吗?

    最近思路有些枯竭,找些务虚的话题来凑.本文内容完全来自于Martin Kelppmann在2019年Kafka伦敦峰会上的演讲.顺便提一句,Kelppmann是<Designing Data-I ...

  6. python可视化_matplotlib

    对于Python数据可视化库,matplotlib 已经成为事实上的数据可视化方面最主要的库,此外还有很多其他库,例如vispy,bokeh, seaborn,pyga,folium 和 networ ...

  7. pandas 之 索引重塑

    import numpy as np import pandas as pd There are a number of basic operations for rearanging tabular ...

  8. 微信小程序 子组件调用父组件方法

    原文连接   --->  https://blog.csdn.net/qq_40190624/article/details/87972265 组件 js:  var value = 123; ...

  9. Java基础--线程创建方式

    线程的创建主要有两种形式,通过继承Thread或者实现Runnable接口,本质上没有太大区别. /** * @date: 2019/7/16 **/ public class ThreadOne i ...

  10. Find the median(2019年牛客多校第七场E题+左闭右开线段树)

    题目链接 传送门 题意 每次往集合里面添加一段连续区间的数,然后询问当前集合内的中位数. 思路 思路很好想,但是卡内存. 当时写的动态开点线段树没卡过去,赛后机房大佬用动态开点过了,\(tql\). ...