0

I have a problem with @Inject and @PostConstruct method not called in a @ApplicationException annoted class. I'm using Glassfish 3.0.1 with JPA,CDI and EJBs in the service(=ejb)-layer and would like to throw an errorMessage that contains a text in the sessionlanguage.

I have an abstract 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;
 }}

And a concrete class:

 @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");
 }

}

The LocaleHandler (=Sessionscoped) should be injected to provide the currentLocale which is used to retrieve an errormessage from a bundle. The problem is, that the @PostConstruct is never called no matter what I try. I even annoted the concrete class with @Named, used @Inject in the concrete class instead of the abstract, but nothing works. When I call initText() directly I can see (in the debugger), that the LocaleHandler is not injected.

Now I'm asking myself if there is a restriction regarding Exception classes and CDI or did I simply not find the source of the problem !

Do you know the answer ?

thanx in advance

Thomas

1 Answers1

0

The problem is solved.

I simply used the Exception as throw new ConcreteException(), as I'm used to since ages. Excactly this was the problem. Now I inject the ConcreteException into my EJB and throw the containercreated field. This way @Inject and @Postconstruct are working !