1. 程式人生 > >**websocket 中使用Service層的方法**

**websocket 中使用Service層的方法**

websocket 中使用Service層的方法

 建立公共Utils 類    
ApplicationContextRegister
@Component
        @Lazy(false)
        public class ApplicationContextRegister  implements ApplicationContextAware {
            private static ApplicationContext APPLICATION_CONTEXT;
            /**
                 * 設定spring上下文  *  * @param applicationContext spring上下文  * @throws BeansException  *
             */
@Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { APPLICATION_CONTEXT = applicationContext; } public static ApplicationContext getApplicationContext() { return
APPLICATION_CONTEXT; } }

使用Util

 //websocket 使用service 層
                ApplicationContext act = ApplicationContextRegister.getApplicationContext();
                messagelogService=act.getBean(MessagelogService.class);
                int resultlog = messagelogService.insertIntoMessagelog
(messagelog);