使用Spring IoC设置枚举值 [英] Using Spring IoC to set up enum values
问题描述
我想要做的是在类加载时注入,以下代码片段中的硬编码值:
public enum Car
{
NANO 非常便宜,印度),
MERCEDES(昂贵,德国),
FERRARI(非常昂贵,意大利);
public final String cost;
public final String madeIn;
Car(String cost,String madeIn)
{
this.cost = cost;
this.madeIn = madeIn;
}
}
让我们说应用程序必须部署在德国,Nanos几乎免费,或在法拉利不负责任的印度。在这两个国家,只有三辆车(确定性集),不再少于一个枚举,而是一个枚举,但它们的内在值可能会有所不同。所以,这是一个不可变的上下文初始化的情况。
你的意思是设置枚举
本身?
我不认为这是可能的。您不能实例化枚举,因为它们具有 static
性质。所以我认为Spring IoC不能创建 枚举
。
另一方面,如果您需要使用枚举
初始化某些内容,请查看 Spring IoC章节。 (搜索枚举)有一个简单的例子可以使用。
Is there a way to set up such enum values via Spring IoC at construction time?
What I would like to do is to inject, at class load time, values that are hard-coded in the code snippet below:
public enum Car
{
NANO ("Very Cheap", "India"),
MERCEDES ("Expensive", "Germany"),
FERRARI ("Very Expensive", "Italy");
public final String cost;
public final String madeIn;
Car(String cost, String madeIn)
{
this.cost= cost;
this.madeIn= madeIn;
}
}
Let's say that the application must be deployed in Germany, where Nanos are "Nearly free", or in India where Ferraris are "Unaffordable". In both countries, there are only three cars (deterministic set), no more no less, hence an enum, but their "inner" values may differ. So, this is a case of contextual initialization of immutables.
Do you mean setting up the enum
itself?
I don't think that's possible. You cannot instantiate enums because they have a static
nature. So I think that Spring IoC can't create enums
as well.
On the other hand, if you need to set initialize something with a enum
please check out the Spring IoC chapter. (search for enum) There's a simple example that you can use.
这篇关于使用Spring IoC设置枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!