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
123
124
125
126
127
128
129
130
131
132 public void setBasePackage(String basePackage) {
133 this.basePackage = basePackage;
134 }
135
136
137
138
139
140
141
142
143 public void setAddToConfig(boolean addToConfig) {
144 this.addToConfig = addToConfig;
145 }
146
147
148
149
150
151
152
153
154
155
156
157 public void setLazyInitialization(String lazyInitialization) {
158 this.lazyInitialization = lazyInitialization;
159 }
160
161
162
163
164
165
166
167
168
169
170
171 public void setAnnotationClass(Class<? extends Annotation> annotationClass) {
172 this.annotationClass = annotationClass;
173 }
174
175
176
177
178
179
180
181
182
183
184
185
186 public void setMarkerInterface(Class<?> superClass) {
187 this.markerInterface = superClass;
188 }
189
190
191
192
193
194
195
196
197
198
199
200 @Deprecated
201 public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
202 this.sqlSessionTemplate = sqlSessionTemplate;
203 }
204
205
206
207
208
209
210
211
212
213
214
215
216
217 public void setSqlSessionTemplateBeanName(String sqlSessionTemplateName) {
218 this.sqlSessionTemplateBeanName = sqlSessionTemplateName;
219 }
220
221
222
223
224
225
226
227
228
229
230
231 @Deprecated
232 public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
233 this.sqlSessionFactory = sqlSessionFactory;
234 }
235
236
237
238
239
240
241
242
243
244
245
246
247
248 public void setSqlSessionFactoryBeanName(String sqlSessionFactoryName) {
249 this.sqlSessionFactoryBeanName = sqlSessionFactoryName;
250 }
251
252
253
254
255
256
257
258
259
260
261
262 public void setProcessPropertyPlaceHolders(boolean processPropertyPlaceHolders) {
263 this.processPropertyPlaceHolders = processPropertyPlaceHolders;
264 }
265
266
267
268
269
270
271
272
273 public void setMapperFactoryBeanClass(Class<? extends MapperFactoryBean> mapperFactoryBeanClass) {
274 this.mapperFactoryBeanClass = mapperFactoryBeanClass;
275 }
276
277
278
279
280 @Override
281 public void setApplicationContext(ApplicationContext applicationContext) {
282 this.applicationContext = applicationContext;
283 }
284
285
286
287
288 @Override
289 public void setBeanName(String name) {
290 this.beanName = name;
291 }
292
293
294
295
296
297
298
299 public BeanNameGenerator getNameGenerator() {
300 return nameGenerator;
301 }
302
303
304
305
306
307
308
309
310 public void setNameGenerator(BeanNameGenerator nameGenerator) {
311 this.nameGenerator = nameGenerator;
312 }
313
314
315
316
317 @Override
318 public void afterPropertiesSet() throws Exception {
319 notNull(this.basePackage, "Property 'basePackage' is required");
320 }
321
322
323
324
325 @Override
326 public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
327
328 }
329
330
331
332
333
334
335 @Override
336 public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
337 if (this.processPropertyPlaceHolders) {
338 processPropertyPlaceHolders();
339 }
340
341 ClassPathMapperScannerner.html#ClassPathMapperScanner">ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);
342 scanner.setAddToConfig(this.addToConfig);
343 scanner.setAnnotationClass(this.annotationClass);
344 scanner.setMarkerInterface(this.markerInterface);
345 scanner.setSqlSessionFactory(this.sqlSessionFactory);
346 scanner.setSqlSessionTemplate(this.sqlSessionTemplate);
347 scanner.setSqlSessionFactoryBeanName(this.sqlSessionFactoryBeanName);
348 scanner.setSqlSessionTemplateBeanName(this.sqlSessionTemplateBeanName);
349 scanner.setResourceLoader(this.applicationContext);
350 scanner.setBeanNameGenerator(this.nameGenerator);
351 scanner.setMapperFactoryBeanClass(this.mapperFactoryBeanClass);
352 if (StringUtils.hasText(lazyInitialization)) {
353 scanner.setLazyInitialization(Boolean.valueOf(lazyInitialization));
354 }
355 scanner.registerFilters();
356 scanner.scan(
357 StringUtils.tokenizeToStringArray(this.basePackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS));
358 }
359
360
361
362
363
364
365
366 private void processPropertyPlaceHolders() {
367 Map<String, PropertyResourceConfigurer> prcs = applicationContext.getBeansOfType(PropertyResourceConfigurer.class);
368
369 if (!prcs.isEmpty() && applicationContext instanceof ConfigurableApplicationContext) {
370 BeanDefinition mapperScannerBean = ((ConfigurableApplicationContext) applicationContext).getBeanFactory()
371 .getBeanDefinition(beanName);
372
373
374
375
376 DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
377 factory.registerBeanDefinition(beanName, mapperScannerBean);
378
379 for (PropertyResourceConfigurer prc : prcs.values()) {
380 prc.postProcessBeanFactory(factory);
381 }
382
383 PropertyValues values = mapperScannerBean.getPropertyValues();
384
385 this.basePackage = updatePropertyValue("basePackage", values);
386 this.sqlSessionFactoryBeanName = updatePropertyValue("sqlSessionFactoryBeanName", values);
387 this.sqlSessionTemplateBeanName = updatePropertyValue("sqlSessionTemplateBeanName", values);
388 this.lazyInitialization = updatePropertyValue("lazyInitialization", values);
389 }
390 this.basePackage = Optional.ofNullable(this.basePackage).map(getEnvironment()::resolvePlaceholders).orElse(null);
391 this.sqlSessionFactoryBeanName = Optional.ofNullable(this.sqlSessionFactoryBeanName)
392 .map(getEnvironment()::resolvePlaceholders).orElse(null);
393 this.sqlSessionTemplateBeanName = Optional.ofNullable(this.sqlSessionTemplateBeanName)
394 .map(getEnvironment()::resolvePlaceholders).orElse(null);
395 this.lazyInitialization = Optional.ofNullable(this.lazyInitialization).map(getEnvironment()::resolvePlaceholders)
396 .orElse(null);
397 }
398
399 private Environment getEnvironment() {
400 return this.applicationContext.getEnvironment();
401 }
402
403 private String updatePropertyValue(String propertyName, PropertyValues values) {
404 PropertyValue property = values.getPropertyValue(propertyName);
405
406 if (property == null) {
407 return null;
408 }
409
410 Object value = property.getValue();
411
412 if (value == null) {
413 return null;
414 } else if (value instanceof String) {
415 return value.toString();
416 } else if (value instanceof TypedStringValue) {
417 return ((TypedStringValue) value).getValue();
418 } else {
419 return null;
420 }
421 }
422
423 }