java字段更改侦听器 [英] java Field change listener

查看:179
本文介绍了java字段更改侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我真的想要某种检测特定对象的字段变化的方法.我已经搜索了很长时间,但是没有找到任何东西.因此,基本上我需要知道的是对象的某些变量何时更改.让我们以此类为例:

So I would really want some way of detecting Field changes of a certain object. I have googled for quite a while but haven't found anything. So basically all I need to know, is when some variable of a object changed. lets take this class for instance:

public class Example{
    String text = "test";

    public static void main(String[] args){
        Example e = new Example();
        e.text = "something else";
    }
}

我基本上想检测'text'变量何时更改. 我知道你们中很多人会说使用getters和setters之类的东西,但这不是我想要的.

I would basically want to detect when the 'text' variable has changed. I know many of you would say use getters and setters and such, this is not what I want though.

由于我无法轻易解释的原因,我需要能够从班级外部检测字段变化.目前,我正在考虑使用第二个线程,该线程基本上只是连续扫描对象列表,并使用反射来检测这种方式的变化,但这显然会使程序变得比必须的重.

For a reason I can't easily explain, I need to be able to detect the field change from outside the class. Currently I am thinking of using a second thread that basically just scans a list of objects continuously and use reflection to detect changes that way, but that will obviously make the program heavier than it has to be.

所以我希望有一种只为字段更改创建侦听器的方法,有人知道这样做的库/系统/方法吗?

So I hope there is a way of just creating listeners for field changes, does anyone know about libraries/systems/methods to do this?

推荐答案

让我们重温它:)

有几种方法:

第一个,我认为从Java 6开始就可以使用,它是使用

The first one, which I believe works like since Java 6, is to use Bound Properties inside Java Beans (yes, it also works in JavaSE).

绑定属性的值更改时会通知侦听器.这有两个含义:

A bound property notifies listeners when its value changes. This has two implications:

bean类包括用于管理bean的侦听器的addPropertyChangeListener()和removePropertyChangeListener()方法. 更改绑定属性后,Bean将PropertyChangeEvent发送到其已注册的侦听器. PropertyChangeEvent和PropertyChangeListener位于java.beans包中.

The bean class includes addPropertyChangeListener() and removePropertyChangeListener() methods for managing the bean's listeners. When a bound property is changed, the bean sends a PropertyChangeEvent to its registered listeners. PropertyChangeEvent and PropertyChangeListener live in the java.beans package.

java.beans包还包括PropertyChangeSupport类,该类负责绑定属性的大部分工作.这个方便的类跟踪属性侦听器,并包括一个方便的方法,该方法将属性更改事件触发到所有已注册的侦听器.

The java.beans package also includes a class, PropertyChangeSupport, that takes care of most of the work of bound properties. This handy class keeps track of property listeners and includes a convenience method that fires property change events to all registered listeners.

第二步,您可以免费获得大多数东西,它在Java 7(javafx 2.0)和

In the second, you get most of the things for free, its on Java 7 (javafx 2.0), and it's called JavaFX Properties.

您还可以添加一个更改侦听器,以便在属性值更改时得到通知,如您所示,还可以添加一个更改侦听器,以便在属性值更改时得到通知.

You can also add a change listener to be notified when the property's value has changed, as shown in You can also add a change listener to be notified when the property's value has changed.

后者的缺点是JPA对此并不友好,因此使其运行起来有些痛苦.

The downside of the latter is that JPA is not friendly with this, so its a bit of a pain to get it running.

这篇关于java字段更改侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆