spring boot整合netty总结
来源:互联网 发布:python 字符串合并 编辑:程序博客网 时间:2024/05/17 06:59
spring boot整合netty总结
第一部分 加载
加载可以用2种方法
1.使用spring 提供的注解
@PostConstructpublic ChannelFuture start() { logger.warn("tcp server start init"); bossGroup = new NioEventLoopGroup(1); workerGroup = new NioEventLoopGroup(); ChannelFuture f = null; try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast( new ZDWServerHandler()); } }); // Start the server. f = b.bind(portNumber).sync(); channel = f.channel(); logger.warn("tcp server start ok"); App.Instance().InitDevices(); App.Instance().InitTimer(); // Wait until the server socket is closed. } catch (Exception e){ String msg = e.getMessage(); logger.warn(msg); } finally{ // Shut down all event loops to terminate all threads. } return f;}2.Application继承CommandLineRunner 的方式
第二部分
为非spring boot 管理代码注入依赖,非spring boot管理的代码,无法自动注入依赖,目前测试通过的一种方法是通过保存SpringApplication来通过getBean获取到对应的依赖Spring Boot部分代码
@SpringBootApplicationpublic class Application implements ApplicationContextAware { public static void main(String[] args) throws Exception { SpringApplication app = new SpringApplication(Application.class); app.setWebEnvironment(false); app.run(args); } private static ApplicationContext applicationContext = null; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { Application.applicationContext = applicationContext; } /** * 注意 bean name默认 = 类名(首字母小写) * 例如: A8sClusterDao = getBean("k8sClusterDao") * @param name * @return * @throws BeansException */ public static Object getBean(String name) throws BeansException { return applicationContext.getBean(name); } /** * 根据类名获取到bean * @param <T> * @param clazz * @return * @throws BeansException */ @SuppressWarnings("unchecked") public static <T> T getBeanByName(Class<T> clazz) throws BeansException { try { char[] cs=clazz.getSimpleName().toCharArray(); cs[0] += 32;//首字母大写到小写 return (T) applicationContext.getBean(String.valueOf(cs)); } catch (Exception e) { e.printStackTrace(); return null; } } public static boolean containsBean(String name) { return applicationContext.containsBean(name); }}
使用依赖的代码部分
deviceStatusService = Application.getBeanByName(DeviceStatusService.class);
目前使用中遇到的问题列表
1.在尝试将共用的jpa代码放到单独的module时,web引用OK,netty目前没有搞定
2.scan的basedir参数,添加jpa所在的包会导致运行出错。
阅读全文
0 1
- spring boot整合netty总结
- spring-boot整合redis 总结
- netty中引入spring-boot
- spring boot 整合Freemarker
- Spring Boot整合MyBatis
- spring-boot整合kindeditor
- spring boot 整合mybatis
- Spring Boot整合MyBatis
- Spring-boot整合Mybatis
- spring boot ehcache整合
- Spring Boot整合MyBatis
- spring boot 整合 CAS
- spring boot 整合mybatis
- spring-boot整合websocket
- Spring Boot整合Mybatis
- spring boot mybatis整合
- spring boot整合mybatis
- Spring Boot 整合 MyBatis
- MongoDB 3.4.2 添加用户、设置权限
- angular4表单-响应式表单
- class path resource [setting.xml] cannot be resolved to URL because it does not exist
- 利用 HookZz 实现反调试与绕过的奇淫技巧
- Windows下自动备份mongodb的bat脚本
- spring boot整合netty总结
- 网络管理:3分钟排障秘技,一招胜过十年功
- Apple Watch开发和数据传输(二)
- 硬盘数据恢复的神器有哪些?
- git的学习整理
- 树形数据结构算法
- React虚拟DOM浅析
- Notify.js 消息管理
- Java的数组和集合