1

我有用于管理语言的单例范围 bean

@Service(AppService.LANGUAGE)
@Scope(Lifetime.SINGLETON)
public class LanguageServiceImpl extends BaseService implements LanguageService {
    public static final Long RU_LOCALE_ID = 1L;
    public static final Long EN_LOCALE_ID = 2L;

    private List<LanguageBean> languageBeans;

    @ManagedProperty(value = ManagedInit.SESSION_REP)
    private SessionRepository sessionRep;

    public LanguageServiceImpl() {
    }

    @PostConstruct
    private void ensureLanguages() throws Exception {
        loadLanguages();
        Map<String, Long> locales = createLangIdMap();
        sessionRep.setLangIdMap(locales);
    }
}

和 Session 范围 bean 用于保存不同的信息。据我了解,如果类中存在 PostConstruct 方法,则会调用它

@Component(AppComponent.SESSION_REP)
@Scope(Lifetime.SESSION)
public class SessionRepository implements Serializable {
private Map<String, Long> langIdMap;

    @PostConstruct
    private void setDefaults() throws Exception{
        FacesContext context = FacesContext.getCurrentInstance();
        /*Locale defaultLocale = context.getApplication().getDefaultLocale();*/
        Locale defaultLocale = new Locale("ru");
        locale = defaultLocale.getLanguage();
        adminLocaleId = LanguageServiceImpl.RU_LOCALE_ID;
        backOutcomePage = new Stack<String>();

        appContext = context.getExternalContext().getRequestContextPath();
        completedWord = ResourceManager.getResource(PropertyEnum.CONTENT_MESSAGE).getProperty(COMPLETED_WORD, locale);
        saveWord = ResourceManager.getResource(PropertyEnum.CONTENT_MESSAGE).getProperty(SAVE_WORD, locale);    

    }

但是在调试中,我得到 n 这一行sessionRep.setLangIdMap(locales);NullPointerException 因为 sessionRep 为空。

我该如何解决这个问题?

4

0 回答 0