public void initialize(ApplicationContext applicationContext) throws Exception { logger.info(">>> 初始化应用中不依赖于Servlet环境的系统常量!"); ApplicationContext rootApplicationContext = applicationContext; if(applicationContext instanceof WebApplicationContext && applicationContext.getParent() != null){//如果当前applicationContext是容器环境下SpringMVC Application上下文 rootApplicationContext = applicationContext.getParent(); } setFinalFieldValue(ApplicationConstants.class, "APPLICATION_CONTEXT", rootApplicationContext); SpringUtils.setApplicationContext(rootApplicationContext); try { Messages.setMessageSource(rootApplicationContext.getBean(AbstractMessageSource.class)); } catch (Exception e) { logger.error(e.getMessage()); } }
/** * Initialize test environment */ protected void setUp() throws Exception { Application.load(null); StaticApplicationContext applicationContext = new StaticApplicationContext(); DefaultApplicationServices applicationServices = new DefaultApplicationServices(applicationContext); DefaultApplicationLifecycleAdvisor advisor = new DefaultApplicationLifecycleAdvisor(); Application app = new Application(advisor); advisor.setApplication(app); Application.instance().setApplicationContext(applicationContext); applicationServices.setApplicationContext(applicationContext); ApplicationServicesLocator locator = new ApplicationServicesLocator(applicationServices); ApplicationServicesLocator.load(locator); // create dummy message source that always return an empty string AbstractMessageSource source = new AbstractMessageSource() { @Override protected MessageFormat resolveCode(String s, Locale locale) { return new MessageFormat(""); } }; MessageSourceAccessor accessor = new MessageSourceAccessor(source); applicationServices.setMessageSourceAccesor(accessor); applicationServices.setMessageSource(source); applicationContext.refresh(); // create formmodel to test on and initialize value models model = FormModelHelper.createFormModel(new ValidatingObject()); model.getValueModel("stringValue"); model.getValueModel("intValue"); }
public static void setMessageSource(AbstractMessageSource messageSource) { Messages.messageSource = messageSource; }