@Inject not working in EntityListener


I started using CUBA yesterday and i must say, this is a great simple and effective framework.

This morning i tried creating a BeforeInsertEntityListener that updates an entity with an unique number from the UniqueNumbersAPI. However the UniqueNumbersAPI Bean doesn’t get injected in the listener. It remains null.

public class InvoiceNumberListener implements BeforeInsertEntityListener<Invoice> {

    private UniqueNumbersAPI uniqueNumbers;

    public void onBeforeInsert(final Invoice invoice, final EntityManager entityManager) {

        Long nextNumber = uniqueNumbers.getNextNumber(invoice.getClass().toString());

        String nextInvoiceId = String.format("2016.####", nextNumber);

Forgot to copy-paste it in the code snippet but it also includes


Got it working by creating the Listener from CUBA studio instead of adding it myself in IntelliJ.

Interesting, what was the difference? As soon as you have made it a Spring @Component, injection should have been working.

Seems to me that I didn’t gave the component a name in my first try. Just annotated it with @Component and added the fully qualified classname in the @Listeners annotation. Because that didn’t work I named the component but forgot to update the @Listeners annotation.

So maybe the @Component needs a name for it to work correctly?

I see. When you add a listener to an entity with the FQN, it is created as a plain object instance, not in the Spring container. That’s why injection didn’t work. This behaviour is kept only for backward compatibility and is not recommended. So always register listeners by their bean names (in fact, if you don’t specify one in the @Component annotation the bean name is just a simple name of the class).