Property binding with Spring @Value annotation in an aggregate

Hi,

I get inconsistent results when I use @Value inside an aggregate.
Is property binding using Spring @Value in an aggregate supported?

@Aggregate
public class GiftCard {
@Value(“${app.name:‘TEST’}”)
private String appName;

@CommandHandler
public GiftCard(IssueCardCommand cmd) {
    **// this.appName is null here**
}

protected GiftCard() {
}
// omitted command handlers and event sourcing handlers

}

Thanks!

The lifecycle of aggregates isn’t managed by Spring, so unfortunately, you cannot use the @Value approach to injecting properties.
Axon provides an injection mechanism for handlers which you could use for this. Create a Spring bean that has the @Value annotated field, and then add that Spring bean as a parameter to your Command Handler (or any other handler that requires the property).

Hope this helps.

2 Likes

Thanks @allardbz!
That is exactly what I did.