- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我已经使用 int getID()
和 MyEnum withID(int)
方法构建了许多 Enum 类,这些方法允许我将 ID 专用于枚举值以实现持久性(从而避免由于枚举外部存储的顺序/名称更改而导致的更改)。
我想构建一个自定义转换器,它会进行一些反射以查找这些方法,并在找不到它们时使用它们或备份到 Ordinal/String 转换。
任何人都可以使用通用枚举转换器吗?这只是我第二次涉足 Converters。
最佳答案
我想说您正在尝试解决错误的问题。我通常将枚举保留为字符串,从而首先避免这个问题。缺点当然是更大的 DB 字段,但这并不重要。
也就是说:
我会说一般来说这是可能的,但不是以一种干净的方式。我要做的是让所有这些枚举实现一个通用接口(interface)(只是一个标记接口(interface)或一个包含 int getId()
方法的接口(interface))。现在只为这个接口(interface)注册你的 PropertyEditor,这样你就不会破坏太多的标准功能。
然后,您的下一个问题是您依赖于静态工厂方法,这不能以通用方式完成。你的 PropertyEditor 当然可以:
enumClass.getDeclaredMethod("withId", int.class).invoke(id)
但我认为这很 hacky。像这样的东西怎么样:
Object target = null;
for(Object e : EnumSet.allOf(yourEnumClass)){
if(e instanceof MyInterface && ((MyInterface)e).getId()==thisId){
target = e;
break;
}
}
return target;
现在您没有使用任何静态工厂方法,并且您具有编译时安全性,只要您的枚举实现一个通用接口(interface)。
更新:使用新的 Converter SPI 变得更容易。使用a custom ConverterFactory :
public class CustomEnumConverterFactory implements
ConverterFactory<String, Enum<?>>{
@Override
public <T extends Enum<?>> Converter<String, T> getConverter(
final Class<T> targetType){
return WithId.class.isAssignableFrom(targetType)
? new EnumWithIdConverter(targetType)
: new StandardEnumConverter(targetType);
}
}
这样注册:
<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<!-- converters is a set of both converters and converterfactories -->
<bean class="foo.bar.CustomEnumConverterFactory" />
</property>
</bean>
关于java - 所有枚举的 Spring 自定义转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5178622/
我在完成这个用于转换咖啡价格的 JavaScript 时遇到问题。我下载了一个调试器,它一直告诉我价格未定义,我不明白。这是脚本。 Coffee House
我有一个使用以下方法的 JSF 转换器: @Override public Object getAsObject(FacesContext context, UIComponent compo
我正在寻找类似paint.net 或Gimp 的东西,但对于音频文件,并在Windows 上运行。 最佳答案 Audacity太棒了 关于audio - 免费的声音编辑器/转换器?,我们在Stack
我目前正在使用以下代码来缩进 XML: transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputPr
我收到以下错误:Conversion Error setting value 'home' for 'null Converter'. Home是所显示内容的字符串表示形式。 对我来说,这没有意义。这
我的 UI 中有很多数字要处理。我希望它们中的一些没有小数位,一些是小数点后 2 位,而另一些是小数点后(3 位或 4 位小数)。 我有一个名为 DoubleToStringConverter 的转换
我正在制作一个货币转换器。转换器需要一个最小范围和最大范围,如果货币值高于或低于这些范围,转换器将要求您更改范围以能够转换货币。例如,如果用户将范围设置在 min-range 3 和 max-rang
我正在使用 Spring Shell 2 CLI,并尝试通过反射在运行时从定义的接口(interface)生成命令。 更新:接口(interface)的实现也是在运行时生成的。 我正在使用 Confi
我正在尝试编写一个通用的 Converter 以在我的代码中的多个类似情况下使用。我有一组子类,我只想使用一个 Converter 来处理,所以我想将一些东西(类类型/一些参数/等)传递给 Conve
我正在尝试读取一个在每个单元格中包含多个值的 csv 文件,并且我想将它们编码为单个 int 格式的字节以存储在 Pandas 单元格中,(例如 (1, 1) -> 771)。为此,我想使用 read
我正在 VC2013 中开发一个 c# Windows Phone 8.1 应用程序,并偶然发现了一个奇怪的问题。 为了使我的代码更“干净”,我决定将应用程序的不同部分放入不同的文件夹中。在 c# 代
是否有将 Puppet 脚本转换为 Chef 的转换器? 我找到了将 Chef 脚本转换为 Puppet 的 ruby 脚本 https://github.com/relistan/chef2pu
我已经开始寻找很好的解决方案,如何使用 Spring CassandraOperations 很好地持久化实体。问题开始是因为我的实体中的某些字段不受 cassandra 支持,例如乔达日期时间。 解
我知道如何实现单链表 monad 转换器,但无法运行其对应的数组。问题是存在分组效应,这使得转换器仅对可交换基 monad 有效。这是一个示例,为了简单起见,转换器和基础 monad 都是数组,并且没
当我尝试将值转换器从定义的枚举状态绑定(bind)到刷子时,我的 XAML 设计器中出现错误: 未找到“OKStatus”资源。 该应用程序在运行时运行良好,但我无法在设计器中看到我的 GUI。 我的
我需要使用列表单子(monad)变压器。我读到 ListT IO 存在潜在问题来自 Control.Monad.List , 自 IO不是可交换的,所以我在看 ListT done right .但我
不用多想,在我看来,一大组 Prolog 的功能可以实现为关系演算(a.k.a. SQL)。 有没有人听说过任何工具可以自动将 Prolog 转换为 SQL? 最佳答案 推荐: https://www
假设我在十六进制值(包括 alpha)中有这种颜色: x [1] "255 36 0" 但是,查看您请求的结果,您似乎在 x 中将 alpha 值作为第一个十六进制数。 - 所以你需要创建一个子字符
我正在寻找可用于跟踪程序进度的 monad 转换器。要解释如何使用它,请考虑以下代码: procedure :: ProgressT IO () procedure = task "Print som
我有一个非常基本的需求,即从数据库中获取一些数据并返回一个 DTO。我发现使用 nHibernate 连接多个表和“投影”可以说,到 DTO 是相当多的代码。在查看了几个示例后,大多数示例都不起作用,
我是一名优秀的程序员,十分优秀!