如何最好地从数据库或 servlet 上下文初始化常量?
我们在应用程序的接口中声明了常量,如下所示。
public interface IConstants
{
public static final String FEVER="6";
public static final String HEADACHE="8";
}
我们现在想要从数据库(或应用程序 servlet 上下文)填充这些常量值(6 和 8)。
存储在查找表中的数据库值已经在应用程序会话中可用(在 servlet 上下文属性中),因此我不必每次都进行数据库调用。
我们如何实现这一目标?
We have constants declared in an interface in our application like this.
public interface IConstants
{
public static final String FEVER="6";
public static final String HEADACHE="8";
}
We now want to populate these constants values (6 and 8) from the database (or application servlet context).
The database values stored in a look up table are already available in the application session (in servlet context attribute) and hence I don't have to make a database call every time.
How do we accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
鉴于您不想对现有代码进行太多更改,最简单的方法是:
Given that you don't want to change the existing code too much, the simplest way would be:
由于常量值保存在数据库中,因此最好的方法是创建一个枚举类型并通过它使其可用。
Since the constant values are persisted in a database, the best approach would be to create an enum type and make them available through it.
你确定需要吗?!
如果您在添加新疾病时需要添加新代码,那么将“常量”设为数据驱动的静态(非最终)全局变量就没有什么意义。
(这只会使事情变得复杂。)
如果您担心数据库和代码之间不匹配,您可以在启动应用程序时验证数据库和常量是否匹配。 您在表中定义了疾病,并且正在使用某种引用完整性,对吗?
如果您确实认为您需要一种数据驱动的方法,那么您可能根本不需要任何“已知疾病”字段,因为代码不应该真正依赖于这些字段。 在这种情况下,每种疾病都应该是一个具有身份和其他属性的适当对象。
如果您需要对某些类型进行特殊处理,您可能应该再次返回到枚举...
如果您有多种疾病(可能会动态添加)并且只有几种类型 - 多种疾病通过相同的代码以相同的方式处理; 在疾病表和代码中添加一个类型作为疾病类型的枚举(或常量),并使用它来运行不同的逻辑。
如果每种疾病实际上都非常复杂,那么尝试为每种疾病编写一个类可能是个好主意。
(然后您可以对子类和所有疾病进行适当的 O/R 操作...)
自java 5以来,接口枚举cludge就不再是必需的,因为你可以获得两个枚举
并根据需要进行静态导入。
(当使用枚举时,验证数据库会更简单,因为您可以免费获得枚举。)
您的字段可能应该位于名为“Disease”或“SickDude”的类中,而不是位于巨大的全局常量类中。 (例如,日历中的字段)。 可能作为它们“所属”类中的内部公共枚举。
Are you sure you need to?!
If you need to add new code when adding a new disease anyways, there is little point in making the "constants" to be data driven static (non-final) globals.
(It will just complicate things.)
If you are worried about a mismatch between the database and the code, you can verify that database and the constants match when starting up the application. You have the diseases defined in a table and are using some kind of referential integrity right?
If you DO think You need a data driven approach, you probably should not need any fields for "known diseases" at all, as the code shouldn't really depend on those. In that case each disease should be a proper object with a identity and other properties.
If you need special handling for certain types you probably should go back to an enum again...
If you instead have many diseases (that might be added dynamically) and only a few types - several diseases are handled in the same way by the same code; add a type in the disease-table and in the code as an enum (or constant) for the type-of-disease and use that to run different logic.
If each disease actually have a lot of complexity, it might be a good idea to try to write one class for each disease.
(You can then do a proper O/R thing with sub-classes and all...)
The interface enum cludge is not neccesary since java 5, as you can get both enums
and do a static import if you need to.
(And when using enum, validating the database is simpler as you get an enumeration for free.)
Your fields should probably be located in a class called Disease or SickDude instead of a gigantic global constants class. (Like, for instance, the fields in Calendar). Possibly as an inner public enum in the class where they "belong."
为了使它们根据数据源而变化,您需要它们的某种数据结构。
您可以定义一个 bean 来保存值(具有 getter/setter 的字段),或者使用值的映射。
您可能应该认为这更像是配置问题而不是常量问题。 常量实际上是从编程的角度来看的。
To make them vary based on a data source, you'd need some kind of data structure for them.
You could define a bean to hold the values (fields with getters/setters), or use a map for the values.
You should probably think of this as more like a configuration problem than a constants problem. Constants are really intended from a programming point of view.
作为常数,你不能轻易做到。
使用这些成员创建一个类,保护 setter(或从构造函数设置它们)并提供 getter 方法。
更多细节:
好的,首先,您不能轻易地从数据库初始化常量,因为它们必须在定义时初始化。
相反,创建一个类似这样的类:
现在,当您构造该类时,您设置了 FEVER 和 HEADACHE。 由于它们是静态的,因此只有一个值; 因为你只有 getter 并且它们是私有的,所以没有人可以重置它们; 由于它们是最终的,所以没有人可以偷偷地在你背后继承遗产。
As constants, you can't readily.
Create a class with those members, protect the setters (or set them from the constructor) and provide getter methods.
More detail:
Okay, to start with, you can't readily initialize constants from a database because they must be initialized at the time they're defined.
Instead, create a class something like this:
Now, when you construct the class, you set FEVER and HEADACHE. Since they're static, there's only one value; since you have only getters and they're private, no one can reset them; and since they're final, no one can sneak behind your back with inheritance.