Hi Team,
This is a very common issue found ( in many axon forums ) and I am finding myself hard to arrive to the solution. And I apologize, not able to keep up to my anxiety levels and repost the same which I already posted in stackoverflow (dependencies - AxoniQ how to configure SpringBeanParameterResolverFactory as part of MultiParameterResolverFactory for spring beans to dependecy inject - Stack Overflow ) for solution.
Axon Framework- 4.5
I am trying to use the feature to auto inject spring bean into the aggregate, which are marked as @Component, and from the blogs I got to know that, its automatically taken care by SpringBeanParameterResolverFactory
, but its not working. I am not sure what else I am missing out.
pom.xml
<axon.version>4.5</axon.version>
...
...
<dependency>
<groupId>org.axonframework</groupId>
<artifactId>axon-spring-boot-starter</artifactId>
<version>${axon.version}</version>
</dependency>
Service to be injected
@Component
public class ExternalService {
public void testcall(){
System.out.println("test operation called");
}
}
Aggregate
@Aggregate
@Profile("command")
public class GiftCard {
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
@AggregateIdentifier
private String giftCardId;
private int remainingValue;
@CommandHandler
public GiftCard(IssueCmd cmd, ExternalService externalService) {
externalService.testcall();
logger.debug("handling {}", cmd);
if (cmd.getAmount() <= 0) {
throw new IllegalArgumentException("amount <= 0");
}
apply(new IssuedEvt(cmd.getId(), cmd.getAmount()));
}
@EventSourcingHandler
public void on(IssuedEvt evt) {
logger.debug("applying {}", evt);
giftCardId = evt.getId();
remainingValue = evt.getAmount();
logger.debug("new remaining value: {}", remainingValue);
}
public GiftCard() {
// Required by Axon
logger.debug("Empty constructor invoked");
}
}
@Component
@Profile("command")
public class GcCommandConfiguration {
@Bean
public Repository<GiftCard> giftCardRepository(EventStore eventStore, Cache cache) {
return EventSourcingRepository.builder(GiftCard.class)
.cache(cache)
.eventStore(eventStore)
.build();
}
@Bean
public Cache cache() {
return new WeakReferenceCache();
}
}
Main class
@SpringBootApplication
public class GcApp {
@Autowired
private CommandGateway commandGateway;
public static void main(String[] args) {
SpringApplication.run(GcApp.class, args).getBean(GcApp.class).run();
}
public void run() {
commandGateway.send(new IssueCmd(UUID.randomUUID().toString(), 10));
}
}
Below is the exception thrown
Caused by: org.axonframework.messaging.annotation.UnsupportedHandlerException: Unable to resolve parameter 1 (ExternalService) in handler public io.axoniq.demo.giftcard.command.GiftCard(io.axoniq.demo.giftcard.api.IssueCmd,io.axoniq.demo.giftcard.service.ExternalService).
at org.axonframework.messaging.annotation.AnnotatedMessageHandlingMember.<init>(AnnotatedMessageHandlingMember.java:71) ~[axon-messaging-4.4.8.jar:4.4.8]
at org.axonframework.messaging.annotation.AnnotatedMessageHandlingMemberDefinition.lambda$createHandler$0(AnnotatedMessageHandlingMemberDefinition.java:51) ~[axon-messaging-4.4.8.jar:4.4.8]
at java.util.Optional.map(Optional.java:215) ~[na:1.8.0_252]
Request for the help pls.