Am trying to make a JPA Entity Listener aware of the spring context by marking it as @Configurable. But the injected spring beans are null. Am able to make JPA entities aware of the Spring context using the same technique. Am using Spring(core and data-jpa) as infrastructure. Any ideas on how to acheive this using JPA Entity Listeners or spring data-jpa?
@Configurable
@Scope("singleton")
public class AggregateRootListener {
private static Logger log = LoggerFactory.getLogger(AggregateRootListener.class);
@Autowired
private EventHandlerHelper eventHandlerHelper;
@PostPersist
@PostUpdate
public void publishEvents(BaseAggregateRoot aggregateRoot){
log.info(aggregateRoot.getEvents().toString());
aggregateRoot.getEvents().stream()
.forEach(event -> {
eventHandlerHelper.notify(event, aggregateRoot);
log.info("Publishing " + event + " " + aggregateRoot.toString());
});
}
}
and the BaseAggregateRoot code
@Configurable
@Scope("prototype")
@MappedSuperclass
@EntityListeners(AggregateRootListener.class)
public abstract class BaseAggregateRoot extends BaseDomain{
public static enum AggregateStatus {
ACTIVE, ARCHIVE
}
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "aggregateId", column = @Column(name = "ID", nullable = false))})
protected AggregateId aggregateId;
@Version
private Long version;
}