java spring mvc @service方法:nullpointerexception新手和翻译 [英] java spring mvc @service method: nullpointerexception | newbie and translation
问题描述
我正在尝试设置将使用gnu gettext的翻译服务.基本想法来自: https://thedarkgod. wordpress.com/2009/01/18/java-webapp-localization-through-gettext/
I am trying to set up a translation service which will use gnu gettext. The basic idea is from: https://thedarkgod.wordpress.com/2009/01/18/java-webapp-localization-through-gettext/
但是我希望将其作为服务来实现.由于某些奇怪的原因,我想上这个课:
But I would like it to be implemented as a service. For some odd reasons, I would like to have this class:
import webapp.service.TranslationService;
import org.springframework.context.i18n.LocaleContextHolder;
/**
* AppStrings<p>
* <p/>
* DOC-TODO:
*/
@Service("applicationStrings")
public class ApplicationStrings {
@Autowired private TranslationService translationService;
public String CART_SUBTYPE = "Cart";
public ApplicationStrings(){
Locale locale = LocaleContextHolder.getLocale();
//translationService.initLocale();
this.updateLocale();
}
public void updateLocale(){
Locale locale = LocaleContextHolder.getLocale();
translationService.updateLocale(locale);
this.setLocale(locale);
}
public void setLocale(Locale locale){
//this.CART_SUBTYPE = translationService._("Cart");
this.CART_SUBTYPE = "CART_DEF - Check ApplicationStrings";
}
}
部分代码被注释了,因为它实际上并不起作用...但是它可能揭示了我的目标.确实有问题的服务类如下所示:
Part of the code is commented as it doesn't really work... but it might reveal my target. The indeed problematic service class looks like this:
package webapp.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import gnu.gettext.GettextResource;
import java.util.ResourceBundle;
import java.util.Locale;
import java.text.MessageFormat;
import java.util.Hashtable;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
/**
* From: https://thedarkgod.wordpress.com/2009/01/18/java-webapp-localization-through-gettext/
*
*/
@Service("translationService")
public class TranslationService {
private final Logger logger = LoggerFactory.getLogger(getClass());
private static Hashtable<Locale, ResourceBundle> trht = new Hashtable<Locale, ResourceBundle> ();
private ResourceBundle myResources = null;
public TranslationService () {
Locale locale = LocaleContextHolder.getLocale();
if ( locale == null){
logger.warn("Setting a default locale!");
locale = Locale.ENGLISH;
}
this.updateLocale(locale);
}
public TranslationService (Locale locale) {
this.updateLocale(locale);
}
public void initLocale () {
Locale locale = LocaleContextHolder.getLocale();
this.updateLocale(locale);
}
public void updateLocale(Locale locale)
{
synchronized (trht)
{
if (!trht.contains (locale))
{
try
{
myResources = GettextResource.getBundle("translation", locale);
}
catch (Exception e)
{
logger.error("UPDATE: Exception.");
e.printStackTrace();
}
trht.put ((Locale) locale.clone(), myResources);
}
else
myResources = trht.get (locale);
}
}
public String _(String s)
{
if (myResources == null) return s;
return GettextResource.gettext (myResources, s);
}
public String N_(String singular, String plural, long n)
{
if (myResources == null) return (n == 1 ? singular : plural);
return GettextResource.ngettext (myResources, singular,
plural, n);
}
public String format (String s, Object ... args)
{
return MessageFormat.format (_(s), args);
}
public String formatN (String singular, String plural,
long n, Object ... args)
{
return MessageFormat.format (N_(singular, plural, n), args);
}
}
这些类在我的应用程序中甚至没有使用,但是spring会实例化它们,并且错误看起来像这样:
These classes are not even a bit used in my application, but of course spring will instantiate them and the error looks like this:
java.lang.NullPointerException
at webapp.constant.ApplicationStrings.updateLocale(ApplicationStrings.java:34)
at webapp.constant.ApplicationStrings.<init>(ApplicationStrings.java:29)
请注意,所有其他@service都在工作,因此我认为这不是某些xml配置的问题,为此我发现了关于stackoverflow的几个问题(和答案),其他服务也都在工作,因此配置应该可以.
Please note that all other @service are working so I assume it is not a problem of some xml configuration, for which I found few questions (and answers) on stackoverflow, again other services are working so the configuration should be fine.
我认为这是我的新手方法,可能会错过一些简单的关键字.甚至是概念.
I assume it is my newbie approach which might miss some simple keyword.. or even concept..
感谢和欢呼
与答案一起使用,因为这可能是获取文本的通用方法,并且确实与问题有关,所以我希望对实际方法提出一些意见.
Together with the answers, as this might be a general approach to gettext and indeed related to the question, I would appreciate a couple of comments on the actual approach.
我也完全不确定同步"部分:这可能是问题所在吗?
Also I am totally not sure about the "sychronized" part: can this be the problem?
推荐答案
您正在构造函数中调用依赖项. Spring在构造对象之后连接了bean依赖项,因此在这里您尝试过早调用这些方法,Spring当时没有机会连接您的bean.要解决此问题,您可以使用@PostConstruct
批注.在构造并连接一个bean之后,Spring总是会调用带有此标记的方法.
You are calling dependencies in your constructor. Spring wires the bean dependencies after constructing the object so here you are trying to call those methods a bit too early, Spring had no chance to wire your bean at that moment. To solve the issue you could make use of the @PostConstruct
annotation. A method marked with this is always called by Spring after constructing and wiring up a bean.
例如
public ApplicationStrings() {
}
@PostConstruct
public void init() {
//translationService.initLocale();
this.updateLocale();
}
这篇关于java spring mvc @service方法:nullpointerexception新手和翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!