18
18
import java .lang .annotation .Annotation ;
19
19
import java .util .ArrayList ;
20
20
import java .util .Arrays ;
21
+ import java .util .HashMap ;
21
22
import java .util .List ;
23
+ import java .util .Map ;
22
24
import java .util .stream .Collectors ;
23
25
24
26
import org .mybatis .spring .mapper .ClassPathMapperScanner ;
31
33
import org .springframework .beans .factory .support .BeanDefinitionRegistry ;
32
34
import org .springframework .beans .factory .support .BeanNameGenerator ;
33
35
import org .springframework .context .ResourceLoaderAware ;
36
+ import org .springframework .context .annotation .FilterType ;
34
37
import org .springframework .context .annotation .ImportBeanDefinitionRegistrar ;
35
38
import org .springframework .core .annotation .AnnotationAttributes ;
36
39
import org .springframework .core .io .ResourceLoader ;
37
40
import org .springframework .core .type .AnnotationMetadata ;
41
+ import org .springframework .core .type .filter .AnnotationTypeFilter ;
42
+ import org .springframework .core .type .filter .AssignableTypeFilter ;
43
+ import org .springframework .core .type .filter .TypeFilter ;
44
+ import org .springframework .util .Assert ;
38
45
import org .springframework .util .ClassUtils ;
39
46
import org .springframework .util .StringUtils ;
40
47
54
61
*/
55
62
public class MapperScannerRegistrar implements ImportBeanDefinitionRegistrar , ResourceLoaderAware {
56
63
64
+ private ResourceLoader resourceLoader ;
65
+
57
66
/**
58
67
* {@inheritDoc}
59
- *
60
- * @deprecated Since 2.0.2, this method not used never.
61
68
*/
62
69
@ Override
63
- @ Deprecated
64
70
public void setResourceLoader (ResourceLoader resourceLoader ) {
65
- // NOP
71
+ this . resourceLoader = resourceLoader ;
66
72
}
67
73
68
74
/**
@@ -126,6 +132,22 @@ void registerBeanDefinitions(AnnotationMetadata annoMeta, AnnotationAttributes a
126
132
basePackages .add (getDefaultBasePackage (annoMeta ));
127
133
}
128
134
135
+ AnnotationAttributes [] excludeFilterArray = annoAttrs .getAnnotationArray ("excludeFilters" );
136
+ if (excludeFilterArray .length > 0 ) {
137
+ List <TypeFilter > typeFilters = new ArrayList <>();
138
+ List <Map <String , String >> rawTypeFilters = new ArrayList <>();
139
+ for (AnnotationAttributes excludeFilters : excludeFilterArray ) {
140
+ if (excludeFilters .getStringArray ("pattern" ).length > 0 ) {
141
+ // in oder to apply placeholder resolver
142
+ rawTypeFilters .addAll (parseFiltersHasPatterns (excludeFilters ));
143
+ } else {
144
+ typeFilters .addAll (typeFiltersFor (excludeFilters ));
145
+ }
146
+ }
147
+ builder .addPropertyValue ("excludeFilters" , typeFilters );
148
+ builder .addPropertyValue ("rawExcludeFilters" , rawTypeFilters );
149
+ }
150
+
129
151
String lazyInitialization = annoAttrs .getString ("lazyInitialization" );
130
152
if (StringUtils .hasText (lazyInitialization )) {
131
153
builder .addPropertyValue ("lazyInitialization" , lazyInitialization );
@@ -145,6 +167,74 @@ void registerBeanDefinitions(AnnotationMetadata annoMeta, AnnotationAttributes a
145
167
146
168
}
147
169
170
+ /**
171
+ * Parse excludeFilters which FilterType is REGEX or ASPECTJ
172
+ *
173
+ * @param filterAttributes
174
+ * AnnotationAttributes of excludeFilters
175
+ *
176
+ * @since 3.0.3
177
+ */
178
+ private List <Map <String , String >> parseFiltersHasPatterns (AnnotationAttributes filterAttributes ) {
179
+
180
+ List <Map <String , String >> rawTypeFilters = new ArrayList <>();
181
+ FilterType filterType = filterAttributes .getEnum ("type" );
182
+ String [] expressionArray = filterAttributes .getStringArray ("pattern" );
183
+ for (String expression : expressionArray ) {
184
+ switch (filterType ) {
185
+ case REGEX :
186
+ case ASPECTJ :
187
+ Map <String , String > typeFilter = new HashMap <>(16 );
188
+ typeFilter .put ("type" , filterType .name ().toLowerCase ());
189
+ typeFilter .put ("expression" , expression );
190
+ rawTypeFilters .add (typeFilter );
191
+ break ;
192
+ default :
193
+ throw new IllegalArgumentException ("Cannot specify the 'pattern' attribute if use the " + filterType
194
+ + " FilterType in exclude filter of @MapperScan" );
195
+ }
196
+ }
197
+ return rawTypeFilters ;
198
+ }
199
+
200
+ /**
201
+ * Parse excludeFilters which FilterType is ANNOTATION ASSIGNABLE or CUSTOM
202
+ *
203
+ * @param filterAttributes
204
+ * AnnotationAttributes of excludeFilters
205
+ *
206
+ * @since 3.0.3
207
+ */
208
+ private List <TypeFilter > typeFiltersFor (AnnotationAttributes filterAttributes ) {
209
+
210
+ List <TypeFilter > typeFilters = new ArrayList <>();
211
+ FilterType filterType = filterAttributes .getEnum ("type" );
212
+
213
+ for (Class <?> filterClass : filterAttributes .getClassArray ("value" )) {
214
+ switch (filterType ) {
215
+ case ANNOTATION :
216
+ Assert .isAssignable (Annotation .class , filterClass ,
217
+ "Specified an unsupported type in 'ANNOTATION' exclude filter of @MapperScan" );
218
+ @ SuppressWarnings ("unchecked" )
219
+ Class <Annotation > annoClass = (Class <Annotation >) filterClass ;
220
+ typeFilters .add (new AnnotationTypeFilter (annoClass ));
221
+ break ;
222
+ case ASSIGNABLE_TYPE :
223
+ typeFilters .add (new AssignableTypeFilter (filterClass ));
224
+ break ;
225
+ case CUSTOM :
226
+ Assert .isAssignable (TypeFilter .class , filterClass ,
227
+ "An error occured when processing a @ComponentScan " + "CUSTOM type filter: " );
228
+ typeFilters .add (BeanUtils .instantiateClass (filterClass , TypeFilter .class ));
229
+ break ;
230
+ default :
231
+ throw new IllegalArgumentException ("Cannot specify the 'value' or 'classes' attribute if use the "
232
+ + filterType + " FilterType in exclude filter of @MapperScan" );
233
+ }
234
+ }
235
+ return typeFilters ;
236
+ }
237
+
148
238
private static String generateBaseBeanName (AnnotationMetadata importingClassMetadata , int index ) {
149
239
return importingClassMetadata .getClassName () + "#" + MapperScannerRegistrar .class .getSimpleName () + "#" + index ;
150
240
}
0 commit comments