最近搜索

SpringBoot使用@ServerEndpoint无法依赖注入问题解决,无法拿到spring的bean

浏览:788
管理员 2021-09-18 01:50


SpringBoot Websocket 无法拿到spring 上下文


解决方法 

新建类。

package com.java456.socket;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@Component
@Lazy(false)
public class LiQiWebsocketApplicationContextAware implements ApplicationContextAware {
	
	private static ApplicationContext APPLICATION_CONTEXT;
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		APPLICATION_CONTEXT = applicationContext;
	}
	
	public static ApplicationContext getApplicationContext () {
		return APPLICATION_CONTEXT;
	}
}




这样就能拿到了。

image.png


@Resource

private UserService userService   = (UserService) LiQiWebsocketApplicationContextAware.getApplicationContext().getBean("userService");



image.png

联系站长

站长微信:xiaomao0055

站长QQ:14496453