1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.InitializingBean;
25 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
26 import org.springframework.beans.factory.xml.ResourceEntityResolver;
27 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
28 import org.springframework.context.ConfigurableApplicationContext;
29 import org.springframework.core.Ordered;
30 import org.springframework.core.io.Resource;
31 import org.springframework.web.context.support.GenericWebApplicationContext;
32 import org.springframework.web.servlet.View;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 public class XmlViewResolver extends AbstractCachingViewResolver
57 implements Ordered, InitializingBean, DisposableBean {
58
59
60 public final static String DEFAULT_LOCATION = "/WEB-INF/views.xml";
61
62
63 private int order = Integer.MAX_VALUE;
64
65 private Resource location;
66
67 private ConfigurableApplicationContext cachedFactory;
68
69
70 public void setOrder(int order) {
71 this.order = order;
72 }
73
74 @Override
75 public int getOrder() {
76 return this.order;
77 }
78
79
80
81
82
83
84 public void setLocation(Resource location) {
85 this.location = location;
86 }
87
88
89
90
91
92 @Override
93 public void afterPropertiesSet() throws BeansException {
94 if (isCache()) {
95 initFactory();
96 }
97 }
98
99
100
101
102
103
104 @Override
105 protected Object getCacheKey(String viewName, Locale locale) {
106 return viewName;
107 }
108
109 @Override
110 protected View loadView(String viewName, Locale locale) throws BeansException {
111 BeanFactory factory = initFactory();
112 try {
113 return factory.getBean(viewName, View.class);
114 }
115 catch (NoSuchBeanDefinitionException ex) {
116
117 return null;
118 }
119 }
120
121
122
123
124
125
126 protected synchronized BeanFactory initFactory() throws BeansException {
127 if (this.cachedFactory != null) {
128 return this.cachedFactory;
129 }
130
131 Resource actualLocation = this.location;
132 if (actualLocation == null) {
133 actualLocation = getApplicationContext().getResource(DEFAULT_LOCATION);
134 }
135
136
137 GenericWebApplicationContext factory = new GenericWebApplicationContext();
138 factory.setParent(getApplicationContext());
139 factory.setServletContext(getServletContext());
140
141
142 XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
143 reader.setEnvironment(getApplicationContext().getEnvironment());
144 reader.setEntityResolver(new ResourceEntityResolver(getApplicationContext()));
145 reader.loadBeanDefinitions(actualLocation);
146
147 factory.refresh();
148
149 if (isCache()) {
150 this.cachedFactory = factory;
151 }
152 return factory;
153 }
154
155
156
157
158
159 @Override
160 public void destroy() throws BeansException {
161 if (this.cachedFactory != null) {
162 this.cachedFactory.close();
163 }
164 }
165
166 }