问题描述:

想在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. 谈谈游戏服务端SDK接入

    “接sdk其实本质上就是一个对着接口文档写adaptor的工作,重复和无味.” 团队减员,身负多职,上一次调SDK已经可以回溯到游戏测试前夕了... 一般SDK只包含验证和支付功能,绝少部分SDK包含 ...

  2. HTML列表简单介绍

    1.无序列表 使用标签:<ul>,<li> 属性:disc,circle,square 2.有序列表 使用标签:<ol>,<li> 属性:A,a,I,i ...

  3. js事件【续】(事件类型)

    一.UI事件[使用时需要添加on eg: onload 页面加载完成事件]load    [一张页面或一幅图像完成加载]页面加载后触发的事件,即进入页面后 unload    [用户退出页面]页面卸载 ...

  4. Django:RestFramework之-------认证

    3 restframework-认证 3.1APIView 认证: 认证是否已经登陆,如果已经登陆返回元组,如果没有登陆报错 源码流程: 执行dispatch方法: def dispatch(self ...

  5. 70.JS---利用原生js做手机端网页自适应解决方案rem布局

    利用原生js做手机端网页自适应解决方案rem布局 刚开始我用的是下面这段代码,然后js通过外部链接引入,最后每次用手机刷新网页的时候都会出现缩略图 function getRem(pwidth, pr ...

  6. 记录下hbuilder vue项目打包APP 在IOS上点击延迟的问题

    做的项目打包成APP在IOS 上有延迟问题,在安卓下却不会,联想到之前 用IONIC时打包的APP也是 在IOS下有300毫秒延迟问题.所以 只能 认吧. 安装fastclick 插件: npm in ...

  7. TP5日志打印方法封装

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

  8. Windows Server 2012 R2 配置IIS

    efs:http://www.07net01.com/storage_networking/windows_server_2012_anzhuang_IIS8_bingzhichi_asp_45191 ...

  9. 网站 & APP 安利

    https://sitoi.cn/posts/51270.html APP 影视类 影视大院 地址:https://www.vfans.fun 描述:没有上架应用商店,需要安装信任企业证书,VIP资源 ...

  10. 【Yellow Cards CodeForces - 1215A 】【贪心】

    该题难点在于求最小的离开数,最大的没什么好说的,关键是求最小的. 可以这样去想,最小的离开数就是每个人获得的牌数等于他所能接受的最大牌数-1,这样就可以直接比较m=a1(k1-1)+a2(k2-1)与 ...