7 Replies Latest reply on Dec 24, 2010 9:21 AM by Thomas Kernstock

    JEE6 - @ApplicationException - @Inject and @PostConstruct not called

    Thomas Kernstock Newbie

      Hi,


      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;
          
           @Inject 
           protected LocaleHandler locHandler;
           
           String localizedMessage;
           
                  //This method should be called as @PostConstruct from the concrete classe
           protected void setLocalizedMessage(String key){
            this.setLocalizedMessage(key, new Object());
           }
           
           protected void setLocalizedMessage(String key, Object... args){
            localizedMessage = ErrorMessages.getErrorMessage(key,locHandler.getCurrentLocale(),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 
            public void initText(){
               setLocalizedMessage("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