1
votes

I have a class defined like this:

public class MyClass extends SimpleChannelInboundHandler<DataFrame<ByteBuf>> implements ApplicationEventPublisherAware

(SimpleChannelInboundHandler is an io.netty class.)

Then, in my xml file I define the MyClass like this:

<bean id="MyClass" class="com.mypackage.MyClass" />

According to the documentation:

At configuration time, the Spring container will detect that EmailService implements ApplicationEventPublisherAware and will automatically call setApplicationEventPublisher().

But it's null when I run this.

Any ideas why?

Thanks

2
what do you mean with "its null"? is the method called with a null argument? - adrobisch

2 Answers

1
votes

A common usage pattern for ApplicationEventPublisherAware looks like this:

package example;

import org.springframework.stereotype.*;
import org.springframework.context.*;

@Component
public class MyBean implements ApplicationEventPublisherAware {
  ApplicationEventPublisher applicationEventPublisher;

  public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
    System.out.println("publisher: " + applicationEventPublisher);
    this.applicationEventPublisher = applicationEventPublisher;
  }

  ... (use applicationEventPublisher in methods)
}

You just need to make sure the bean is really added to the context via component scan / configuration / <bean> tag, try to inject it into another bean to check that.

0
votes

You should use the getBean method of ApplicationContext to get an instance of MyClass instead of initializing by yourself using the new keyword. So that Spring Container could set the ApplicationEventPublisher.