1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.web.servlet.mvc.support;
18
19 import java.util.Arrays;
20 import java.util.Collections;
21 import java.util.HashSet;
22 import java.util.Set;
23
24 import org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping;
25
26
27
28
29
30
31
32
33
34
35 public abstract class AbstractControllerUrlHandlerMapping extends AbstractDetectingUrlHandlerMapping {
36
37 private ControllerTypePredicate predicate = new AnnotationControllerTypePredicate();
38
39 private Set<String> excludedPackages = Collections.singleton("org.springframework.web.servlet.mvc");
40
41 private Set<Class<?>> excludedClasses = Collections.emptySet();
42
43
44
45
46
47 public void setIncludeAnnotatedControllers(boolean includeAnnotatedControllers) {
48 this.predicate = (includeAnnotatedControllers ?
49 new AnnotationControllerTypePredicate() : new ControllerTypePredicate());
50 }
51
52
53
54
55
56
57
58
59
60
61
62
63 public void setExcludedPackages(String... excludedPackages) {
64 this.excludedPackages = (excludedPackages != null) ?
65 new HashSet<String>(Arrays.asList(excludedPackages)) : new HashSet<String>();
66 }
67
68
69
70
71
72 public void setExcludedClasses(Class<?>... excludedClasses) {
73 this.excludedClasses = (excludedClasses != null) ?
74 new HashSet<Class<?>>(Arrays.asList(excludedClasses)) : new HashSet<Class<?>>();
75 }
76
77
78
79
80
81
82 @Override
83 protected String[] determineUrlsForHandler(String beanName) {
84 Class<?> beanClass = getApplicationContext().getType(beanName);
85 if (isEligibleForMapping(beanName, beanClass)) {
86 return buildUrlsForHandler(beanName, beanClass);
87 }
88 else {
89 return null;
90 }
91 }
92
93
94
95
96
97
98
99
100
101 protected boolean isEligibleForMapping(String beanName, Class<?> beanClass) {
102 if (beanClass == null) {
103 if (logger.isDebugEnabled()) {
104 logger.debug("Excluding controller bean '" + beanName + "' from class name mapping " +
105 "because its bean type could not be determined");
106 }
107 return false;
108 }
109 if (this.excludedClasses.contains(beanClass)) {
110 if (logger.isDebugEnabled()) {
111 logger.debug("Excluding controller bean '" + beanName + "' from class name mapping " +
112 "because its bean class is explicitly excluded: " + beanClass.getName());
113 }
114 return false;
115 }
116 String beanClassName = beanClass.getName();
117 for (String packageName : this.excludedPackages) {
118 if (beanClassName.startsWith(packageName)) {
119 if (logger.isDebugEnabled()) {
120 logger.debug("Excluding controller bean '" + beanName + "' from class name mapping " +
121 "because its bean class is defined in an excluded package: " + beanClass.getName());
122 }
123 return false;
124 }
125 }
126 return isControllerType(beanClass);
127 }
128
129
130
131
132
133
134 protected boolean isControllerType(Class<?> beanClass) {
135 return this.predicate.isControllerType(beanClass);
136 }
137
138
139
140
141
142
143 protected boolean isMultiActionControllerType(Class<?> beanClass) {
144 return this.predicate.isMultiActionControllerType(beanClass);
145 }
146
147
148
149
150
151
152
153
154 protected abstract String[] buildUrlsForHandler(String beanName, Class<?> beanClass);
155
156 }