建议 spring-data-jpa 中的所有删除和保存方法
advising all delete and save methods in spring-data-jpa
我有一个要求,我需要建议所有删除和保存方法并将 deleted/saved 记录发送到其他地方。
我正在使用具有
的JpaRepository
- 6 x 删除
- 3 次保存
基本上这些方法我都需要建议。问题是它们中的每一个都有不同的方法签名和 return 类型,有时接受 Long、Object 或 List。我正在考虑使用方面来实现这一点,但它似乎很讨厌,因为我目前有 4 个我需要审核的对象,涉及 4 x 9 = 36 个不同的切入点。还有更多这样的东西,所以很快就会达到数百个。
有没有更好的方法?
我按照@sheltem 的建议让它工作了。我使用了 EntityListeners。在我的例子中,我需要访问一个 spring bean 并且能够通过这种方式访问它:
@Component
public class PublishEntityListener {
private static PublishingService publishingService;
@Autowired(
required = true)
public void setPublishingService(PublishingService publishingService) {
this.publishingService = publishingService;
}
@PostConstruct
public void init() {
//Allow the static dependency to be setup post construct as @EntityListeners are no spring managed
}
@PostPersist
public void prePersist(DomainObject<?> entity) {
publishingService.publish(getTopicName(entity), HttpMethod.POST, entity);
}
@PostUpdate
public void preUpdate(DomainObject<?> entity) {
publishingService.publish(getTopicName(entity), HttpMethod.PUT, entity);
}
@PostRemove
public void onDelete(DomainObject<?> entity) {
publishingService.publish(getTopicName(entity), HttpMethod.DELETE, entity);
}
}
我有一个要求,我需要建议所有删除和保存方法并将 deleted/saved 记录发送到其他地方。
我正在使用具有
的JpaRepository- 6 x 删除
- 3 次保存
基本上这些方法我都需要建议。问题是它们中的每一个都有不同的方法签名和 return 类型,有时接受 Long、Object 或 List。我正在考虑使用方面来实现这一点,但它似乎很讨厌,因为我目前有 4 个我需要审核的对象,涉及 4 x 9 = 36 个不同的切入点。还有更多这样的东西,所以很快就会达到数百个。
有没有更好的方法?
我按照@sheltem 的建议让它工作了。我使用了 EntityListeners。在我的例子中,我需要访问一个 spring bean 并且能够通过这种方式访问它:
@Component
public class PublishEntityListener {
private static PublishingService publishingService;
@Autowired(
required = true)
public void setPublishingService(PublishingService publishingService) {
this.publishingService = publishingService;
}
@PostConstruct
public void init() {
//Allow the static dependency to be setup post construct as @EntityListeners are no spring managed
}
@PostPersist
public void prePersist(DomainObject<?> entity) {
publishingService.publish(getTopicName(entity), HttpMethod.POST, entity);
}
@PostUpdate
public void preUpdate(DomainObject<?> entity) {
publishingService.publish(getTopicName(entity), HttpMethod.PUT, entity);
}
@PostRemove
public void onDelete(DomainObject<?> entity) {
publishingService.publish(getTopicName(entity), HttpMethod.DELETE, entity);
}
}