创建公共Utils 类
[java] view plain copy
- ApplicationContextRegister
[java] view plain copy
- @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;
- }
- }
[java] view plain copy
- //websocket 使用service 层
- ApplicationContext act = ApplicationContextRegister.getApplicationContext();
- messagelogService=act.getBean(MessagelogService.class);
- int resultlog = messagelogService.insertIntoMessagelog(messagelog);
即可使用到service里的方法了!!
原文地址:https://www.cnblogs.com/duanqiao123/p/8616836.html
时间: 2024-11-03 12:17:22