gpt4 book ai didi

java - 如何使用 Spring applicationContext 从 messageSource 检索消息?

转载 作者:行者123 更新时间:2023-11-30 07:27:19 30 4
gpt4 key购买 nike

这是我的 WEB-INF/applicationContext.xml :

<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="WEB-INF/resources/messages"/>
<property name="defaultEncoding" value="UTF-8" />
</bean>

<bean id="configurationService" class="com.services.ConfigurationService"/>
<bean id="companyService" class="com.services.CompanyService"/>
<bean id="messageService" class="com.services.MessageService"/>

它在 jsps (spring:message) 中完美运行。问题是我无法检索 MessageService 中的消息。我尝试了两种不同的方法:第一种方法(参见下面的代码)是使我的 MessageService通过实现org.springframework.context.ApplicationContextAware“了解”上下文。上下文是在初始化期间由 Spring 加载的,但是当我尝试在名称“messageResource”下查找 bean 时,applicationContext.getBean("messageSource")返回null .

package com.services;

import java.util.Locale;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;

public class MessageService implements ApplicationContextAware{

/** MessageService Instance */
private static MessageService instance = null;

/** Spring Message source*/
private ReloadableResourceBundleMessageSource messageSource;

/** Application context */
private ApplicationContext applicationContext;

/** Return MessageService instance */
public final static MessageService getInstance()
{
if (MessageService.instance==null)
{
synchronized(MessageService.class)
{
if (MessageService.instance==null)
MessageService.instance = new MessageService();

}
}
return instance;
}


/** Return a message */
public String getMessage(String messageId)
{
messageSource = (ReloadableResourceBundleMessageSource)applicationContext.getBean("messageSource");
Locale locale = LocaleContextHolder.getLocale();
return messageSource.getMessage(messageId,null, locale);
}

@Override
@Autowired
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;

}
}

第二种方法是在我的 MessageService 中加载上下文使用FileSystemXmlApplicationContextapplicationContext.getBean("messageSource")不返回null不再了,但是messageSource.getMessage(messageId,null, locale)找不到任何消息( NoSuchMessageException )。这些消息当然存在于我的 .properties

这两个问题看起来都像上下文问题,但我无法弄清楚在 .jsp 和 java 类中使用相同的 .properties 文件。另外,我希望在初始化期间在应用程序中加载消息一次,而不必每次在类中需要它们时都加载它们。感谢您的帮助!

最佳答案

我最终通过将 .properties 移动到类路径中的目录中来解决这个问题。对于那些有兴趣的人:

<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="resources/language/messages"/>
<property name="defaultEncoding" value="UTF-8" />
</bean>

<bean id="messageService" class="com.services.MessageService" factory-method="getInstance"/>

消息服务

package com.services;

import java.util.Locale;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.ResourceBundleMessageSource;

public class MessageService implements ApplicationContextAware{

/** MessageService Instance */
private static MessageService instance = null;

/** Spring Message source*/
private ResourceBundleMessageSource messageSource;

/** Application context */
private ApplicationContext applicationContext;

/** Return MessageService instance */
public final static MessageService getInstance()
{
if (MessageService.instance==null)
{
synchronized(MessageService.class)
{
if (MessageService.instance==null)
MessageService.instance = new MessageService();

}
}
return instance;
}


/** Return a message */
public String getMessage(String messageId)
{
messageSource = (ResourceBundleMessageSource) applicationContext.getBean("messageSource");
Locale locale = LocaleContextHolder.getLocale();
return messageSource.getMessage(messageId,null, locale);
}


@Override
@Autowired
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;

}
}

注意:此代码不适用于ReloadableResourceBundleMessageSource。它尝试查找资源的方式似乎与 ResourceBundleMessageSource 不同。

关于java - 如何使用 Spring applicationContext 从 messageSource 检索消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36650778/

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