1 16 17 package org.springframework.web.servlet.view; 18 19 import java.util.Locale ; 20 21 import org.springframework.beans.BeansException; 22 import org.springframework.beans.factory.BeanFactory; 23 import org.springframework.beans.factory.DisposableBean; 24 import org.springframework.beans.factory.NoSuchBeanDefinitionException; 25 import org.springframework.beans.factory.xml.ResourceEntityResolver; 26 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; 27 import org.springframework.context.ConfigurableApplicationContext; 28 import org.springframework.core.Ordered; 29 import org.springframework.core.io.Resource; 30 import org.springframework.web.context.support.GenericWebApplicationContext; 31 import org.springframework.web.servlet.View; 32 33 53 public class XmlViewResolver extends AbstractCachingViewResolver implements Ordered, DisposableBean { 54 55 56 public final static String DEFAULT_LOCATION = "/WEB-INF/views.xml"; 57 58 59 private int order = Integer.MAX_VALUE; 61 private Resource location; 62 63 private ConfigurableApplicationContext cachedFactory; 64 65 66 public void setOrder(int order) { 67 this.order = order; 68 } 69 70 public int getOrder() { 71 return order; 72 } 73 74 79 public void setLocation(Resource location) { 80 this.location = location; 81 } 82 83 87 protected void initApplicationContext() throws BeansException { 88 if (isCache()) { 89 initFactory(); 90 } 91 } 92 93 94 98 protected Object getCacheKey(String viewName, Locale locale) { 99 return viewName; 100 } 101 102 protected View loadView(String viewName, Locale locale) throws BeansException { 103 BeanFactory factory = initFactory(); 104 try { 105 return (View) factory.getBean(viewName, View.class); 106 } 107 catch (NoSuchBeanDefinitionException ex) { 108 return null; 110 } 111 } 112 113 118 protected synchronized BeanFactory initFactory() throws BeansException { 119 if (this.cachedFactory != null) { 120 return this.cachedFactory; 121 } 122 123 Resource actualLocation = this.location; 124 if (actualLocation == null) { 125 actualLocation = getApplicationContext().getResource(DEFAULT_LOCATION); 126 } 127 128 GenericWebApplicationContext factory = new GenericWebApplicationContext(); 130 factory.setParent(getApplicationContext()); 131 factory.setServletContext(getServletContext()); 132 133 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory); 135 reader.setEntityResolver(new ResourceEntityResolver(getApplicationContext())); 136 reader.loadBeanDefinitions(actualLocation); 137 138 factory.refresh(); 139 140 if (isCache()) { 141 this.cachedFactory = factory; 142 } 143 return factory; 144 } 145 146 147 150 public void destroy() throws BeansException { 151 if (this.cachedFactory != null) { 152 this.cachedFactory.close(); 153 } 154 } 155 156 } 157 | Popular Tags |