在Spring中加载ResourceBundle对象

发布于 2024-12-25 22:34:43 字数 1585 浏览 2 评论 0原文

我需要在 JBoss AS7 上运行的已部署 WAR 的上下文中将 Spring Web 应用程序的 i18n 本地化属性作为 ResourceBundle 对象加载。

这里的原因是我需要将 ResourceBundle 对象作为参数提供给 JasperReports 才能正确编译报告。 JasperReports 使用参数“REPORT_RESOURCE_BUNDLE”通过 str() 函数或 $R{} 表达式绑定来处理报告中的国际化。此参数唯一接受的类型是 ResourceBundle 对象,因此是我的情况。

我在某个时候让它工作,但似乎 JBoss 保留了属性的陈旧版本,我相信这一点可以通过以下事实得到澄清:在我进行了干净的打包之后,它无法再使用未修改的代码找到 ResourceBundle,并在项目上重新部署。

在 Spring 中,我们配置了一个 MessageSource Bean,如下所示,它能够成功加载并使用属性文件作为资源包:

@Bean 
public MessageSource messageSource() {
  ReloadableResourceBundleMessageSource ms = new ReloadableResourceBundleMessageSource();
  String[] basenames = {"/WEB-INF/classes/messages/i18n/core_messages",
                        "/WEB-INF/classes/messages/i18n/mui_messages"};
  ms.setBasenames(basenames);
  return ms;
}

我当前获取 ResourceBundle 对象的实现如下:

public ResourceBundle getBeanResourceBundle() {
  Locale fromConfig = DEFAULT_LOCALE;

  //Resolve Locale from a configuration service

  ClassLoader cl = Thread.currentThread().getContextClassLoader();
  return ResourceBundle.getBundle("core_messages", fromConfig, cl);
}

使用 this.getClassLoader( 加载类时遇到问题),然后在我更改为从 Thread.currentThread() 加载后开始工作。然而现在 clean:package:deploy 已经使其无效,并且我有合理的信息向我建议它实际上停止加载新的“core_messages”ResourceBundle 对象许多部署回来,因为明显的键值更改在已部署的应用程序中似乎从未发生更改。

部署的结构是通过 maven 构建的,如下所示:

将核心模块打包为 JAR,该 JAR 是前端模块的依赖项,前端模块打包为 JBoss 部署的 WAR。

具有加载诸如上述方法中的 ResourceBundle 之类的服务的代码位于 Core 模块中。最后的 WAR 是前端模块之一,它尝试解析其 WEB-INF/classes/.. 类路径中的文件。

我需要访问什么类加载器才能从适当的类路径加载?

I need to load the i18n localization properties of my Spring web application as a ResourceBundle object in the context of a deployed WAR running on JBoss AS7.

The reason here is that I need to feed in a ResourceBundle object as a parameter to JasperReports to compile reports properly. The parameter "REPORT_RESOURCE_BUNDLE" is used by JasperReports to handle internationalization in the reports with the str() function or the $R{} expression binding. The only accepted type for this parameter is a ResourceBundle object, hence my situation.

I had it working at some point, but it seemed that JBoss was holding onto a stale version of the properties, which I believe was clarified by the fact that it could no longer locate the ResourceBundle using unmodified code after I did a clean, package, and redeploy on the project.

In Spring we have configured a MessageSource Bean as follows, that is able to successfully load and use the properties files as resource bundles:

@Bean 
public MessageSource messageSource() {
  ReloadableResourceBundleMessageSource ms = new ReloadableResourceBundleMessageSource();
  String[] basenames = {"/WEB-INF/classes/messages/i18n/core_messages",
                        "/WEB-INF/classes/messages/i18n/mui_messages"};
  ms.setBasenames(basenames);
  return ms;
}

My current implementation for fetching the ResourceBundle object is as follows:

public ResourceBundle getBeanResourceBundle() {
  Locale fromConfig = DEFAULT_LOCALE;

  //Resolve Locale from a configuration service

  ClassLoader cl = Thread.currentThread().getContextClassLoader();
  return ResourceBundle.getBundle("core_messages", fromConfig, cl);
}

It was having issues loading the class using this.getClassLoader() and then started working after I changed to load from the Thread.currentThread(). Yet now the clean:package:deploy has rendered this ineffective and I have reasonable information to suggest to me it stopped actually loading new "core_messages" ResourceBundle objects many deployments back because obvious key-value changes never appeared to change in the deployed application.

The structuring of the deployment is build through maven as follows:

Core module packages as a JAR that is a dependency for the FrontEnd modules that are packaged as WARs for JBoss deployment.

The code that has the services to load things like the ResourceBundle in the method above are in the Core module. The final WAR is one of the FrontEnd modules that is trying to resolve the files from its WEB-INF/classes/.. classpath.

What class loader do I need to access to load from the appropriate classpath?

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

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

发布评论

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

评论(1

挽你眉间 2025-01-01 22:34:43

Spring 中的 MessageSourceResourceBundle 类是 ResourceBundle 的实现,它使用 MessageSource 来提供其属性。它可以使用 MessageSource 对象 (Autowired) 和 Locale 进行实例化。 MessageSource 已经处理了我在应用程序中需要的内容,我只需要获取一个工作的 ResourceBundle,它与 Locale 的功能一样好。

public ResourceBundle getBeanResourceBundle() {
  Locale fromConfig = DEFAULT_LOCALE;

  //Resolve Locale from a configuration service

  return new MessageSourceResourceBundle(ms, fromNodeConfig); //replace code in my question
}

The MessageSourceResourceBundle class in Spring is an implementation of ResourceBundle that uses the MessageSource to feed its properties. It can be instantiated with a MessageSource object (Autowired) and a Locale. MessageSource already handles what I need in the applications and I just needed to grab a working ResourceBundle which it does as well with Locale.

public ResourceBundle getBeanResourceBundle() {
  Locale fromConfig = DEFAULT_LOCALE;

  //Resolve Locale from a configuration service

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