gpt4 book ai didi

java - 找不到属性文件 ReloadableResourceBundleMessageSource 错误

转载 作者:行者123 更新时间:2023-11-29 08:18:09 26 4
gpt4 key购买 nike

我正在尝试使用可重新加载的 spring 资源包,但 spring 找不到该文件。我尝试了很多不同的路径,但无法让它在任何地方工作。在下面的代码中,您将看到我从同一路径变量加载了 spring 包和常规包,但只有一个有效。

我已经为此苦苦挣扎太久了。大家有什么想法吗?

日志文件


INFO 2010-04-28 11:38:31,805 [main] org.myorg.test.TestMessages: C:\www\htdocs\messages.properties
INFO 2010-04-28 11:38:31,805 [main] org.myorg.data.Messages: initializing Spring Message Source to C:\www\htdocs\messages.properties
INFO 2010-04-28 11:38:31,821 [main] org.myorg.data.Messages: Attempting to load properties from C:\www\htdocs\messages.properties
DEBUG 2010-04-28 11:38:31,836 [main] org.springframework.context.support.ReloadableResourceBundleMessageSource: No properties file found for [C:\www\htdocs\messages.properties_en_US] - neither plain properties nor XML
DEBUG 2010-04-28 11:38:31,842 [main] org.springframework.context.support.ReloadableResourceBundleMessageSource: No properties file found for [C:\www\htdocs\messages.properties_en] - neither plain properties nor XML
DEBUG 2010-04-28 11:38:31,848 [main] org.springframework.context.support.ReloadableResourceBundleMessageSource: No properties file found for [C:\www\htdocs\messages.properties] - neither plain properties nor XML
INFO 2010-04-28 11:38:31,848 [main] org.myorg.test.TestMessages: I am C:\www\htdocs\messages.properties

消息.java


package org.myorg.data;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;

public class Messages {
protected static final Log logger = LogFactory.getLog(Messages.class);
private static ReloadableResourceBundleMessageSource msgSource = null;
private static ResourceBundle RESOURCE_BUNDLE;

public static final String PATH = "C:" + File.separator + "www"
+ File.separator + "htdocs" + File.separator + "messages.properties";

private Messages() {

}

public static String getString(String key) {
initBundle();

return msgSource.getMessage(key, null,
RESOURCE_BUNDLE.getString(key), null);
}

private static void initBundle(){
if(null == msgSource || null == RESOURCE_BUNDLE){
logger.info("initializing Spring Message Source to " + PATH);
msgSource = new ReloadableResourceBundleMessageSource();
msgSource.setBasename(PATH);
msgSource.setCacheSeconds(1);

FileInputStream fis = null;
try {
logger.info("Attempting to load properties from " + PATH);
fis = new FileInputStream(PATH);
RESOURCE_BUNDLE = new PropertyResourceBundle(fis);
} catch (Exception e) {
logger.info("couldn't find " + PATH);
} finally {
try {
if(null != fis)
fis.close();
} catch (IOException e) {

}
}
}
}
}

TestMessages.java


package org.myorg.test;

import org.myorg.data.Messages;

public class TestMessages extends AbstractTest {
public void testMessage(){
logger.info(Messages.PATH);
logger.info(Messages.getString("OpenKey.TEST"));
}
}

最佳答案

仅供记录,您可以尝试这种方法:

  1. 使用 basename='messages' 声明 reloadableResourceBundle bean。假定扩展名为 .properties,但不是必需的
  2. 将其作为引用注入(inject)到您的 Bean 中
  3. 创建 messages.properties 文件并将您的内容放入其中
  4. 将 messages.properties 复制到 yourProject/web/messages.properties
  5. 放松并享受

说明:Spring 可重新加载消息包不使用类路径,因为它由应用程序服务器缓存。您可以通过 classpath:your.properies 来规避此问题,但您也可以使用不可重新加载的版本,该版本的速度要快一些。换句话说,技巧是使用不被缓存的 webApp 目录。您可以将文件放在 yourProject/web 下或下面的任何位置,只要您让加载程序知道即可。

将其放入 webapp-config.xml

<bean id="messageSource"  class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="message"/>
<property name="cacheSeconds" value="1"/>
</bean>

这是你的 bean:

public class YouBeanWithMsgReloadableResourceBundle {
public void yourMethod(){
String msg = ms.getMessage("your.memo.nic", null, "your default message", Locale.CANADA);
}

@Autowired MessageSource ms;}

由于 JSF2 确实很酷,您可能想在 faces 上下文中使用它:

public void validate(FacesContext context, UIComponent component, Object value)
throws ValidatorException {
MessageSource ms = FacesContextUtils.getWebApplicationContext(context).getBean(MessageSource.class);
String msg = ms.getMessage("your.memo.nic", null, "your default message", Locale.CANADA);
}

关于java - 找不到属性文件 ReloadableResourceBundleMessageSource 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2732280/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com