如何使用Weld注入不可序列化的类(如java.util.ResourceBundle) [英] How to inject a non-serializable class (like java.util.ResourceBundle) with Weld

查看:203
本文介绍了如何使用Weld注入不可序列化的类(如java.util.ResourceBundle)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Producer,它可以将java.util.ResourceBundle注入任何类,以便轻松获取本地化的字符串。我的ResourceBundle-Producer如下所示:

I want to create a Producer that makes it possible to inject a java.util.ResourceBundle into any class in order to get localized Strings easily. My ResourceBundle-Producer looks like this:

public class ResourceBundleProducer {
  @Inject       
  public Locale locale;

  @Inject       
  public FacesContext facesContext;

  @Produces
  public ResourceBundle getResourceBundle() {
    return ResourceBundle.getBundle("/messages", locale )
  }
}

Locale和FacesContext的注入工作(从Seam 3 Alpha Source获取相应的生产者)。但不幸的是,ResourceBundle不是Serializable,因此无法以这种方式生成。当我试图访问一个调用使用我的ResourceBundle的bean的JSF页面时,我从Weld得到以下错误:

The Injection of Locale and FacesContext works (took the corresponding producers from the Seam 3 Alpha Source). But unfortunately, ResourceBundle is not Serializable and therefore can't be produced in this way. I'm getting the following Error from Weld when trying to access a JSF-page which calls a bean that uses my ResourceBundle:


引起的:org.jboss.weld.IllegalProductException:WELD-000054生产者不能产生用于注射不可序列实例为钝化beans\\\\
\\\\
Producer\的非瞬态字段:org.jboss.weld .bean- / d:/ Program Files文件(x86)的/GlassFish-Tools-Bundle-For-Eclipse-1.2/glassfishv3/glassfish/domains/teachernews/applications/teachernews/-ProducerMethod-services.producers.ResourceBundleProducer.getResourceBundle()\\ \\
Injection Point \:field web.PersonH​​ome.bundle

有没有办法让我的ResourceBundleResolver工作?或者是否有其他机制来获得类似的功能?
提前致谢!

Are there any ways to get my ResourceBundleResolver to work? Or are there any other mechanisms to get a similar functionality? Thanks in advance!

编辑:

好的,我我会花掉一些难得的积分;)
也会接受这个问题的好方法!

Okay, i'll spent some of my hardly earned points ;) Will also accept a good workaround for this issue!

我有另一个例子,创建一个制作人没有'工作:FlashProducer。还无法生成FacesContext-Flash,因为Flash不可序列化。

I got another example where creating a Producer doesn't work: a FlashProducer. A FacesContext-Flash also cannot be produced because Flash isn't serializable.

推荐答案

首先,ResourceBundle 不可序列化。请参见此处。消息很明确

Well, First of all ResourceBundle is not Serializable. See here. And The message is clear


无法生成非序列化实例,以便注入非瞬态钝化豆类的字段

cannot produce non-serializable instances for injection into non-transient fields of passivating beans

钝化豆 ???我认为 web.PersonH​​ome 是有状态会话Bean或@ConversationScoped bean。我对吗 ???如果是这样,你应将你的捆绑属性标记为瞬态

passivating beans ??? I Think web.PersonHome is Either a Stateful Session Bean or a @ConversationScoped bean. Am i right ??? If so you should mark your bundle property as transient

private transient @Inject ResourceBundle bundle;

这篇关于如何使用Weld注入不可序列化的类(如java.util.ResourceBundle)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆