[英]Java - Key-Value Decision Making
What would be the best way to store the following Business Rules in a File so that they can be applied on the input values which would be keys? 将以下业务规则存储在文件中以便可以将它们应用于键的输入值的最佳方法是什么?
Key-INDIA; Value-Delhi.
Key-Australia; Value-Canberra.
Key-Germany, Value-Berlin.
One Solution :- Xml 一种解决方案:-Xml
<Countries>
<India>Delhi</India>
<Australia>Canberra</Australia>
<Germany>Berlin</Germany>
</Countries>
As the number of rules are going to be > 1000; 由于规则数量将> 1000; implementing it using Map is not possible. 无法使用Map实施。
Regards, Shreyas. 问候,Shreyas。
Use .properties
file and store them in a key value pair. 使用.properties
文件并将其存储在键值对中。
India=Delhi.
Australia=Canberra.
Germany=Berlin.
And use java.util.Properties
to read that file as told by hmjd. 并按照hmjd的说明使用java.util.Properties
读取该文件。
For Example : 例如 :
Properties prop = new Properties();
try {
//load a properties file
prop.load(new FileInputStream("countries.properties"));
//get the property value and print it out
System.out.println(prop.getProperty("India"));
System.out.println(prop.getProperty("Australia"));
System.out.println(prop.getProperty("Germany"));
} catch (IOException ex) {
ex.printStackTrace();
}
Use java.util.Properties
to write, and read, from file: 使用java.util.Properties
来读写文件:
Properties p = new Properties();
p.setProperty("Australia", "Canberra");
p.setProperty("Germany", "Berlin");
File f = new File("my.properties");
FileOutputStream fos = new FileOutputStream(f);
p.store(fos, "my properties");
Use p.load()
to read them back from file and p.getProperty()
to query them once loaded. 使用p.load()
从文件中读取它们,并使用p.getProperty()
在加载后查询它们。
Create a properties file (like file.properties): 创建一个属性文件(如file.properties):
INDIA=Delhi.
Australia=Canberra.
Germany=Berlin.
Then in the code: 然后在代码中:
public static void main(String[] args) {
Properties prop = new Properties();
try {
prop.load(new FileInputStream("file.properties"));
String value= prop.getProperty("INDIA");
...
} catch (Exception e) {
}
}
Have you looked at Spring configuration ? 您是否看过Spring配置 ? That way you can/create a map in config and store object definitions per key. 这样,您可以/在配置中创建映射并按键存储对象定义。 eg 例如
<map>
<entry key="India" value="Delhi">
</map>
You're talking about business rules but at the moment you're simply storing a key/value pair. 您在谈论业务规则,但此刻您仅存储键/值对。 If those rules become more complex then a simple key/value pair won't suffice. 如果这些规则变得更加复杂,那么简单的键/值对就不够了。 So perhaps you need something like: 因此,也许您需要类似:
Map<String, Country>
in your code, and Country is an object with (for now) a capital city, but in the future it would contain (say) locations, or an international phone number prefix, or a tax rule etc. In Spring it would resemble: 在您的代码中,“国家/地区”是(目前)拥有首都的对象,但将来它将包含(例如)地点,国际电话号码前缀或税法等。在春季,它类似于:
<map>
<entry key="India" ref="india"/>
</map>
<!-- create a subclass of Country -->
<bean id="india" class="com.example.India">
I realise this is considerably more complex than the other suggestions here. 我意识到这比这里的其他建议要复杂得多。 However since you're talking about rules, I suspect you'll be looking to configure/define some sort of behaviour. 但是,由于您在谈论规则,所以我怀疑您将寻求配置/定义某种行为。 You can do this using properties (or similar) but likely you'll end up with different properties sets for the different behavioural aspects of your rules. 您可以使用属性(或类似属性)来执行此操作,但最终可能会为规则的不同行为方面使用不同的属性集。 That rapidly becomes a real maintenance nightmare. 迅速成为真正的维护噩梦。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.