15
15
*/
16
16
package org .mybatis .guice ;
17
17
18
- import com .google .inject .Scopes ;
19
- import com .google .inject .TypeLiteral ;
20
- import com .google .inject .multibindings .MapBinder ;
21
- import com .google .inject .multibindings .Multibinder ;
18
+ import static com .google .inject .multibindings .MapBinder .newMapBinder ;
19
+ import static com .google .inject .multibindings .Multibinder .newSetBinder ;
20
+ import static com .google .inject .name .Names .named ;
21
+ import static com .google .inject .util .Providers .guicify ;
22
+ import static org .mybatis .guice .Preconditions .checkArgument ;
23
+ import static org .mybatis .guice .Preconditions .checkState ;
24
+
25
+ import java .util .Collection ;
26
+ import java .util .Set ;
27
+
28
+ import javax .inject .Provider ;
29
+ import javax .sql .DataSource ;
22
30
23
- import com .google .inject .util .Providers ;
24
31
import org .apache .ibatis .io .ResolverUtil ;
25
32
import org .apache .ibatis .mapping .DatabaseIdProvider ;
26
33
import org .apache .ibatis .mapping .Environment ;
45
52
import org .mybatis .guice .configuration .Mappers ;
46
53
import org .mybatis .guice .configuration .MappingTypeHandlers ;
47
54
import org .mybatis .guice .configuration .TypeAliases ;
55
+ import org .mybatis .guice .configuration .settings .AggressiveLazyLoadingConfigurationSetting ;
56
+ import org .mybatis .guice .configuration .settings .AutoMappingBehaviorConfigurationSetting ;
57
+ import org .mybatis .guice .configuration .settings .CacheEnabledConfigurationSetting ;
58
+ import org .mybatis .guice .configuration .settings .ConfigurationSetting ;
59
+ import org .mybatis .guice .configuration .settings .ConfigurationSettings ;
60
+ import org .mybatis .guice .configuration .settings .DefaultExecutorTypeConfigurationSetting ;
61
+ import org .mybatis .guice .configuration .settings .DefaultScriptingLanguageTypeConfigurationSetting ;
62
+ import org .mybatis .guice .configuration .settings .DefaultStatementTimeoutConfigurationSetting ;
63
+ import org .mybatis .guice .configuration .settings .FailFastSettingImpl ;
64
+ import org .mybatis .guice .configuration .settings .LazyLoadingEnabledConfigurationSetting ;
65
+ import org .mybatis .guice .configuration .settings .LocalCacheScopeConfigurationSetting ;
66
+ import org .mybatis .guice .configuration .settings .MapUnderscoreToCamelCaseConfigurationSetting ;
67
+ import org .mybatis .guice .configuration .settings .MultipleResultSetsEnabledConfigurationSetting ;
68
+ import org .mybatis .guice .configuration .settings .ObjectFactoryConfigurationSetting ;
69
+ import org .mybatis .guice .configuration .settings .ObjectWrapperFactoryConfigurationSetting ;
70
+ import org .mybatis .guice .configuration .settings .UseColumnLabelConfigurationSetting ;
71
+ import org .mybatis .guice .configuration .settings .UseGeneratedKeysConfigurationSetting ;
48
72
import org .mybatis .guice .environment .EnvironmentProvider ;
49
73
import org .mybatis .guice .session .SqlSessionFactoryProvider ;
50
74
import org .mybatis .guice .type .TypeHandlerProvider ;
51
75
52
- import javax .inject .Provider ;
53
- import javax .sql .DataSource ;
54
-
55
- import java .util .Collection ;
56
- import java .util .Set ;
57
-
58
- import static com .google .inject .multibindings .MapBinder .newMapBinder ;
59
- import static com .google .inject .multibindings .Multibinder .newSetBinder ;
60
- import static com .google .inject .name .Names .named ;
61
- import static com .google .inject .util .Providers .guicify ;
62
- import static org .mybatis .guice .Preconditions .checkArgument ;
63
- import static org .mybatis .guice .Preconditions .checkState ;
76
+ import com .google .inject .Scopes ;
77
+ import com .google .inject .TypeLiteral ;
78
+ import com .google .inject .multibindings .MapBinder ;
79
+ import com .google .inject .multibindings .Multibinder ;
80
+ import com .google .inject .util .Providers ;
64
81
65
82
/**
66
83
* Easy to use helper Module that alleviates users to write the boilerplate
@@ -93,6 +110,8 @@ public abstract class MyBatisModule extends AbstractMyBatisModule {
93
110
private Multibinder <Interceptor > interceptors ;
94
111
95
112
private Multibinder <Class <?>> mappers ;
113
+
114
+ private Multibinder <ConfigurationSetting > configurationSettings ;
96
115
97
116
/**
98
117
* {@inheritDoc}
@@ -103,15 +122,18 @@ final void internalConfigure() {
103
122
checkState ( handlers == null , "Re-entry is not allowed." );
104
123
checkState ( interceptors == null , "Re-entry is not allowed." );
105
124
checkState ( mappers == null , "Re-entry is not allowed." );
125
+ checkState ( configurationSettings == null , "Re-entry is not allowed." );
106
126
107
127
aliases = newMapBinder (binder (), new TypeLiteral <String >(){}, new TypeLiteral <Class <?>>(){}, TypeAliases .class );
108
128
handlers = newMapBinder (binder (), new TypeLiteral <Class <?>>(){}, new TypeLiteral <TypeHandler <?>>(){});
109
129
interceptors = newSetBinder (binder (), Interceptor .class );
110
130
mappingTypeHandlers = newSetBinder (binder (), new TypeLiteral <TypeHandler <?>>(){}, MappingTypeHandlers .class );
111
131
mappers = newSetBinder (binder (), new TypeLiteral <Class <?>>(){}, Mappers .class );
132
+ configurationSettings = newSetBinder (binder (), ConfigurationSetting .class , ConfigurationSettings .class );
112
133
113
134
try {
114
135
initialize ();
136
+
115
137
} finally {
116
138
aliases = null ;
117
139
handlers = null ;
@@ -129,8 +151,11 @@ final void internalConfigure() {
129
151
// parametric bindings
130
152
bind (ObjectFactory .class ).to (objectFactoryType ).in (Scopes .SINGLETON );
131
153
bind (ObjectWrapperFactory .class ).to (objectWrapperFactoryType ).in (Scopes .SINGLETON );
132
- bind (new TypeLiteral <Class <? extends LanguageDriver >>() {}).toInstance (defaultScriptingLanguageType );
133
154
155
+
156
+ bindConfigurationSettingProvider (new ObjectFactoryConfigurationSetting (objectFactoryType ));
157
+ bindConfigurationSettingProvider (new ObjectWrapperFactoryConfigurationSetting (objectWrapperFactoryType ));
158
+ bindConfigurationSetting (new DefaultScriptingLanguageTypeConfigurationSetting (defaultScriptingLanguageType ));
134
159
}
135
160
136
161
/**
@@ -148,47 +173,47 @@ protected final void environmentId(String environmentId) {
148
173
* @param lazyLoadingEnabled
149
174
*/
150
175
protected final void lazyLoadingEnabled (boolean lazyLoadingEnabled ) {
151
- bindBoolean ( "mybatis.configuration.lazyLoadingEnabled" , lazyLoadingEnabled );
176
+ bindConfigurationSetting ( new LazyLoadingEnabledConfigurationSetting ( lazyLoadingEnabled ) );
152
177
}
153
178
154
179
/**
155
180
*
156
181
* @param aggressiveLazyLoading
157
182
*/
158
183
protected final void aggressiveLazyLoading (boolean aggressiveLazyLoading ) {
159
- bindBoolean ( "mybatis.configuration.aggressiveLazyLoading" , aggressiveLazyLoading );
184
+ bindConfigurationSetting ( new AggressiveLazyLoadingConfigurationSetting ( aggressiveLazyLoading ) );
160
185
}
161
186
162
187
/**
163
188
*
164
189
* @param multipleResultSetsEnabled
165
190
*/
166
191
protected final void multipleResultSetsEnabled (boolean multipleResultSetsEnabled ) {
167
- bindBoolean ( "mybatis.configuration.multipleResultSetsEnabled" , multipleResultSetsEnabled );
192
+ bindConfigurationSetting ( new MultipleResultSetsEnabledConfigurationSetting ( multipleResultSetsEnabled ) );
168
193
}
169
194
170
195
/**
171
196
*
172
197
* @param useGeneratedKeys
173
198
*/
174
199
protected final void useGeneratedKeys (boolean useGeneratedKeys ) {
175
- bindBoolean ( "mybatis.configuration.useGeneratedKeys" , useGeneratedKeys );
200
+ bindConfigurationSetting ( new UseGeneratedKeysConfigurationSetting ( useGeneratedKeys ) );
176
201
}
177
202
178
203
/**
179
204
*
180
205
* @param useColumnLabel
181
206
*/
182
207
protected final void useColumnLabel (boolean useColumnLabel ) {
183
- bindBoolean ( "mybatis.configuration.useColumnLabel" , useColumnLabel );
208
+ bindConfigurationSetting ( new UseColumnLabelConfigurationSetting ( useColumnLabel ) );
184
209
}
185
210
186
211
/**
187
212
*
188
213
* @param useCacheEnabled
189
214
*/
190
215
protected final void useCacheEnabled (boolean useCacheEnabled ) {
191
- bindBoolean ( "mybatis.configuration.cacheEnabled" , useCacheEnabled );
216
+ bindConfigurationSetting ( new CacheEnabledConfigurationSetting ( useCacheEnabled ) );
192
217
}
193
218
194
219
/**
@@ -210,7 +235,7 @@ protected final void useSqlSessionFactoryProvider(Class<? extends Provider<? ext
210
235
* @param failFast
211
236
*/
212
237
protected final void failFast (boolean failFast ) {
213
- bindBoolean ( "mybatis.configuration.failFast" , failFast );
238
+ bindConstant (). annotatedWith ( FailFastSettingImpl . get ()). to ( failFast );
214
239
}
215
240
216
241
/**
@@ -219,7 +244,7 @@ protected final void failFast(boolean failFast) {
219
244
* @param mapUnderscoreToCamelCase Toggles this settings value.
220
245
*/
221
246
protected final void mapUnderscoreToCamelCase (boolean mapUnderscoreToCamelCase ) {
222
- bindBoolean ( "mybatis.configuration. mapUnderscoreToCamelCase" , mapUnderscoreToCamelCase );
247
+ bindConfigurationSetting ( new MapUnderscoreToCamelCaseConfigurationSetting ( mapUnderscoreToCamelCase ) );
223
248
}
224
249
225
250
/**
@@ -228,20 +253,15 @@ protected final void mapUnderscoreToCamelCase(boolean mapUnderscoreToCamelCase)
228
253
* @param defaultStatementTimeout default statement timeout in seconds.
229
254
*/
230
255
protected final void defaultStatementTimeout (Integer defaultStatementTimeout ) {
231
- bindInteger ( "mybatis.configuration. defaultStatementTimeout" , defaultStatementTimeout );
256
+ bindConfigurationSetting ( new DefaultStatementTimeoutConfigurationSetting ( defaultStatementTimeout ) );
232
257
}
233
-
234
- /**
235
- *
236
- * @param name
237
- * @param value
238
- */
239
- private final void bindBoolean (String name , boolean value ) {
240
- bindConstant ().annotatedWith (named (name )).to (value );
258
+
259
+ public void bindConfigurationSetting (final ConfigurationSetting configurationSetting ){
260
+ configurationSettings .addBinding ().toProvider (Providers .of (configurationSetting ));
241
261
}
242
-
243
- private final void bindInteger ( String name , Integer value ) {
244
- bind ( Integer . class ). annotatedWith ( named ( name )) .toProvider (Providers . of ( value ));
262
+
263
+ public void bindConfigurationSettingProvider ( final Provider <? extends ConfigurationSetting > configurationSettingProvider ) {
264
+ configurationSettings . addBinding () .toProvider (guicify ( configurationSettingProvider ));
245
265
}
246
266
247
267
/**
@@ -251,7 +271,7 @@ private final void bindInteger(String name, Integer value) {
251
271
*/
252
272
protected final void executorType (ExecutorType executorType ) {
253
273
checkArgument (executorType != null , "Parameter 'executorType' must be not null" );
254
- bindConstant (). annotatedWith ( named ( "mybatis.configuration.defaultExecutorType" )). to ( executorType );
274
+ bindConfigurationSetting ( new DefaultExecutorTypeConfigurationSetting ( executorType ) );
255
275
}
256
276
257
277
/**
@@ -262,7 +282,7 @@ protected final void executorType(ExecutorType executorType) {
262
282
*/
263
283
protected final void localCacheScope (LocalCacheScope localeCacheScope ) {
264
284
checkArgument (localeCacheScope != null , "Parameter 'localCacheScope' must be not null" );
265
- bindConstant (). annotatedWith ( named ( "mybatis.configuration.localCacheScope" )). to ( localeCacheScope );
285
+ bindConfigurationSetting ( new LocalCacheScopeConfigurationSetting ( localeCacheScope ) );
266
286
}
267
287
268
288
/**
@@ -272,7 +292,7 @@ protected final void localCacheScope(LocalCacheScope localeCacheScope) {
272
292
*/
273
293
protected final void autoMappingBehavior (AutoMappingBehavior autoMappingBehavior ) {
274
294
checkArgument (autoMappingBehavior != null , "Parameter 'autoMappingBehavior' must be not null" );
275
- bindConstant (). annotatedWith ( named ( "mybatis.configuration. autoMappingBehavior" )). to ( autoMappingBehavior );
295
+ bindConfigurationSetting ( new AutoMappingBehaviorConfigurationSetting ( autoMappingBehavior ) );
276
296
}
277
297
278
298
/**
0 commit comments