我有管理语言的单例范围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 Method,则会调用它
@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);
}
但是在调试中我得到了这一行sessionRep.setLangIdMap(locales);
NullPointerException beacuse sessionRep为null。
我如何解决这个问题?