From 164d27006a1d971b09474528721dcc79482c27e4 Mon Sep 17 00:00:00 2001 From: Andrus Adamchik Date: Sat, 9 Dec 2023 18:57:21 -0500 Subject: [PATCH] Switching to injectable factories per bootique/bootique#345 --- .../io/bootique/mybatis/MybatisModule.java | 15 +---- .../mybatis/SqlSessionManagerFactory.java | 59 +++++++++++-------- 2 files changed, 38 insertions(+), 36 deletions(-) diff --git a/bootique-mybatis/src/main/java/io/bootique/mybatis/MybatisModule.java b/bootique-mybatis/src/main/java/io/bootique/mybatis/MybatisModule.java index be58a55..855d191 100644 --- a/bootique-mybatis/src/main/java/io/bootique/mybatis/MybatisModule.java +++ b/bootique-mybatis/src/main/java/io/bootique/mybatis/MybatisModule.java @@ -23,16 +23,12 @@ import io.bootique.config.ConfigurationFactory; import io.bootique.di.Binder; import io.bootique.di.Provides; -import io.bootique.jdbc.DataSourceFactory; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionManager; import org.apache.ibatis.transaction.TransactionFactory; import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; -import org.apache.ibatis.type.TypeHandler; -import javax.inject.Provider; import javax.inject.Singleton; -import java.util.Set; public class MybatisModule implements BQModule { @@ -78,17 +74,10 @@ TransactionFactory provideTransactionFactory() { // SqlSessionManager is a newer more feature-rich version of SqlSessionFactory (which actually wraps a factory) @Provides @Singleton - public SqlSessionManager provideSessionManager( - ConfigurationFactory configFactory, - DataSourceFactory dsFactory, - Provider transactionFactory, - @ByMybatisModule Set> mappers, - @ByMybatisModule Set mapperPackages, - Set typeHandlers, - @TypeHandlerPackageByMybatisModule Set typeHandlerPackages) { + public SqlSessionManager provideSessionManager(ConfigurationFactory configFactory) { return configFactory .config(SqlSessionManagerFactory.class, CONFIG_PREFIX) - .createSessionManager(dsFactory, transactionFactory, mappers, mapperPackages, typeHandlers, typeHandlerPackages); + .createSessionManager(); } } diff --git a/bootique-mybatis/src/main/java/io/bootique/mybatis/SqlSessionManagerFactory.java b/bootique-mybatis/src/main/java/io/bootique/mybatis/SqlSessionManagerFactory.java index 30899fe..cbb3c04 100644 --- a/bootique-mybatis/src/main/java/io/bootique/mybatis/SqlSessionManagerFactory.java +++ b/bootique-mybatis/src/main/java/io/bootique/mybatis/SqlSessionManagerFactory.java @@ -34,6 +34,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.inject.Inject; import javax.inject.Provider; import javax.sql.DataSource; import java.io.IOException; @@ -48,21 +49,39 @@ public class SqlSessionManagerFactory { private final Logger logger = LoggerFactory.getLogger(SqlSessionManagerFactory.class); + private final DataSourceFactory dsFactory; + private final Provider transactionFactory; + private final @ByMybatisModule Set> mappers; + private final @ByMybatisModule Set mapperPackages; + private final Set typeHandlers; + private final @TypeHandlerPackageByMybatisModule Set typeHandlerPackages; + private String environmentId; private String datasource; private ResourceFactory config; - public SqlSessionManager createSessionManager( - DataSourceFactory dataSourceFactory, + @Inject + public SqlSessionManagerFactory( + DataSourceFactory dsFactory, Provider transactionFactory, - Set> mappers, - Set mapperPackages, + @ByMybatisModule Set> mappers, + @ByMybatisModule Set mapperPackages, Set typeHandlers, - Set typeHandlerPackages) { + @TypeHandlerPackageByMybatisModule Set typeHandlerPackages) { + + this.dsFactory = dsFactory; + this.transactionFactory = transactionFactory; + this.mappers = mappers; + this.mapperPackages = mapperPackages; + this.typeHandlers = typeHandlers; + this.typeHandlerPackages = typeHandlerPackages; + } + + public SqlSessionManager createSessionManager() { Configuration configuration = config != null - ? createConfigurationFromXML(dataSourceFactory, transactionFactory) - : createConfigurationFromScratch(dataSourceFactory, transactionFactory.get()); + ? createConfigurationFromXML() + : createConfigurationFromScratch(); // must install handlers before loading mappers... mappers need handlers mergeDITypeHandlers(configuration, typeHandlers, typeHandlerPackages); @@ -72,9 +91,7 @@ public SqlSessionManager createSessionManager( return SqlSessionManager.newInstance(sessionFactoryDelegate); } - protected Configuration createConfigurationFromXML( - DataSourceFactory dataSourceFactory, - Provider transactionFactory) { + protected Configuration createConfigurationFromXML() { String environmentId = getEnvironmentId(); Configuration configuration = loadConfigurationFromXML(config, environmentId); @@ -85,30 +102,26 @@ protected Configuration createConfigurationFromXML( logger.debug("MyBatis XML configuration does not specify environment for '{}'. Bootstrapping environment from Bootique...", environmentId); // deferring TransactionFactory creation until we know for sure that we need it... - Environment environment = createEnvironment(dataSourceFactory, transactionFactory.get()); + Environment environment = createEnvironment(); configuration.setEnvironment(environment); } return configuration; } - protected Configuration createConfigurationFromScratch( - DataSourceFactory dataSourceFactory, - TransactionFactory transactionFactory) { + protected Configuration createConfigurationFromScratch() { - Environment environment = createEnvironment(dataSourceFactory, transactionFactory); + Environment environment = createEnvironment(); return new Configuration(environment); } - protected Environment createEnvironment( - DataSourceFactory dataSourceFactory, - TransactionFactory transactionFactory) { + protected Environment createEnvironment() { - String datasourceName = dataSourceName(dataSourceFactory); + String datasourceName = dataSourceName(); logger.debug("Using Bootique DataSource named '{}' for MyBatis", datasourceName); - DataSource ds = dataSourceFactory.forName(datasourceName); - return new Environment(getEnvironmentId(), transactionFactory, ds); + DataSource ds = dsFactory.forName(datasourceName); + return new Environment(getEnvironmentId(), transactionFactory.get(), ds); } protected Configuration loadConfigurationFromXML(ResourceFactory configResource, String environmentId) { @@ -145,13 +158,13 @@ protected void mergeDIMappers( mapperPackages.forEach(mp -> configuration.addMappers(mp.getName())); } - protected String dataSourceName(DataSourceFactory dataSourceFactory) { + protected String dataSourceName() { if (datasource != null) { return datasource; } - Collection names = dataSourceFactory.allNames(); + Collection names = dsFactory.allNames(); if (names.size() == 1) { return names.iterator().next(); }