1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.spring.mapper;
17
18 import static org.springframework.util.Assert.notNull;
19
20 import java.lang.annotation.Annotation;
21 import java.util.Map;
22 import java.util.Optional;
23
24 import org.apache.ibatis.session.SqlSessionFactory;
25 import org.mybatis.spring.SqlSessionTemplate;
26 import org.springframework.beans.PropertyValue;
27 import org.springframework.beans.PropertyValues;
28 import org.springframework.beans.factory.BeanNameAware;
29 import org.springframework.beans.factory.InitializingBean;
30 import org.springframework.beans.factory.config.BeanDefinition;
31 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
32 import org.springframework.beans.factory.config.PropertyResourceConfigurer;
33 import org.springframework.beans.factory.config.TypedStringValue;
34 import org.springframework.beans.factory.support.BeanDefinitionRegistry;
35 import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
36 import org.springframework.beans.factory.support.BeanNameGenerator;
37 import org.springframework.beans.factory.support.DefaultListableBeanFactory;
38 import org.springframework.context.ApplicationContext;
39 import org.springframework.context.ApplicationContextAware;
40 import org.springframework.context.ConfigurableApplicationContext;
41 import org.springframework.core.env.Environment;
42 import org.springframework.util.StringUtils;
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 public class MapperScannerConfigurer
92 implements BeanDefinitionRegistryPostProcessor, InitializingBean, ApplicationContextAware, BeanNameAware {
93
94 private String basePackage;
95
96 private boolean addToConfig = true;
97
98 private String lazyInitialization;
99
100 private SqlSessionFactory sqlSessionFactory;
101
102 private SqlSessionTemplate sqlSessionTemplate;
103
104 private String sqlSessionFactoryBeanName;
105
106 private String sqlSessionTemplateBeanName;
107
108 private Class<? extends Annotation> annotationClass;
109
110 private Class<?> markerInterface;
111
112 private Class<? extends MapperFactoryBean> mapperFactoryBeanClass;
113
114 private ApplicationContext applicationContext;
115
116 private String beanName;
117
118 private boolean processPropertyPlaceHolders;
119
120 private BeanNameGenerator nameGenerator;
121
122 private String defaultScope;
123
124
125
126
127
128
129
130
131
132
133
134 public void setBasePackage(String basePackage) {
135 this.basePackage = basePackage;
136 }
137
138
139
140
141
142
143
144
145
146 public void setAddToConfig(boolean addToConfig) {
147 this.addToConfig = addToConfig;
148 }
149
150
151
152
153
154
155
156
157
158
159
160
161 public void setLazyInitialization(String lazyInitialization) {
162 this.lazyInitialization = lazyInitialization;
163 }
164
165
166
167
168
169
170
171
172
173
174
175 public void setAnnotationClass(Class<? extends Annotation> annotationClass) {
176 this.annotationClass = annotationClass;
177 }
178
179
180
181
182
183
184
185
186
187
188
189
190 public void setMarkerInterface(Class<?> superClass) {
191 this.markerInterface = superClass;
192 }
193
194
195
196
197
198
199
200
201
202
203
204 @Deprecated
205 public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
206 this.sqlSessionTemplate = sqlSessionTemplate;
207 }
208
209
210
211
212
213
214
215
216
217
218
219
220
221 public void setSqlSessionTemplateBeanName(String sqlSessionTemplateName) {
222 this.sqlSessionTemplateBeanName = sqlSessionTemplateName;
223 }
224
225
226
227
228
229
230
231
232
233
234
235 @Deprecated
236 public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
237 this.sqlSessionFactory = sqlSessionFactory;
238 }
239
240
241
242
243
244
245
246
247
248
249
250
251
252 public void setSqlSessionFactoryBeanName(String sqlSessionFactoryName) {
253 this.sqlSessionFactoryBeanName = sqlSessionFactoryName;
254 }
255
256
257
258
259
260
261
262
263
264
265
266 public void setProcessPropertyPlaceHolders(boolean processPropertyPlaceHolders) {
267 this.processPropertyPlaceHolders = processPropertyPlaceHolders;
268 }
269
270
271
272
273
274
275
276
277
278 public void setMapperFactoryBeanClass(Class<? extends MapperFactoryBean> mapperFactoryBeanClass) {
279 this.mapperFactoryBeanClass = mapperFactoryBeanClass;
280 }
281
282
283
284
285 @Override
286 public void setApplicationContext(ApplicationContext applicationContext) {
287 this.applicationContext = applicationContext;
288 }
289
290
291
292
293 @Override
294 public void setBeanName(String name) {
295 this.beanName = name;
296 }
297
298
299
300
301
302
303
304
305 public BeanNameGenerator getNameGenerator() {
306 return nameGenerator;
307 }
308
309
310
311
312
313
314
315
316
317 public void setNameGenerator(BeanNameGenerator nameGenerator) {
318 this.nameGenerator = nameGenerator;
319 }
320
321
322
323
324
325
326
327
328
329
330
331
332 public void setDefaultScope(String defaultScope) {
333 this.defaultScope = defaultScope;
334 }
335
336
337
338
339 @Override
340 public void afterPropertiesSet() throws Exception {
341 notNull(this.basePackage, "Property 'basePackage' is required");
342 }
343
344
345
346
347 @Override
348 public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
349
350 }
351
352
353
354
355
356
357 @Override
358 public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
359 if (this.processPropertyPlaceHolders) {
360 processPropertyPlaceHolders();
361 }
362
363 ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);
364 scanner.setAddToConfig(this.addToConfig);
365 scanner.setAnnotationClass(this.annotationClass);
366 scanner.setMarkerInterface(this.markerInterface);
367 scanner.setSqlSessionFactory(this.sqlSessionFactory);
368 scanner.setSqlSessionTemplate(this.sqlSessionTemplate);
369 scanner.setSqlSessionFactoryBeanName(this.sqlSessionFactoryBeanName);
370 scanner.setSqlSessionTemplateBeanName(this.sqlSessionTemplateBeanName);
371 scanner.setResourceLoader(this.applicationContext);
372 scanner.setBeanNameGenerator(this.nameGenerator);
373 scanner.setMapperFactoryBeanClass(this.mapperFactoryBeanClass);
374 if (StringUtils.hasText(lazyInitialization)) {
375 scanner.setLazyInitialization(Boolean.valueOf(lazyInitialization));
376 }
377 if (StringUtils.hasText(defaultScope)) {
378 scanner.setDefaultScope(defaultScope);
379 }
380 scanner.registerFilters();
381 scanner.scan(
382 StringUtils.tokenizeToStringArray(this.basePackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS));
383 }
384
385
386
387
388
389
390
391 private void processPropertyPlaceHolders() {
392 Map<String, PropertyResourceConfigurer> prcs = applicationContext.getBeansOfType(PropertyResourceConfigurer.class,
393 false, false);
394
395 if (!prcs.isEmpty() && applicationContext instanceof ConfigurableApplicationContext) {
396 BeanDefinition mapperScannerBean = ((ConfigurableApplicationContext) applicationContext).getBeanFactory()
397 .getBeanDefinition(beanName);
398
399
400
401
402 DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
403 factory.registerBeanDefinition(beanName, mapperScannerBean);
404
405 for (PropertyResourceConfigurer prc : prcs.values()) {
406 prc.postProcessBeanFactory(factory);
407 }
408
409 PropertyValues values = mapperScannerBean.getPropertyValues();
410
411 this.basePackage = getPropertyValue("basePackage", values);
412 this.sqlSessionFactoryBeanName = getPropertyValue("sqlSessionFactoryBeanName", values);
413 this.sqlSessionTemplateBeanName = getPropertyValue("sqlSessionTemplateBeanName", values);
414 this.lazyInitialization = getPropertyValue("lazyInitialization", values);
415 this.defaultScope = getPropertyValue("defaultScope", values);
416 }
417 this.basePackage = Optional.ofNullable(this.basePackage).map(getEnvironment()::resolvePlaceholders).orElse(null);
418 this.sqlSessionFactoryBeanName = Optional.ofNullable(this.sqlSessionFactoryBeanName)
419 .map(getEnvironment()::resolvePlaceholders).orElse(null);
420 this.sqlSessionTemplateBeanName = Optional.ofNullable(this.sqlSessionTemplateBeanName)
421 .map(getEnvironment()::resolvePlaceholders).orElse(null);
422 this.lazyInitialization = Optional.ofNullable(this.lazyInitialization).map(getEnvironment()::resolvePlaceholders)
423 .orElse(null);
424 this.defaultScope = Optional.ofNullable(this.defaultScope).map(getEnvironment()::resolvePlaceholders).orElse(null);
425 }
426
427 private Environment getEnvironment() {
428 return this.applicationContext.getEnvironment();
429 }
430
431 private String getPropertyValue(String propertyName, PropertyValues values) {
432 PropertyValue property = values.getPropertyValue(propertyName);
433
434 if (property == null) {
435 return null;
436 }
437
438 Object value = property.getValue();
439
440 if (value == null) {
441 return null;
442 } else if (value instanceof String) {
443 return value.toString();
444 } else if (value instanceof TypedStringValue) {
445 return ((TypedStringValue) value).getValue();
446 } else {
447 return null;
448 }
449 }
450
451 }