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