Skip to content

Commit 3fbcfb5

Browse files
authored
Merge pull request #619 from kazuki43zoo/gh-617.support-spring-native.add-SqlSessionFactoryBeanCustomizer
Add SqlSessionFactoryBeanCustomizer
2 parents 13863e2 + 06e74bf commit 3fbcfb5

File tree

4 files changed

+100
-3
lines changed

4 files changed

+100
-3
lines changed

mybatis-spring-boot-autoconfigure/src/main/java/org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,21 @@ public class MybatisAutoConfiguration implements InitializingBean {
105105

106106
private final List<ConfigurationCustomizer> configurationCustomizers;
107107

108+
private final List<SqlSessionFactoryBeanCustomizer> sqlSessionFactoryBeanCustomizers;
109+
108110
public MybatisAutoConfiguration(MybatisProperties properties, ObjectProvider<Interceptor[]> interceptorsProvider,
109111
ObjectProvider<TypeHandler[]> typeHandlersProvider, ObjectProvider<LanguageDriver[]> languageDriversProvider,
110112
ResourceLoader resourceLoader, ObjectProvider<DatabaseIdProvider> databaseIdProvider,
111-
ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizersProvider) {
113+
ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizersProvider,
114+
ObjectProvider<List<SqlSessionFactoryBeanCustomizer>> sqlSessionFactoryBeanCustomizers) {
112115
this.properties = properties;
113116
this.interceptors = interceptorsProvider.getIfAvailable();
114117
this.typeHandlers = typeHandlersProvider.getIfAvailable();
115118
this.languageDrivers = languageDriversProvider.getIfAvailable();
116119
this.resourceLoader = resourceLoader;
117120
this.databaseIdProvider = databaseIdProvider.getIfAvailable();
118121
this.configurationCustomizers = configurationCustomizersProvider.getIfAvailable();
122+
this.sqlSessionFactoryBeanCustomizers = sqlSessionFactoryBeanCustomizers.getIfAvailable();
119123
}
120124

121125
@Override
@@ -180,7 +184,7 @@ public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Excepti
180184
// Need to mybatis-spring 2.0.2+
181185
factory.setDefaultScriptingLanguageDriver(defaultLanguageDriver);
182186
}
183-
187+
applySqlSessionFactoryBeanCustomizers(factory);
184188
return factory.getObject();
185189
}
186190

@@ -197,6 +201,14 @@ private void applyConfiguration(SqlSessionFactoryBean factory) {
197201
factory.setConfiguration(configuration);
198202
}
199203

204+
private void applySqlSessionFactoryBeanCustomizers(SqlSessionFactoryBean factory) {
205+
if (!CollectionUtils.isEmpty(this.sqlSessionFactoryBeanCustomizers)) {
206+
for (SqlSessionFactoryBeanCustomizer customizer : this.sqlSessionFactoryBeanCustomizers) {
207+
customizer.customize(factory);
208+
}
209+
}
210+
}
211+
200212
@Bean
201213
@ConditionalOnMissingBean
202214
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2015-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.spring.boot.autoconfigure;
17+
18+
import org.mybatis.spring.SqlSessionFactoryBean;
19+
20+
/**
21+
* Callback interface that can be customized a {@link SqlSessionFactoryBean} object generated on auto-configuration.
22+
*
23+
* @author Kazuki Shimizu
24+
* @since 2.2.2
25+
*/
26+
@FunctionalInterface
27+
public interface SqlSessionFactoryBeanCustomizer {
28+
29+
/**
30+
* Customize the given a {@link SqlSessionFactoryBean} object.
31+
*
32+
* @param factoryBean
33+
* the factory bean object to customize
34+
*/
35+
void customize(SqlSessionFactoryBean factoryBean);
36+
37+
}

mybatis-spring-boot-autoconfigure/src/site/markdown/index.md

+24-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ mybatis:
183183
## Using a ConfigurationCustomizer
184184

185185
The MyBatis-Spring-Boot-Starter provide opportunity to customize a MyBatis configuration generated by auto-configuration using Java Config.
186-
The MyBatis-Spring-Boot-Starter will search beans that implements the `ConfigurationCustomizer` interface by automatically,
186+
The MyBatis-Spring-Boot-Starter will search beans that implement the `ConfigurationCustomizer` interface by automatically,
187187
and call a method that customize a MyBatis configuration. (Available since 1.2.1 or above)
188188

189189
For example:
@@ -203,6 +203,29 @@ public class MyBatisConfig {
203203
}
204204
```
205205

206+
## Using a SqlSessionFactoryBeanCustomizer
207+
208+
The MyBatis-Spring-Boot-Starter provide opportunity to customize a `SqlSessionFactoryBean` generated by auto-configuration using Java Config.
209+
The MyBatis-Spring-Boot-Starter will search beans that implement the `SqlSessionFactoryBeanCustomizer` interface by automatically,
210+
and call a method that customize a `SqlSessionFactoryBean`. (Available since 2.2.2 or above)
211+
212+
For example:
213+
214+
```java
215+
@Configuration
216+
public class MyBatisConfig {
217+
@Bean
218+
SqlSessionFactoryBeanCustomizer sqlSessionFactoryBeanCustomizer() {
219+
return new SqlSessionFactoryBeanCustomizer() {
220+
@Override
221+
public void customize(SqlSessionFactoryBean factoryBean) {
222+
// customize ...
223+
}
224+
};
225+
}
226+
}
227+
```
228+
206229
## Using the SpringBootVFS
207230

208231
The MyBatis-Spring-Boot-Starter provides the `SpringBootVFS` as an implementation class of `VFS`.

mybatis-spring-boot-autoconfigure/src/test/java/org/mybatis/spring/boot/autoconfigure/MybatisAutoConfigurationTest.java

+25
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,17 @@ void testWithMyBatisConfigurationCustomizer() {
563563
assertThat(sqlSessionFactory.getConfiguration().getCache("test")).isNotNull();
564564
}
565565

566+
@Test
567+
void testWithSqlSessionFactoryBeanCustomizer() {
568+
this.context.register(EmbeddedDataSourceConfiguration.class, MybatisAutoConfiguration.class,
569+
SqlSessionFactoryBeanCustomizerConfiguration.class);
570+
this.context.refresh();
571+
SqlSessionFactory sqlSessionFactory = this.context.getBean(SqlSessionFactory.class);
572+
assertThat(sqlSessionFactory.getConfiguration().getTypeHandlerRegistry().getTypeHandler(BigInteger.class))
573+
.isInstanceOf(DummyTypeHandler.class);
574+
assertThat(sqlSessionFactory.getConfiguration().getCache("test")).isNotNull();
575+
}
576+
566577
@Test
567578
void testConfigFileAndConfigurationWithTogether() {
568579
TestPropertyValues
@@ -952,6 +963,20 @@ ConfigurationCustomizer cacheConfigurationCustomizer() {
952963
}
953964
}
954965

966+
@Configuration
967+
@EnableAutoConfiguration
968+
static class SqlSessionFactoryBeanCustomizerConfiguration {
969+
@Bean
970+
SqlSessionFactoryBeanCustomizer typeHandlerSqlSessionFactoryBeanCustomizer() {
971+
return factoryBean -> factoryBean.setTypeHandlers(new DummyTypeHandler());
972+
}
973+
974+
@Bean
975+
SqlSessionFactoryBeanCustomizer cacheSqlSessionFactoryBeanCustomizer() {
976+
return factoryBean -> factoryBean.setCache(new PerpetualCache("test"));
977+
}
978+
}
979+
955980
@Configuration
956981
static class SingleLanguageDriverConfiguration {
957982
@Bean

0 commit comments

Comments
 (0)