0

我在@ApplicationException 注释类中没有调用@Inject 和@PostConstruct 方法有问题。我在服务(=ejb)层中使用带有 JPA、CDI 和 EJB 的 Glassfish 3.0.1,并且想抛出一个包含会话语言文本的 errorMessage。

我有一个抽象的 ExceptionClass

 public abstract class LocalizedException extends Exception {
        private static final long serialVersionUID = 1L;


 String localizedMessage;

 //This method should be called as @PostConstruct from the concrete classe
protected void setLocalizedMessage(LocaleHandler localeHandler, String key){
    this.setLocalizedMessage(localeHandler, key, new Object());
}

protected void setLocalizedMessage(LocaleHandler localeHandler, String key, Object... args){
    localizedMessage = ErrorMessages.getErrorMessage(key,localeHandler.getAktuelleLokale(),args);
}

 @Override
 public String getMessage() {
  return localizedMessage;
 }

 @Override
 public String getLocalizedMessage() {
  return localizedMessage;
 }}

还有一个具体的类:

 @ApplicationException
        public class ConcreteException extends LocalizedException {
        private static final long serialVersionUID = 2615388267911318734L;
 private int userId;

 public ConcreteException(int userId) {
     this.userId=userId;
 }

 public int getUserId() {
  return userId;
 }

 @PostConstruct
 @Inject  
 public void initText(LocaleHandler localeHandler){
         setLocalizedMessage(localeHandler, "msgKey");
 }

}

应该注入 LocaleHandler (=Sessionscoped) 以提供 currentLocale 用于从包中检索错误消息。问题是,无论我尝试什么,都不会调用 @PostConstruct。我什至用@Named 注释了具体类,在具体类中使用@Inject 而不是抽象类,但没有任何效果。当我直接调用 initText() 时,我可以看到(在调试器中)没有注入 LocaleHandler。

现在我问自己是否有关于异常类和 CDI 的限制,或者我根本没有找到问题的根源!

你知道答案吗 ?

提前谢谢

托马斯

4

1 回答 1

0

问题已经解决了。

我只是使用 Exception as throw new ConcreteException(),就像我自古以来就习惯的那样。这正是问题所在。现在我将 ConcreteException 注入我的 EJB 并抛出 containercreated 字段。这样@Inject 和@Postconstruct 正在工作!

于 2010-12-24T14:31:57.067 回答