1
1
/**
2
- * Copyright 2015-2017 the original author or authors.
2
+ * Copyright 2015-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
28
28
import org .springframework .beans .BeansException ;
29
29
import org .springframework .beans .factory .BeanFactory ;
30
30
import org .springframework .beans .factory .BeanFactoryAware ;
31
+ import org .springframework .beans .factory .InitializingBean ;
31
32
import org .springframework .beans .factory .ObjectProvider ;
32
33
import org .springframework .beans .factory .support .BeanDefinitionRegistry ;
33
34
import org .springframework .boot .autoconfigure .AutoConfigurationPackages ;
34
35
import org .springframework .boot .autoconfigure .AutoConfigureAfter ;
35
36
import org .springframework .boot .autoconfigure .AutoConfigureBefore ;
36
37
import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
37
- import org .springframework .boot .autoconfigure .condition .ConditionalOnBean ;
38
38
import org .springframework .boot .autoconfigure .condition .ConditionalOnClass ;
39
39
import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingBean ;
40
40
import org .springframework .boot .autoconfigure .condition .ConditionalOnProperty ;
41
+ import org .springframework .boot .autoconfigure .condition .ConditionalOnSingleCandidate ;
41
42
import org .springframework .boot .autoconfigure .jdbc .DataSourceAutoConfiguration ;
42
43
import org .springframework .boot .context .properties .EnableConfigurationProperties ;
43
44
import org .springframework .context .EnvironmentAware ;
58
59
import tk .mybatis .spring .mapper .MapperFactoryBean ;
59
60
import tk .mybatis .spring .mapper .SpringBootBindUtil ;
60
61
61
- import javax .annotation .PostConstruct ;
62
62
import javax .sql .DataSource ;
63
63
import java .util .Arrays ;
64
64
import java .util .List ;
78
78
* @author Eduardo Macarrón
79
79
*/
80
80
@ org .springframework .context .annotation .Configuration
81
- @ ConditionalOnClass ({SqlSessionFactory .class , SqlSessionFactoryBean .class })
82
- @ ConditionalOnBean (DataSource .class )
83
- @ EnableConfigurationProperties ({ MybatisProperties .class } )
81
+ @ ConditionalOnClass ({ SqlSessionFactory .class , SqlSessionFactoryBean .class })
82
+ @ ConditionalOnSingleCandidate (DataSource .class )
83
+ @ EnableConfigurationProperties (MybatisProperties .class )
84
84
@ AutoConfigureAfter (DataSourceAutoConfiguration .class )
85
85
@ AutoConfigureBefore (name = "org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration" )
86
- public class MapperAutoConfiguration {
86
+ public class MapperAutoConfiguration implements InitializingBean {
87
87
88
88
private static final Logger logger = LoggerFactory .getLogger (MapperAutoConfiguration .class );
89
89
@@ -98,19 +98,23 @@ public class MapperAutoConfiguration {
98
98
private final List <ConfigurationCustomizer > configurationCustomizers ;
99
99
100
100
public MapperAutoConfiguration (MybatisProperties properties ,
101
- ObjectProvider <Interceptor []> interceptorsProvider ,
102
- ResourceLoader resourceLoader ,
103
- ObjectProvider <DatabaseIdProvider > databaseIdProvider ,
104
- ObjectProvider <List <ConfigurationCustomizer >> configurationCustomizersProvider ) {
101
+ ObjectProvider <Interceptor []> interceptorsProvider ,
102
+ ResourceLoader resourceLoader ,
103
+ ObjectProvider <DatabaseIdProvider > databaseIdProvider ,
104
+ ObjectProvider <List <ConfigurationCustomizer >> configurationCustomizersProvider ) {
105
105
this .properties = properties ;
106
106
this .interceptors = interceptorsProvider .getIfAvailable ();
107
107
this .resourceLoader = resourceLoader ;
108
108
this .databaseIdProvider = databaseIdProvider .getIfAvailable ();
109
109
this .configurationCustomizers = configurationCustomizersProvider .getIfAvailable ();
110
110
}
111
111
112
- @ PostConstruct
113
- public void checkConfigFileExists () {
112
+ @ Override
113
+ public void afterPropertiesSet () {
114
+ checkConfigFileExists ();
115
+ }
116
+
117
+ private void checkConfigFileExists () {
114
118
if (this .properties .isCheckConfigLocation () && StringUtils .hasText (this .properties .getConfigLocation ())) {
115
119
Resource resource = this .resourceLoader .getResource (this .properties .getConfigLocation ());
116
120
Assert .state (resource .exists (), "Cannot find config location: " + resource
@@ -127,16 +131,7 @@ public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Excepti
127
131
if (StringUtils .hasText (this .properties .getConfigLocation ())) {
128
132
factory .setConfigLocation (this .resourceLoader .getResource (this .properties .getConfigLocation ()));
129
133
}
130
- Configuration configuration = this .properties .getConfiguration ();
131
- if (configuration == null && !StringUtils.hasText (this .properties .getConfigLocation ())) {
132
- configuration = new Configuration ();
133
- }
134
- if (configuration != null && !CollectionUtils .isEmpty (this .configurationCustomizers )) {
135
- for (ConfigurationCustomizer customizer : this .configurationCustomizers ) {
136
- customizer .customize (configuration );
137
- }
138
- }
139
- factory .setConfiguration (configuration );
134
+ applyConfiguration (factory );
140
135
if (this .properties .getConfigurationProperties () != null ) {
141
136
factory .setConfigurationProperties (this .properties .getConfigurationProperties ());
142
137
}
@@ -149,6 +144,9 @@ public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Excepti
149
144
if (StringUtils .hasLength (this .properties .getTypeAliasesPackage ())) {
150
145
factory .setTypeAliasesPackage (this .properties .getTypeAliasesPackage ());
151
146
}
147
+ if (this .properties .getTypeAliasesSuperType () != null ) {
148
+ factory .setTypeAliasesSuperType (this .properties .getTypeAliasesSuperType ());
149
+ }
152
150
if (StringUtils .hasLength (this .properties .getTypeHandlersPackage ())) {
153
151
factory .setTypeHandlersPackage (this .properties .getTypeHandlersPackage ());
154
152
}
@@ -159,6 +157,19 @@ public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Excepti
159
157
return factory .getObject ();
160
158
}
161
159
160
+ private void applyConfiguration (SqlSessionFactoryBean factory ) {
161
+ Configuration configuration = this .properties .getConfiguration ();
162
+ if (configuration == null && !StringUtils .hasText (this .properties .getConfigLocation ())) {
163
+ configuration = new Configuration ();
164
+ }
165
+ if (configuration != null && !CollectionUtils .isEmpty (this .configurationCustomizers )) {
166
+ for (ConfigurationCustomizer customizer : this .configurationCustomizers ) {
167
+ customizer .customize (configuration );
168
+ }
169
+ }
170
+ factory .setConfiguration (configuration );
171
+ }
172
+
162
173
@ Bean
163
174
@ ConditionalOnMissingBean
164
175
public SqlSessionTemplate sqlSessionTemplate (SqlSessionFactory sqlSessionFactory ) {
@@ -242,11 +253,11 @@ public void setResourceLoader(ResourceLoader resourceLoader) {
242
253
* on the same component-scanning path as Spring Boot itself.
243
254
*/
244
255
@ org .springframework .context .annotation .Configuration
245
- @ Import ({AutoConfiguredMapperScannerRegistrar .class })
256
+ @ Import ({ AutoConfiguredMapperScannerRegistrar .class })
246
257
@ ConditionalOnMissingBean (MapperFactoryBean .class )
247
- public static class MapperScannerRegistrarNotFoundConfiguration {
258
+ public static class MapperScannerRegistrarNotFoundConfiguration implements InitializingBean {
248
259
249
- @ PostConstruct
260
+ @ Override
250
261
public void afterPropertiesSet () {
251
262
logger .debug ("No {} found." , MapperFactoryBean .class .getName ());
252
263
}
0 commit comments