/** * Pre-initialize the factory from the XML file. * Only effective if caching is enabled. */ @Override public void afterPropertiesSet() throws BeansException { if (isCache()) { initFactory(); } }
/** * Initialize the view bean factory from the XML file. * Synchronized because of access by parallel threads. * @throws BeansException in case of initialization errors */ protected synchronized BeanFactory initFactory() throws BeansException { if (this.cachedFactory != null) { return this.cachedFactory; } ApplicationContext applicationContext = obtainApplicationContext(); Resource actualLocation = this.location; if (actualLocation == null) { actualLocation = applicationContext.getResource(DEFAULT_LOCATION); } // Create child ApplicationContext for views. GenericWebApplicationContext factory = new GenericWebApplicationContext(); factory.setParent(applicationContext); factory.setServletContext(getServletContext()); // Load XML resource with context-aware entity resolver. XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory); reader.setEnvironment(applicationContext.getEnvironment()); reader.setEntityResolver(new ResourceEntityResolver(applicationContext)); reader.loadBeanDefinitions(actualLocation); factory.refresh(); if (isCache()) { this.cachedFactory = factory; } return factory; }
/** * Initialize the view bean factory from the XML file. * Synchronized because of access by parallel threads. * @throws BeansException in case of initialization errors */ protected synchronized BeanFactory initFactory() throws BeansException { if (this.cachedFactory != null) { return this.cachedFactory; } Resource actualLocation = this.location; if (actualLocation == null) { actualLocation = getApplicationContext().getResource(DEFAULT_LOCATION); } // Create child ApplicationContext for views. GenericWebApplicationContext factory = new GenericWebApplicationContext(); factory.setParent(getApplicationContext()); factory.setServletContext(getServletContext()); // Load XML resource with context-aware entity resolver. XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory); reader.setEnvironment(getApplicationContext().getEnvironment()); reader.setEntityResolver(new ResourceEntityResolver(getApplicationContext())); reader.loadBeanDefinitions(actualLocation); factory.refresh(); if (isCache()) { this.cachedFactory = factory; } return factory; }
@Test public void testXmlViewResolverWithoutCache() throws Exception { StaticWebApplicationContext wac = new StaticWebApplicationContext() { @Override protected Resource getResourceByPath(String path) { assertTrue("Correct default location", XmlViewResolver.DEFAULT_LOCATION.equals(path)); return super.getResourceByPath(path); } }; wac.setServletContext(new MockServletContext()); wac.refresh(); XmlViewResolver vr = new XmlViewResolver(); vr.setCache(false); try { vr.setApplicationContext(wac); } catch (ApplicationContextException ex) { fail("Should not have thrown ApplicationContextException: " + ex.getMessage()); } try { vr.resolveViewName("example1", Locale.getDefault()); fail("Should have thrown BeanDefinitionStoreException"); } catch (BeanDefinitionStoreException ex) { // expected } }
@Override protected View loadView(String viewName, Locale locale) throws BeansException { BeanFactory factory = initFactory(); try { return factory.getBean(viewName, View.class); } catch (NoSuchBeanDefinitionException ex) { // Allow for ViewResolver chaining... return null; } }
wac.refresh(); TestBean testBean = (TestBean) wac.getBean("testBean"); XmlViewResolver vr = new XmlViewResolver(); vr.setLocation(new ClassPathResource("org/springframework/web/servlet/view/views.xml")); vr.setApplicationContext(wac); View view1 = vr.resolveViewName("example1", Locale.getDefault()); assertTrue("Correct view class", TestView.class.equals(view1.getClass())); assertTrue("Correct URL", "/example1.jsp".equals(((InternalResourceView) view1).getUrl())); View view2 = vr.resolveViewName("example2", Locale.getDefault()); assertTrue("Correct view class", JstlView.class.equals(view2.getClass())); assertTrue("Correct URL", "/example2new.jsp".equals(((InternalResourceView) view2).getUrl()));
@Test public void testXmlViewResolverDefaultLocation() { StaticWebApplicationContext wac = new StaticWebApplicationContext() { @Override protected Resource getResourceByPath(String path) { assertTrue("Correct default location", XmlViewResolver.DEFAULT_LOCATION.equals(path)); return super.getResourceByPath(path); } }; wac.setServletContext(new MockServletContext()); wac.refresh(); XmlViewResolver vr = new XmlViewResolver(); try { vr.setApplicationContext(wac); vr.afterPropertiesSet(); fail("Should have thrown BeanDefinitionStoreException"); } catch (BeanDefinitionStoreException ex) { // expected } }
/** * Initialize the view bean factory from the XML file. * Synchronized because of access by parallel threads. * @throws BeansException in case of initialization errors */ protected synchronized BeanFactory initFactory() throws BeansException { if (this.cachedFactory != null) { return this.cachedFactory; } ApplicationContext applicationContext = obtainApplicationContext(); Resource actualLocation = this.location; if (actualLocation == null) { actualLocation = applicationContext.getResource(DEFAULT_LOCATION); } // Create child ApplicationContext for views. GenericWebApplicationContext factory = new GenericWebApplicationContext(); factory.setParent(applicationContext); factory.setServletContext(getServletContext()); // Load XML resource with context-aware entity resolver. XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory); reader.setEnvironment(applicationContext.getEnvironment()); reader.setEntityResolver(new ResourceEntityResolver(applicationContext)); reader.loadBeanDefinitions(actualLocation); factory.refresh(); if (isCache()) { this.cachedFactory = factory; } return factory; }
@Override protected View loadView(String viewName, Locale locale) throws BeansException { BeanFactory factory = initFactory(); try { return factory.getBean(viewName, View.class); } catch (NoSuchBeanDefinitionException ex) { // Allow for ViewResolver chaining... return null; } }
/** * Pre-initialize the factory from the XML file. * Only effective if caching is enabled. */ @Override public void afterPropertiesSet() throws BeansException { if (isCache()) { initFactory(); } }
@Override protected View loadView(String viewName, Locale locale) throws BeansException { BeanFactory factory = initFactory(); try { return factory.getBean(viewName, View.class); } catch (NoSuchBeanDefinitionException ex) { // Allow for ViewResolver chaining... return null; } }
/** * Pre-initialize the factory from the XML file. * Only effective if caching is enabled. */ @Override public void afterPropertiesSet() throws BeansException { if (isCache()) { initFactory(); } }