JEE6 - @ApplicationException - @Inject 和 @PostConstruct 未调用

发布于 2024-10-08 13:40:39 字数 1726 浏览 5 评论 0原文

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

我有一个抽象的 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 的限制,或者我只是没有找到问题的根源!

你知道答案吗?

提前感谢

托马斯

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等风来 2024-10-15 13:40:39

问题解决了。

我只是将异常用作抛出新的ConcreteException(),就像我多年来所习惯的那样。正是这个问题。现在,我将 ConcreteException 注入到我的 EJB 中并抛出 containercreated 字段。这样 @Inject 和 @Postconstruct 就可以工作了!

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 !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文