码迷,mamicode.com
首页 > 其他好文 > 详细

@ServerEndpoint:无法主入bean

时间:2020-07-26 01:44:21      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:vol   soc   poi   无法   beans   nts   ntc   ack   component   

解决办法:

import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@ConditionalOnWebApplication
@Configuration
public class WebSocketConfigurator {

  @Bean
  public CustomSpringConfigurator customSpringConfigurator() {
    return new CustomSpringConfigurator(); // This is just to get context
  }
}
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import javax.websocket.server.ServerEndpointConfig;

public class CustomSpringConfigurator extends ServerEndpointConfig.Configurator implements ApplicationContextAware {

    /**
     * Spring application context.
     */
    private static volatile BeanFactory context;

    @Override
    public <T> T getEndpointInstance(Class<T> clazz) throws InstantiationException {
        return context.getBean(clazz);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        CustomSpringConfigurator.context = applicationContext;
    }
}
@Component
@ServerEndpoint(value = "/websocket/car/",configurator = CustomSpringConfigurator.class)
public class WebSocketCar {



}

@ServerEndpoint:无法主入bean

标签:vol   soc   poi   无法   beans   nts   ntc   ack   component   

原文地址:https://www.cnblogs.com/huiy/p/13378534.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!