Issue
Initial situation
I'm currently building an API with Spring using the library PipelinR, which is inspired by the famous NuGet package MediatR. I've created multiple packages within this application to isolate the java classes. The entrypoint of the API is in the package com.example.project.WebApi
. The configuration file for the pipeline is also located here.
@Configuration
public class PipelinrConfiguration {
@Bean
Pipeline pipeline(ObjectProvider<Command.Handler> commandHandlers, ObjectProvider<Notification.Handler> notificationHandlers, ObjectProvider<Command.Middleware> middlewares) {
return new Pipelinr()
.with(commandHandlers::stream)
.with(notificationHandlers::stream)
.with(middlewares::orderedStream);
}
}
Anyways all the commands and command handlers are in different packages, like com.example.project.ApplicationService.CreateSomethingCommand
.
com.example.project.ApplicationService.CreateSomething/
CreateSomethingCommand.java
CreateSomethingCommandHandler.java
Does anybody knows how I could provide these classes in my PipelinrConfiguration.java
file, so that the ObjectProvider is able to find those.
I highly appreciate any kind of help, cheers!
Edit: #001
Yes, the beans are annotated with @Component
.
CreateSomethingCommand.java
public class CreateSomethingCommand implements Command<Voidy> {
public String host;
public CreateSomethingCommand() {
}
public CreateSomethingCommand(String host) {
this();
this.host = host;
}
}
CreateSomethingCommandHandler.java
@Component
public class CreateSomethingCommandHandler implements Command.Handler<CreateSomethingCommand, Voidy> {
@Override
public Voidy handle(CreateSomethingCommand command) {
System.out.println("Command recieved by " + command.host);
return null;
}
}
Solution
@Configuration
@ComponentScan(basePackages = {"package1”, "package2"})
public class PipelinrConfiguration {
// attention here you have to declare three different beans of type ObjectProvider otherwise it will inject by type
@Bean
Pipeline pipeline(@Qualifier(“bean1”) ObjectProvider<Command.Handler> commandHandlers, @Qualifier(“bean2”) ObjectProvider<Notification.Handler> notificationHandlers, @Qualifier(“bean3”) ObjectProvider<Command.Middleware> middlewares) {
return new Pipelinr()
.with(commandHandlers::stream)
.with(notificationHandlers::stream)
.with(middlewares::orderedStream);
}
}
Answered By - Pp88
Answer Checked By - Timothy Miller (JavaFixing Admin)