Java中的继承和静态成员
我正在开发一个包含几个模块的项目。我想在每个模块中提供一个自定义异常的功能,该异常可以使用自定义 error_code-error_message 对从属性文件静态填充内部结构,例如 HashMap。 我有一个包含静态属性的基本抽象自定义异常:
public abstract class AbstractException extends RuntimeException{
public static Map<String, String> ERRORS = new HashMap<String, String>();
public String code;
// getters and setter for code ommited
public static init(String fileName, Class<?> clazz){
// read properties file
// populate map
}
public static String getMessageByCode(String code){
//
String mess = ERRORS.get(code);
// in case of null message provide default message about unknown error
}
public AbstractException(String code){
super(getMessageByCode(code));
this.setCode(code);
}
public AbstractException(Throwable thr){
super(getMessageByCode("ERROR_999"), thr);
this.setCode(code);
}
public AbstractException(Throwable thr, String msg){
super(getMessageByCode("ERROR_999") + " " + msg, thr);
this.setCode(code);
}
}
简单自定义异常
public class MyException extends AbstractException{
static{
// populate internal map with module-specific errors
init("module1.errors.properties", MyException.class);
}
public MyException(String code){
super(getMessageByCode());
}
// rest code omited
}
在代码中自定义异常的简单用法:
throw new MyException("ERROR_404");
我在这段代码中看到的问题:
- 抽象异常的所有子类都存在错误映射
- 对静态错误字段的并发访问。
问题是,如何避免这些问题,也许有人有更好的解决方案来解决我的问题?
I am working on a project which contains a couple of modules. I want to provide the ability in every module to have a custom exception that statically populates internal structure, e.g. HashMap, from property file with custom error_code-error_message pairs.
I have a base abstract custom exception what contains a static property:
public abstract class AbstractException extends RuntimeException{
public static Map<String, String> ERRORS = new HashMap<String, String>();
public String code;
// getters and setter for code ommited
public static init(String fileName, Class<?> clazz){
// read properties file
// populate map
}
public static String getMessageByCode(String code){
//
String mess = ERRORS.get(code);
// in case of null message provide default message about unknown error
}
public AbstractException(String code){
super(getMessageByCode(code));
this.setCode(code);
}
public AbstractException(Throwable thr){
super(getMessageByCode("ERROR_999"), thr);
this.setCode(code);
}
public AbstractException(Throwable thr, String msg){
super(getMessageByCode("ERROR_999") + " " + msg, thr);
this.setCode(code);
}
}
Simple custom exception
public class MyException extends AbstractException{
static{
// populate internal map with module-specific errors
init("module1.errors.properties", MyException.class);
}
public MyException(String code){
super(getMessageByCode());
}
// rest code omited
}
Simple usage of custom exception in code:
throw new MyException("ERROR_404");
Propblems what I can see in this code:
- ERRORS map exists for all child classes of abstract exception
- Concurrent access to static ERRORS field.
Question is, how to avoid these problems, may be someone have a better solution of my problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这种设计行不通,因为只有一份
ERRORS
副本,在所有子类之间共享。一种可能的解决方案是 ExceptionFactory,它管理各种ERRORS
映射,并可以为您创建所需子类的异常。例如This design won't work because there will be only one copy of
ERRORS
, shared among all subclasses. One possible solution is an ExceptionFactory that manages the variousERRORS
maps and can create and exceptions of the required subclasses for you. For example