- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
关于向现有 POJO 类添加 JavaFX 属性支持,存在相当多的问题。这些类的属性可以通过使用 javafx.beans.property.adapter 中的适配器来创建。包裹。但是,以这种方式创建的属性不会反射(reflect)使用 POJO 类的 setter 方法所做的更改,除非PropertyChangeSupport添加到 POJO 类中。
更改现有类有时是不可能的,即使可以,如果您有很多类,添加 PropertyChangeSupport 也可能会非常乏味。所以我想分享一种不需要更改现有类的方法。
最佳答案
该解决方案的灵感来自 article作者:Ben Galbraith,并使用 AspectJ 。它绝对不需要对现有模型类进行任何更改。安装 AspectJ 超出了本教程的范围,可以说所有主要 IDE 都支持它(在 Eclipse 中安装它很简单)。
此示例假设所有模型类都扩展一个基类,在本例中称为 BaseEntity。如果您的实现与此不同,您当然需要调整该方面。
首先,我们将创建一个定义 PropertyChangeSupport 所需方法的接口(interface)。
package com.mycompany.myapp;
import java.beans.PropertyChangeListener;
public interface ChangeSupport {
// Add listener for all properties
public void addPropertyChangeListener(PropertyChangeListener listener);
// Remove listener for all properties
public void removePropertyChangeListener(PropertyChangeListener listener);
// Add listener for specific property
public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener);
// Remove listener for specific property
public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener);
// Fire change event for specific property
public void firePropertyChange(String propertyName, Object oldValue, Object newValue);
// Check if property has any listeners attached
public boolean hasListeners(String propertyName);
}
接下来,我们将创建该接口(interface)的实现。
package com.mycompany.myapp;
import com.mycompany.myapp.model.BaseEntity;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
public class ChangeSupportImpl implements ChangeSupport {
// Declared transient as there is no need to serialize these fields
private transient PropertyChangeSupport propertyChangeSupport;
private final transient Object source;
public ChangeSupportImpl() {
super();
this.source = this;
}
// Needed for annotation-style aspect
public ChangeSupportImpl(final BaseEntity baseEntity) {
super();
this.source = baseEntity;
}
@Override
public void addPropertyChangeListener(final PropertyChangeListener listener) {
// PropertyChangeSupport is loaded lazily
if (this.propertyChangeSupport == null)
this.propertyChangeSupport = new PropertyChangeSupport(this.source);
this.propertyChangeSupport.addPropertyChangeListener(listener);
}
@Override
public void removePropertyChangeListener(final PropertyChangeListener listener) {
if (this.propertyChangeSupport != null)
this.propertyChangeSupport.removePropertyChangeListener(listener);
}
@Override
public void addPropertyChangeListener(final String propertyName, final PropertyChangeListener listener) {
// PropertyChangeSupport is loaded lazily
if (this.propertyChangeSupport == null)
this.propertyChangeSupport = new PropertyChangeSupport(this.source);
this.propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
}
@Override
public void removePropertyChangeListener(final String propertyName, final PropertyChangeListener listener) {
if (this.propertyChangeSupport != null)
this.propertyChangeSupport.removePropertyChangeListener(propertyName, listener);
}
@Override
public void firePropertyChange(final String propertyName, final Object oldValue, final Object newValue) {
if (this.propertyChangeSupport != null)
this.propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue);
}
@Override
public boolean hasListeners(final String propertyName) {
return this.propertyChangeSupport != null && (this.propertyChangeSupport.hasListeners(propertyName)
|| this.propertyChangeSupport.hasListeners(null));
}
}
最后,我们将创建一个方面,将 PropertyChangeSupport 添加到 BaseEntity 类。该方面使用自定义类 ReflectUtils 来获取属性的旧值。您可以使用任何您喜欢的实用程序,或者普通的旧式 Java 反射(不过,这可能会影响性能)。
package com.mycompany.myapp;
import com.mycompany.myapp.model.BaseEntity;
import com.mycompany.myapp.util.ReflectUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareMixin;
import java.util.Objects;
@Aspect
public class BaseEntityObservabilityAspect {
@DeclareMixin("com.mycompany.myapp.model.BaseEntity")
public static ChangeSupport createChangeSupportImplementation(final BaseEntity baseEntity) {
return new ChangeSupportImpl(baseEntity);
}
// Intercept setters in all BaseEntity objects in order to notify about property change
@Around("this(baseEntity) && execution(public void set*(*))")
public void firePropertyChange(final BaseEntity baseEntity,
final ProceedingJoinPoint joinPoint) throws Throwable {
// Get property name from method name
final String setterName = joinPoint.getSignature().getName();
final String property = setterName.substring(3, 4).toLowerCase() + setterName.substring(4);
final ChangeSupport support = (ChangeSupport)baseEntity;
if (support.hasListeners(property)) {
// Get old value via reflection
final Object oldValue = ReflectUtils.invokeGetter(baseEntity, property);
// Proceed with the invocation of the method
joinPoint.proceed();
// New value is the first (and only) argument of this method
final Object newValue = joinPoint.getArgs()[0];
// Fire only if value actually changed
if (!Objects.equals(oldValue, newValue))
support.firePropertyChange(property, oldValue, newValue);
} else {
// No listeners have been registered with BaseEntity, so there is no need to fire property change event
joinPoint.proceed();
}
}
}
如果由于某种原因无法使用注释样式,这里使用 AspectJ 代码样式也是如此。
package com.mycompany.myapp;
import java.util.Objects;
import com.mycompany.myapp.model.BaseEntity;
import com.mycompany.myapp.util.ReflectUtils;
public aspect BaseEntityObservabilityAspect {
declare parents: BaseEntity extends ChangeSupportImpl;
// Intercept setters in all BaseEntity objects in order to notify about property change
void around(final BaseEntity entity, final ChangeSupport support):
this(entity) && this(support) && execution(public void BaseEntity+.set*(*)) {
// Get property name from method name
final String setterName = thisJoinPoint.getSignature().getName();
final String property = setterName.substring(3, 4).toLowerCase() + setterName.substring(4);
if (support.hasListeners(property)) {
final Object oldValue;
try {
// Get old value via reflection
oldValue = ReflectUtils.invokeGetter(entity, property);
} catch (final Throwable e) {
// Should not happen
proceed(entity, support);
return;
}
// Proceed with the invocation of the method
proceed(entity, support);
// New value is the first (and only) argument of this method
final Object newValue = thisJoinPoint.getArgs()[0];
// Fire only if value actually changed
if (!Objects.equals(oldValue, newValue))
support.firePropertyChange(property, oldValue, newValue);
} else {
// No listeners have been registered with BaseEntity, so there is no need to fire property change event
proceed(entity, support);
}
}
}
关于java - 完全支持 POJO 中的 JavaFX 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30142576/
1。 Set 的 parallelStream 没有使用足够的线程。 Java8 parallelStream 不能完全并行工作。在我的计算机中,当任务数小于处理器数时,java8 集的 parall
我想将位置发送到 Google Geocoding API,因此我想用 + 替换文本中的任何空格或逗号(因为可以接收)。 例如,所有这些样本应返回 Glentworth+Ireland: Glentw
所以我需要为将要上传的图像文件生成较小的预览,并且我必须在每个文件名的末尾附加“_preview”。 目前我正在这样做: uploadFile.map((file) => { if (fi
我们可以用参数定义类型同义词,这在与实际类型一起使用时效果很好: type MyType t = t String String data Test a b = Test a b f :: MyTyp
给定一个包含一些 TGraphic 后代的 Delphi TPicture,我需要计算像素颜色和不透明度。我认为我必须为每个类提供不同的实现,并且我认为我已经涵盖了 TPngImage。 32 位位图
我正在调试 Powershell 项目。我正在使用 Import-Module 从我的 C# dll 加载 PS 模块,一切正常。尽管调用 Remove-Module 并不会完全卸载模块,因为 DLL
有没有办法在ElasticSearch中要求完整(尽管不一定精确)匹配? 例如,如果一个字段具有术语"I am a little teapot short and stout",我想匹配" i am
我正在尝试根据日期范围连接两个表。 表A格式为: ID CAT DATE_START DATE_END 1 10 2018-01-01 2020-12-31 2
我最近加入了一家公司,在分析他们的环境时,我注意到 SharePoint web.config 的信任级别设置为“完全”。我知道这绝对是一个糟糕的做法,并且希望 stackoverflow 社区能够帮
我构建了一个完全依赖 AJAX 的 php/js 应用程序,因此没有任何内容是静态的。 我正在尝试找到一种方法来转换基于内容的广告,该广告使用 AJAX 交付的内容作为关键字。 Google 的 Ad
我正在尝试根据日期范围连接两个表。 表A格式为: ID CAT DATE_START DATE_END 1 10 2018-01-01 2020-12-31 2
我熟悉 FileSystemWatcher 类,并使用它进行了测试,或者我使用快速循环进行了测试,并在目录中列出了类型文件的目录列表。在这种特殊情况下,它们是 zip 压缩的 SDF 文件,我需要解压
按照 Disqus 上的教程进行操作时,评论框不会呈现。从 disqus 上找到的管理员看来,它的设置似乎是正确的。 var disqus_config = function () { this
是否可以使用 Cython 将 Python 3 应用程序完全编译/链接为可执行格式(当然假设所有使用的模块都是 cythonable)。 我在 Linux 下工作,我希望获得一个依赖性尽可能小的 E
我有一个 C# 控制台应用程序,而不是运行预构建步骤(以获取 NuGet 包)。 当我调试这个时,我想传入一个参数并显示控制台。当我不调试它时,我不想看到它。我什至不希望它在那里闪烁一秒钟。 我找到了
我在 n 个节点上有一个完整的 19 元树。我标记所有具有以下属性的节点,即它们的所有非根祖先都是最年长或最小的 child (包括根)。我必须为标记节点的数量给出一个渐近界限。 我注意到 第一层有一
我正在阅读一篇关于 Java Volatile 关键字的文章,遇到了一些问题。 click here public class MyClass { private int years;
一本书中写道——“如果问题 A 是 NP-Complete,则存在解决 A 的非确定性多项式时间算法”。但据我所知,"is"——NP 完全问题的答案可以在多项式时间内“验证”。我真的很困惑。能否使用非
考虑以下问题: 有N个硬币,编号为1到N。 你看不到它们,但是给出了关于它们的 M 个事实,形式如下: struct Fact { set positions int num_head
我想制作一个包装数字类型的类型(并提供额外的功能)。 此外,我需要数字和包装器可以隐式转换彼此。 到目前为止我有: template struct Wrapper { T value;
我是一名优秀的程序员,十分优秀!