关于java:在Web项目中定义一组常量的最佳方法是什么?

What is the best way to define a set of constants in a web project?

本问题已经有最佳答案,请猛点这里访问。

我正在开发一个EJB项目,我需要定义一组常量,这些常量可以在整个项目中使用,所以我创建了一个保存常量集的接口我的问题是,有没有更好的方法来定义Web项目中的常量?

这是我不变界面的一个例子:

1
2
3
4
5
public interface ejbConstant {
    final String  CONST23="AB23";
    final String  CONST24="AB24";
    // ..........
}

谢谢你的建议


声纳用例子对此有很好的解释

根据《有效Java》的作者Joshua Bloch

The constant interface pattern is a poor use of interfaces.

That a class uses some constants internally is an implementation
detail. Implementing a constant interface causes this implementation
detail to leak into the class's exported API. It is of no consequence
to the users of a class that the class implements a constant
interface. In fact, it may even confuse them. Worse, it represents a
commitment: if in a future release the class is modified so that it no
longer needs to use the constants, it still must implement the
interface to ensure binary compatibility. If a nonfinal class
implements a constant interface, all of its subclasses will have their
namespaces polluted by the constants in the interface.