使用 Micronaut 在 Java 中用于命令和处理程序的通用 class 注册表
Generic class registry for command and handler in Java with Micronaut
尝试在 Micronaut
中进行 java 泛型 class 的依赖注入
界面
public interface IRequestHandler<C, R> {
R handler(C c);
}
依赖注入
@Singleton
public record ServiceBus(IRequestHandler<C, R> iRequestHandler) implements IServiceBus{
@Override
public <C, R> R send(C c) {
return iRequestHandler.handler(c);
}
}
界面
public interface IServiceBus {
<C,R> R send(C c);
}
实施
public class ProductController {
public ProductController(IServiceBus iRequestHandler){
_iserviceBus= iRequestHandler;
}
public Mono<?> get(ProductSearchCriteriaCommand searchCriteria) {
var item = _iserviceBus.<CreateProductCommand,CreateProductCommand>send(new CreateProductCommand());
return null;
}
}
我的问题出在 ServiceBus class 上,我如何传递 public record ServiceBus(IRequestHandler<C, R> iRequestHandler)
和 iRequestHandler.handler(c);
的类型
@Singleton
public class CreateProductCommandHandler implements IRequestHandler<CreateProductCommand, CreateProductCommand> {
@Override
public CreateProductCommand handler(CreateProductCommand createProductCommand) {
return null;
}
}
@Singleton
public record DeleteProductCommandHandler() implements IRequestHandler<DeleteProductCommand,DeleteProductCommand> {
@Override
public DeleteProductCommand handler(DeleteProductCommand deleteProductCommand) {
return null;
}
}
当我在控制器中调用 _iserviceBus.<CreateProductCommand,CreateProductCommand>send(new CreateProductCommand());
时,我试图在 CreateProductCommandHandler
class
中调用 handler
方法
类似地,如果我调用 _iserviceBus.<CreateProductCommand,CreateProductCommand>send(new DeleteProductCommand());
DeleteProductCommandHandler
class 的 handler
方法应该调用
您错误地定义了 IServiceBus 接口:
public interface IServiceBus {
<C,R> R send(C c);
}
应该是:
public interface IServiceBus<C,R> {
R send(C c);
}
更新。非常基本的注册表示例。
public interface IRequestHandler<C, R> {
R handler(C c);
Class<C> getCmdType();
}
public class ServiceBus implements IServiceBus {
private final Map<Class<?>, IRequestHandler<?, ?>> handlerMap;
public ServiceBus(List<IRequestHandler<?, ?>> handlers) {
handlerMap = handlers.stream()
.collect(toMap(
IRequestHandler::getCmdType,
Function.identity()
));
}
@Override
public <C, R> R send(C c) {
IRequestHandler<C, R> handler = (IRequestHandler<C, R>) handlerMap.get(c.getClass());
if (handler == null) {
throw new UnsupportedOperationException("Unsupported command: " + c.getClass());
}
return handler.handler(c);
}
}
尝试在 Micronaut
中进行 java 泛型 class 的依赖注入界面
public interface IRequestHandler<C, R> {
R handler(C c);
}
依赖注入
@Singleton
public record ServiceBus(IRequestHandler<C, R> iRequestHandler) implements IServiceBus{
@Override
public <C, R> R send(C c) {
return iRequestHandler.handler(c);
}
}
界面
public interface IServiceBus {
<C,R> R send(C c);
}
实施
public class ProductController {
public ProductController(IServiceBus iRequestHandler){
_iserviceBus= iRequestHandler;
}
public Mono<?> get(ProductSearchCriteriaCommand searchCriteria) {
var item = _iserviceBus.<CreateProductCommand,CreateProductCommand>send(new CreateProductCommand());
return null;
}
}
我的问题出在 ServiceBus class 上,我如何传递 public record ServiceBus(IRequestHandler<C, R> iRequestHandler)
和 iRequestHandler.handler(c);
@Singleton
public class CreateProductCommandHandler implements IRequestHandler<CreateProductCommand, CreateProductCommand> {
@Override
public CreateProductCommand handler(CreateProductCommand createProductCommand) {
return null;
}
}
@Singleton
public record DeleteProductCommandHandler() implements IRequestHandler<DeleteProductCommand,DeleteProductCommand> {
@Override
public DeleteProductCommand handler(DeleteProductCommand deleteProductCommand) {
return null;
}
}
当我在控制器中调用 _iserviceBus.<CreateProductCommand,CreateProductCommand>send(new CreateProductCommand());
时,我试图在 CreateProductCommandHandler
class
handler
方法
类似地,如果我调用 _iserviceBus.<CreateProductCommand,CreateProductCommand>send(new DeleteProductCommand());
DeleteProductCommandHandler
class 的 handler
方法应该调用
您错误地定义了 IServiceBus 接口:
public interface IServiceBus {
<C,R> R send(C c);
}
应该是:
public interface IServiceBus<C,R> {
R send(C c);
}
更新。非常基本的注册表示例。
public interface IRequestHandler<C, R> {
R handler(C c);
Class<C> getCmdType();
}
public class ServiceBus implements IServiceBus {
private final Map<Class<?>, IRequestHandler<?, ?>> handlerMap;
public ServiceBus(List<IRequestHandler<?, ?>> handlers) {
handlerMap = handlers.stream()
.collect(toMap(
IRequestHandler::getCmdType,
Function.identity()
));
}
@Override
public <C, R> R send(C c) {
IRequestHandler<C, R> handler = (IRequestHandler<C, R>) handlerMap.get(c.getClass());
if (handler == null) {
throw new UnsupportedOperationException("Unsupported command: " + c.getClass());
}
return handler.handler(c);
}
}