记一次自定义管理工厂使用spring自动装载bean
1.核心工厂配置类
@Component
public class HandlerContext {
private Map<String, AbstractHandler> handlerMap;
private HandlerContext() {
}
private void init(Map<String, AbstractHandler> handlerMap) {
this.handlerMap = handlerMap;
}
public AbstractHandler getInstance(String type) {
AbstractHandler handler = handlerMap.get(type);
if (handler == null) {
System.out.println("没有type为‘" + type + "’的处理器");
throw new IllegalArgumentException("not found handler for type: " + type);
}
return handler;
}
//初始化处理器
@Bean
private Map<String, AbstractHandler> initHandlerContext(Map<String, AbstractHandler> handlerMap){
this.init(handlerMap);
return this.handlerMap;
}
}
2.抽象处理父类
public abstract class AbstractHandler {
protected static final Logger log = LoggerFactory.getLogger(AbstractHandler.class);
@Autowired
protected HandlerThreadPool handlerThreadPool;
@Autowired
protected RedisTemplate redisTemplate;
public abstract String handler(ChannelHandlerContext ctx, NettyMessage message);
}
3.子类写法 需使用spring的service注解
@Service("base")
public class BaseHandler extends AbstractHandler
@Service("mls")
public class GameMLSHandler extends AbstractHandler
等等等·····
4. 使用
AbstractHandler handler = handlerContext.getInstance(message.getType());
handler.handler(ctx, message);
5.线程池数组
可根据角标指定哪一个线程去处理,可解决部分多次发送等多线程问题。
@Component
public class HandlerThreadPool {
//线程池数组
private static ExecutorService[] handlerThreadPool = new ExecutorService[5];
public HandlerThreadPool() {
for (int i = 0; i < handlerThreadPool.length; i++) {
handlerThreadPool[i] = Executors.newFixedThreadPool(1);
}
}
public ExecutorService getHandlerThread(int index){
return index > handlerThreadPool.length ? null : handlerThreadPool[index];
}
public static int getThreadSize(){
return handlerThreadPool.length;
}
}
以上的NettyMessage为自定义消息类,需根据具体业务自行编写
记一次自定义管理工厂使用spring自动装载bean的更多相关文章
- Spring自动装配bean
Spring推荐面向接口编程,这样可以很好的解耦具体的实现类. CompactDisc.class 文件: public interface CompactDisc { void play(); } ...
- Spring 自动装配 Bean
Spring3系列8- Spring 自动装配 Bean 1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiri ...
- Spring自动装配Bean详解
1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiring ‘byType 4. Auto-Wirin ...
- Spring深入浅出(二)IOC的单例 ,继承,依赖,JDBC,工厂模式以及自动装载
IOC的单例模式--Bean Spring中的bean是根据scope来决定的. scope有4种类型: 1.singleton:单例模型,表示通过Spring容器获取的该对象是唯一的.常用并且默认. ...
- Spring核心技术(八)——Spring自动装载的注解
本文针对自动装载的一些注解进行描述. 基于注解的容器配置 @Required注解 @Required注解需要应用到Bean的属性的setter方法上面,如下面的例子: public class Sim ...
- Spring自动装配Bean的五种方式
在Spring中,支持 5 自动装配模式. no – 缺省情况下,自动配置是通过“ref”属性手动设定,在项目中最常用byName – 根据属性名称自动装配.如果一个bean的名称和其他bean属性的 ...
- Spring自动注入Bean
通过@Autowired或@Resource来实现在Bean中自动注入的功能,但还要在配置文件中写Bean定义,下面我们将介绍如何注解Bean,从而从XML配置文件 中完全移除Bean定义的配置. 1 ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring自动装配Bean
除了使用 XML 和 Annotation 的方式装配 Bean 以外,还有一种常用的装配方式——自动装配.自动装配就是指 Spring 容器可以自动装配(autowire)相互协作的 Bean 之间 ...
- spring 自动装配 bean 有哪些方式?
Spring容器负责创建应用程序中的bean同时通过ID来协调这些对象之间的关系.作为开发人员,我们需要告诉Spring要创建哪些bean并且如何将其装配到一起. spring中bean装配有两种方式 ...
- SPRING IN ACTION 第4版笔记-第二章-001-用@Autowired\@ComponentScan、@Configuration、@Component实现自动装载bean
1. package soundsystem; import org.springframework.context.annotation.ComponentScan; import org.spri ...
随机推荐
- 羊了怎么居家办公?免费不限速的远控软件RayLink一解燃眉之急!!
近期疫情放开,各地症状不断的小阳人数量猛增(R君盼望大家早日转阴!!).不论是"羊"后居家,还是天选打工人被迫居家,远程控制公司电脑实现居家办公,成了大部分人的默契选择. 想远程控 ...
- tp3.2 写入日志
function logs($content, $subDir = ''){ $subDir = trim($subDir, '/\\'); if (empty($content)) return f ...
- 递归分批次插入数据(An I/O error occurred while sending to the backend报错解决方案)
//递归插入public void add(List<Object> all, long start, long limit){ //截取 List<Object> colle ...
- Google Chrome 离线安装包官方下载
我们参照 Google Chrome帮助中的<下载和安装 Google Chrome>,在这里找到官方最新版离线安装包的下载地址:备用 Chrome 安装程序,历史版本请通过版本号的方式下 ...
- iOS开发之时间格式化
//返回当前时间,以GMT为准 NSDate * date = [NSDate date]; NSLog(@"%@", date); //显示当前时间距离1970-01-01 00 ...
- Unity 设计模式-简单工厂模式和其他好用的通用代码块
1. 2.加法操作类 using System.Collections; using System.Collections.Generic; using UnityEngine; //加法操作类 pu ...
- Kubernetes--用kubeadm搭建集群环境
架构 k8s的组件架构图 OCI (Open Container Initiative) 开放容器标准.是一个轻量级,开放的治理结构(项目),在 Linux 基金会的支持下成立,致力于围绕容器格式和运 ...
- 突然连不上虚拟机,本地网络里没有VMnet8
今天打开虚拟机,突然发现无法ping通网络了,但是能ping通虚拟机ip,打开我的window的网络适配器发现居然没有vmnet 8虚拟网卡了,防火墙什么的都设置好了,仍然不行,后来发现,在网络和共享 ...
- js操作网页元素
二.操作网页元素 1.查找网页元素 给标签设置id属性,一个网页中id的值不能出现重复的 <button id="btn">按钮</button> 2.给按 ...
- 关于decimal与double数据类型
关于double和decimal类型, double类型能表示的精度不如decimal,但是其数据范围比decimal的大. 对于double类型的字段,用sum函数会出现多位小数的情况,比如a+b+ ...