0

我在@ApplicationException annoted类中没有调用@Inject和@PostConstruct方法有问题。我在服务(= ejb)层中使用Glassfish 3.0.1和JPA,CDI和EJB,并希望在会话语言中引发一个包含文本的errorMessage。JEE6 - @ApplicationException - @Inject和@PostConstruct不叫

我有一个抽象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没有被注入。

现在我问自己,如果有关于Exception类和CDI的限制,或者我没有找到问题的根源!

你知道答案吗?提前

托马斯

回答

0

感谢名单,问题就解决了。

我简单地使用异常作为throw new ConcreteException(),因为我已经习惯了年龄。很显然,这是问题所在。现在我将ConcreteException注入到EJB中并引发containercreated字段。这样@Inject和@Postconstruct正在工作!