WebSocket獲取Bean 【SpringBoot篇-2】
阿新 • • 發佈:2021-12-02
SpringBoot+WebSocket對每個客戶端連線都會建立一個WebSocketServer(@ServerEndpoint註解對應的)物件,Bean的注入操作會被直接略過,因而手動注入一個全域性變數
@ServerEndpoint("/im/{userId}/{toUserId}") @RestController public class ImController { public static ChatMessageRepository chatMessageRepository; //...... }
開啟WebSocket支援
/** * 開啟WebSocket支援 *@author zhengkai */ @Configuration public class WebSocketConfig { /** * ServerEndpointExporter 用於掃描和註冊所有攜帶 ServerEndPoint 註解的例項, * 若部署到外部容器 則無需提供此類。 * * @return */ @Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); }/** * 因 SpringBoot WebSocket 對每個客戶端連線都會建立一個 WebSocketServer(@ServerEndpoint 註解對應的) 物件,
* Bean 注入操作會被直接略過,因而手動注入一個全域性變數 * * @param messageService */ @Autowired public void setMessageService(ChatMessageRepository chatMessageRepository) { ImController.chatMessageRepository= chatMessageRepository; } }
獲取Bean工具:SpringUtils.getBean
1. 傳入String格式的類名
2. 傳入.class的類
import org.springframework.beans.BeansException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.stereotype.Component; /** * @author liuzhihao */ @Component public final class SpringUtils implements BeanFactoryPostProcessor { private static ConfigurableListableBeanFactory beanFactory; @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { SpringUtils.beanFactory = beanFactory; } public static ConfigurableListableBeanFactory getBeanFactory() { return beanFactory; } /** * 獲取物件 * * @param name * @return Object 一個以所給名字註冊的bean的例項 * @throws org.springframework.beans.BeansException * */ @SuppressWarnings("unchecked") public static <T> T getBean(String name) throws BeansException { if(getBeanFactory()==null){ //zhengkai.blog.csdn.net System.out.println("本地除錯Main模式,沒有BeanFactory,忽略錯誤"); return null; }else{ T result = (T) getBeanFactory().getBean(name); return result; } } /** * 獲取型別為requiredType的物件 * * @param name * @return * @throws org.springframework.beans.BeansException * */ public static <T> T getBean(Class<T> name) throws BeansException { if(getBeanFactory()==null){ //zhengkai.blog.csdn.net System.out.println("本地除錯Main模式,沒有BeanFactory,忽略錯誤"); return null; }else{ T result = (T) getBeanFactory().getBean(name); return result; } } /** * 如果BeanFactory包含一個與所給名稱匹配的bean定義,則返回true * * @param name * @return boolean */ public static boolean containsBean(String name) { return getBeanFactory().containsBean(name); } /** * 判斷以給定名字註冊的bean定義是一個singleton還是一個prototype。
* 如果與給定名字相應的bean定義沒有被找到,將會丟擲一個異常(NoSuchBeanDefinitionException) * * @param name * @return boolean * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException * */ public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { return getBeanFactory().isSingleton(name); } /** * @param name * @return Class 註冊物件的型別 * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException * */ public static Class<?> getType(String name) throws NoSuchBeanDefinitionException { return getBeanFactory().getType(name); } /** * 如果給定的bean名字在bean定義中有別名,則返回這些別名 * * @param name * @return * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException * */ public static String[] getAliases(String name) throws NoSuchBeanDefinitionException { return getBeanFactory().getAliases(name); } }
Example
@ServerEndpoint("/imserver/{userId}") @Component public class WebSocketServer { /** * ServerEndpoint不支援注入,可以用SpringUtils獲取IOC例項 */ private StringRedisTemplate redisTemplate = SpringUtils.getBean(StringRedisTemplate.class); //....省略redisTemplate的呼叫 }