Skip to content

Commit

Permalink
Switching to injectable factories per bootique/bootique#345
Browse files Browse the repository at this point in the history
  • Loading branch information
andrus committed Dec 9, 2023
1 parent a227310 commit 164d270
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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> transactionFactory,
@ByMybatisModule Set<Class<?>> mappers,
@ByMybatisModule Set<Package> mapperPackages,
Set<TypeHandler> typeHandlers,
@TypeHandlerPackageByMybatisModule Set<Package> typeHandlerPackages) {
public SqlSessionManager provideSessionManager(ConfigurationFactory configFactory) {

return configFactory
.config(SqlSessionManagerFactory.class, CONFIG_PREFIX)
.createSessionManager(dsFactory, transactionFactory, mappers, mapperPackages, typeHandlers, typeHandlerPackages);
.createSessionManager();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -48,21 +49,39 @@ public class SqlSessionManagerFactory {

private final Logger logger = LoggerFactory.getLogger(SqlSessionManagerFactory.class);

private final DataSourceFactory dsFactory;
private final Provider<TransactionFactory> transactionFactory;
private final @ByMybatisModule Set<Class<?>> mappers;
private final @ByMybatisModule Set<Package> mapperPackages;
private final Set<TypeHandler> typeHandlers;
private final @TypeHandlerPackageByMybatisModule Set<Package> typeHandlerPackages;

private String environmentId;
private String datasource;
private ResourceFactory config;

public SqlSessionManager createSessionManager(
DataSourceFactory dataSourceFactory,
@Inject
public SqlSessionManagerFactory(
DataSourceFactory dsFactory,
Provider<TransactionFactory> transactionFactory,
Set<Class<?>> mappers,
Set<Package> mapperPackages,
@ByMybatisModule Set<Class<?>> mappers,
@ByMybatisModule Set<Package> mapperPackages,
Set<TypeHandler> typeHandlers,
Set<Package> typeHandlerPackages) {
@TypeHandlerPackageByMybatisModule Set<Package> 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);
Expand All @@ -72,9 +91,7 @@ public SqlSessionManager createSessionManager(
return SqlSessionManager.newInstance(sessionFactoryDelegate);
}

protected Configuration createConfigurationFromXML(
DataSourceFactory dataSourceFactory,
Provider<TransactionFactory> transactionFactory) {
protected Configuration createConfigurationFromXML() {

String environmentId = getEnvironmentId();
Configuration configuration = loadConfigurationFromXML(config, environmentId);
Expand All @@ -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) {
Expand Down Expand Up @@ -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<String> names = dataSourceFactory.allNames();
Collection<String> names = dsFactory.allNames();
if (names.size() == 1) {
return names.iterator().next();
}
Expand Down

0 comments on commit 164d270

Please sign in to comment.