简体   繁体   English

实体观察者并不总是有效

[英]Observer on Entity does not always work

I have a spring boot and spring data setup (@Data). 我有一个弹簧靴和弹簧数据设置(@Data)。 I need to do some actions when a property of one entity is changed. 当一个实体的属性更改时,我需要执行一些操作。 I am trying to use the observer pattern, so when we call the setter of that property from the code I have added the stateChanged there. 我正在尝试使用观察者模式,因此,当我们从代码中调用该属性的设置器时,我在其中添加了stateChanged。 Do you think that will work as expected, or spring is calling the setters behind the scene? 您是否认为这会按预期工作,或者Spring正在召集二传手?

Spring is definitely not calling these setters from time to time. 春天肯定不会不时召集这些二传手。

To execute your custom code right before an entity update is persisted in the database you could use the @PreUpdate annotation on a method in your entity class, or you could have a higher level abstract entity class with this method, if your @PreUpdate code is the same. 要在将实体更新持久存储在数据库中之前立即执行自定义代码,可以在实体类中的方法上使用@PreUpdate批注,或者如果您的@PreUpdate代码为,则可以在此方法中使用更高级别的抽象实体类相同。

@PreUpdate
public void onPreUpdate() {
    //your custom code here
}

Doing this you can remove the stateChanged calls from your setters. 这样做可以从设置器中删除stateChanged调用。

We had similar needs for two different scenarios and had to use two different solutions. 我们对两种不同的方案有相似的需求,并且不得不使用两种不同的解决方案。

For hibernate entities we used org.hibernate.event.service.spi.EventListenerRegistry to the session factory and listened to required events like PRE_UPDATE or SAVE etc. Listener in our case is a spring bean and has requisite knowledge of other application. 对于休眠实体,我们使用org.hibernate.event.service.spi.EventListenerRegistry到会话工厂,并侦听所需的事件,例如PRE_UPDATE或SAVE等。在我们的案例中,侦听器是一个Spring bean,并且具有其他应用程序的必要知识。 The event listener then notifies required bean about the event took place. 然后,事件侦听器将事件发生的情况通知所需的bean。 But this is valid only hibernate events. 但这仅在休眠事件中有效。

For other scenario, we had to inform other parts of the application something interesting happened. 对于其他情况,我们必须通知应用程序的其他部分有趣的事情。 Not exactly observable pattern, but need was similar. 并不是完全可观察的模式,但是需求是相似的。 We used org.springframework.context.ApplicationEventPublisher and other beans listened to desired types of events using org.springframework.context.ApplicationListener 我们使用org.springframework.context.ApplicationEventPublisher和其他Bean使用org.springframework.context.ApplicationListener侦听所需的事件类型

In my experience second way was much cleaner as classes were not aware of each other. 以我的经验,第二种方法更清洁,因为班级之间彼此不认识。 However if your entities are hibernate entities, this approach may not work. 但是,如果您的实体是休眠实体,则此方法可能不起作用。

I am still researching on that question. 我仍在研究这个问题。 But.. I think I found the answer. 但是..我想我找到了答案。 Because I am sure I have seen setters getting called behind the scene in spring environment. 因为我确定我在春天的环境中看到二传手在幕后被召唤。 ITS FROM THE AUTOWIRING- A PROPERTY AND AUTOWIRING IN THE CONSTRUCTOR 来自自动布线的属性-建筑中的属性和自动布线

 @Autowired private ServiceTest testService;

vs

@Autowired
public ServiceOther(ServiceTest testService){
     this.testService = testService;
}

So Observer pattern can be applied to an Entity in the spring environment if we use Autowiring in the Constructor for that property. 因此,如果我们在构造器中为该属性使用自动装配 ,则可以在春季环境中将观察者模式应用于实体

Still not 1000% sure. 仍不能保证1000%。 So if anyone is good at the topic please explain. 因此,如果有人擅长该主题,请解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM