java spring mvc @service method: nullpointerexcept

2019-09-11 13:11发布

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);
    }

}

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)

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..

Thanks and cheers

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?

1条回答
姐就是有狂的资本
2楼-- · 2019-09-11 13:46

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.

e.g.

public ApplicationStrings() {
}

@PostConstruct
public void init() {
    //translationService.initLocale();
    this.updateLocale();
}
查看更多
登录 后发表回答