How to inject resources into command handler in axon 3?

Hi Allard,

nice work with finishing the 3.0 release just before this year ends :slight_smile:

I’m experimenting with axon 3 through the release candidates now for a while and switched to the 3.0 release today. So far I’ve got a prototype web application running but ran into some trouble using a command handler class. Currently the injection point of the event sourcing repository in my command handler is not called. I’ve checked the new documentation (great work) but couldn’t find a hint on that. Here is the code of my command handler:

`

public class ParticipantLinkService {
    private static Logger logger = LoggerFactory.getLogger(ParticipantLinkService.class);

    private Repository<ParticipantAggregate> repository;

    public ParticipantLinkService() {
        logger.info("Service created");
    }

    public void setRepository(Repository<ParticipantAggregate> repository) {
        this.repository = repository;
        logger.info("Got repository {}", repository);
    }

    @CommandHandler
    public void linkParticipants(LinkParticipants command) {
        logger.info("Handling command {}", command);

        // try to load participants to make sure they exist
        ParticipantAggregate fromParticipant = (ParticipantAggregate) repository.load(command.getFromId().toString());
        ParticipantAggregate toParticipant = (ParticipantAggregate) repository.load(command.getToId().toString());

        // add relation
        fromParticipant.beziehungKnüpfen(command.getRelationType(), command.getToId());
    }
}

`

The command handler is called but results in a NullPointerException because the repository is not injected. Any help would be appreciated.

Frank.

P.S. A happy new year to you and all axon fellows on this news group.

Hi Frank,

it seems like you forgot to annotate your setter method with (for example) @Autowired or @Inject.

Cheers,

Allard

it seems like you forgot to annotate your setter method with (for example) @Autowired or @Inject.

Hi Alard,

tried this but still doesn’t work. My Code now looks like this:

`

public class ParticipantLinkService {
private static Logger logger = LoggerFactory.getLogger(ParticipantLinkService.class);

@Inject
@Named(“ParticipantRepository”)
private Repository repository;

@CommandHandler
public void on(LinkParticipantCommand command) {
logger.info(“Handling command fromId:{}, toId:{}, relationType:{}”, command.getFromId(), command.getToId(), command.getRelationType());

if (repository != null) {
// try to load participants to make sure they exist
ParticipantAggregate fromParticipant = (ParticipantAggregate) repository.load(command.getFromId().toString());
ParticipantAggregate toParticipant = (ParticipantAggregate) repository.load(command.getToId().toString());

// add relation
fromParticipant.LinkParticipant(command.getRelationType(), command.getToId());
} else {
logger.error(“Injection of repository failed”);
}
}
}

`

I’ve tried the @Inject with and without @Named as well as with setter injection instead of field injection.

I’m using axon within a jee web application and configured via DefaultConfigurer. As far as I understand the source code, the injection is then done using the ConfigurationResourceInjector which extends org.axonframework.eventhandling.saga.AbstractResourceInjector.

Must I use another injector within this setup?

Take care,

Frank

Hi Frank,

the ResourceInjector only works with Sagas, not with arbitrary beans (which I assume your Service is). Therefore, for injection, you would need to rely on your JEE container to inject your resources.

Cheers,

Allard