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

问题描述

我想创建一个生产者,它可以将 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 生产者不能产生不可序列化的实例来注入钝化 bean 的非瞬态字段\n\nProducer: 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()\nInjection Point: fieldweb.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!

我得到了另一个创建 Producer 不起作用的示例:FlashProducer.由于 Flash 不可序列化,因此也无法生成 FacesContext-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

无法生成不可序列化实例以注入到钝化bean的非瞬态字段

钝化 bean ???我认为 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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 23:12