关于你的设计有几件事是行不通的,我不确定真正的要求是什么,但github.com/jeffbrown/sanjaisyinterfaces上的项目应该会有所帮助。
src/main/java/sanjaisyinterfaces/SomeReturnType.java
package sanjaisyinterfaces;
public class SomeReturnType {
}
src/main/java/sanjaisyinterfaces/SomeOtherReturnType.java
package sanjaisyinterfaces;
public class SomeOtherReturnType {
}
src/main/java/sanjaisyinterfaces/CommandType.java
package sanjaisyinterfaces;
public interface CommandType {
}
src/main/java/sanjaisyinterfaces/SomeCommandType.java
package sanjaisyinterfaces;
public class SomeCommandType implements CommandType {
}
src/main/java/sanjaisyinterfaces/SomeOtherCommandType.java
package sanjaisyinterfaces;
public class SomeOtherCommandType implements CommandType {
}
src/main/java/sanjaisyinterfaces/IRequestHandler.java
包 sanjaisyinterfaces;
public interface IRequestHandler<C extends CommandType, R> {
R handler(C c);
Class<C> getCmdType();
}
src/main/java/sanjaisyinterfaces/SomeRequestHandler.java
package sanjaisyinterfaces;
import jakarta.inject.Singleton;
@Singleton
public class SomeRequestHandler implements IRequestHandler<SomeCommandType, SomeReturnType> {
@Override
public SomeReturnType handler(SomeCommandType someCommandType) {
System.out.println("SomeRequestHandler is handling a request for SomeCommandType");
// do whatever needs to be done here
return null;
}
@Override
public Class getCmdType() {
return SomeCommandType.class;
}
}
src/main/java/sanjaisyinterfaces/SomeOtherRequestHandler.java
package sanjaisyinterfaces;
import jakarta.inject.Singleton;
@Singleton
public class SomeOtherRequestHandler implements IRequestHandler<SomeOtherCommandType, SomeOtherReturnType> {
@Override
public SomeOtherReturnType handler(SomeOtherCommandType someCommandType) {
System.out.println("SomeOtherRequestHandler is handling a request for SomeOtherCommandType");
// do whatever needs to be done here
return null;
}
@Override
public Class getCmdType() {
return SomeOtherCommandType.class;
}
}
src/main/java/sanjaisyinterfaces/IServiceBus.java
package sanjaisyinterfaces;
public interface IServiceBus {
Object send(CommandType c);
}
src/main/java/sanjaisyinterfaces/ServiceBus.java
package sanjaisyinterfaces;
import jakarta.inject.Singleton;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import static java.util.stream.Collectors.toMap;
@Singleton
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 Object send(CommandType c) {
IRequestHandler handler = handlerMap.get(c.getClass());
if (handler == null)
throw new UnsupportedOperationException("Unsupported command: " + c.getClass());
return handler.handler(c);
}
}
src/main/java/sanjaisyinterfaces/SanjaisyinterfacesCommand.java
package sanjaisyinterfaces;
import io.micronaut.configuration.picocli.PicocliRunner;
import io.micronaut.context.ApplicationContext;
import jakarta.inject.Inject;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
@Command(name = "sanjaisyinterfaces", description = "...",
mixinStandardHelpOptions = true)
public class SanjaisyinterfacesCommand implements Runnable {
@Inject
ServiceBus serviceBus;
public static void main(String[] args) throws Exception {
PicocliRunner.run(SanjaisyinterfacesCommand.class, args);
}
public void run() {
CommandType firstCommand = new SomeCommandType();
CommandType secondCommand = new SomeOtherCommandType();
serviceBus.send(firstCommand);
serviceBus.send(secondCommand);
}
}
运行该项目表明关系正在被识别为预期:
~ $ mkdir example
~ $ cd example
example $ git clone git@github.com:jeffbrown/sanjaisyinterfaces.git
Cloning into 'sanjaisyinterfaces'...
remote: Enumerating objects: 47, done.
remote: Counting objects: 100% (47/47), done.
remote: Compressing objects: 100% (35/35), done.
remote: Total 47 (delta 8), reused 44 (delta 5), pack-reused 0
Receiving objects: 100% (47/47), 63.88 KiB | 838.00 KiB/s, done.
Resolving deltas: 100% (8/8), done.
example $
example $ cd sanjaisyinterfaces
sanjaisyinterfaces (main)$ ./gradlew run
> Task :run
09:10:39.377 [main] INFO i.m.context.env.DefaultEnvironment - Established active environments: [cli]
SomeRequestHandler is handling a request for SomeCommandType
SomeOtherRequestHandler is handling a request for SomeOtherCommandType
sanjaisyinterfaces (main)$