Spring MessageSource是否支持多类路径? [英] Does Spring MessageSource Support Multiple Class Path?

查看:268
本文介绍了Spring MessageSource是否支持多类路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring框架为基于Web的应用程序设计插件系统。插件是类路径上的jar。所以我能够得到像jsp这样的资源,见下文

I am designing a plugin system for our web based application using Spring framework. Plugins are jars on classpath. So I am able to get sources such as jsp, see below

ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] pages = resolver.getResources("classpath*:jsp/*jsp");

到目前为止一切顺利。但我的messageSource有问题。在我看来, ReloadableResourceBundleMessageSource #setBasename 通过classpath *:执行不支持多个类路径如果我只使用classpath:,我只获取messageSource只有一个插件。

So far so good. But I have a problem with the messageSource. It seems to me that ReloadableResourceBundleMessageSource#setBasename does NOT support multiple class path via the "classpath*:" If I use just "classpath:", I get the messageSource just only from one plugin.

有没有人知道如何从所有插件注册messageSources?是否存在MessageSource的这种实现?

Does anyone have an idea how to register messageSources from all plugins? Does exist such an implementation of MessageSource?

推荐答案

这里的问题不是多个类路径或类加载器,而是有多少资源代码将尝试加载给定路径。

The issue here is not with multiple classpaths or classloaders, but with how many resources the code will try and load for a given path.

classpath * 语法是一种Spring机制,允许用于为给定路径加载多个资源的代码。非常便利。但是, ResourceBundleMessageSource 使用标准 java.util.ResourceBundle 来加载资源,这是一个更简单,更简单的方法机制,它将加载给定路径的第一个资源,并忽略其他所有内容。

The classpath* syntax is a Spring mechanism, one which allows code to load multiple resources for a given path. Very handy. However, ResourceBundleMessageSource uses the standard java.util.ResourceBundle to load the resources, and this is a much simpler, dumber mechanism, which will load the first resource for a given path, and ignore everything else.

我真的没有一个简单的修复程序。我想你将不得不抛弃 ResourceBundleMessageSource 并编写一个 MessageSource 的自定义实现(很可能是通过子类化 AbstractMessageSource )使用 PathMatchingResourcePatternResolver 查找各种资源并通过 MessageSource 界面。 ResourceBundle 不会有多大帮助。

I don't really have an easy fix for you. I think you're going to have to ditch ResourceBundleMessageSource and write a custom implementation of MessageSource (most likely by subclassing AbstractMessageSource) which uses PathMatchingResourcePatternResolver to locate the various resources and expose them via the MessageSource interface. ResourceBundle isn't going to be much help.

这篇关于Spring MessageSource是否支持多类路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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