From a258d66dd4f6f0ce0cb52e4806e14addc35003c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Sun, 2 Apr 2023 15:14:28 +0800 Subject: [PATCH] optimize DbAutoConfiguration --- .../src/main/java/com/mybatisflex/core/row/Db.java | 3 ++- .../mybatisflex/spring/boot/DbAutoConfiguration.java | 11 ++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/Db.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/Db.java index 42466663..96655691 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/Db.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/row/Db.java @@ -39,7 +39,8 @@ public class Db { public static RowMapperInvoker invoker() { if (defaultRowMapperInvoker == null) { - SqlSessionFactory sqlSessionFactory = FlexGlobalConfig.getDefaultConfig().getSqlSessionFactory(); + FlexGlobalConfig defaultConfig = FlexGlobalConfig.getDefaultConfig(); + SqlSessionFactory sqlSessionFactory = defaultConfig.getSqlSessionFactory(); defaultRowMapperInvoker = new RowMapperInvoker(sqlSessionFactory); } return defaultRowMapperInvoker; diff --git a/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/DbAutoConfiguration.java b/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/DbAutoConfiguration.java index fa32c576..b8223e3a 100644 --- a/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/DbAutoConfiguration.java +++ b/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/DbAutoConfiguration.java @@ -15,18 +15,27 @@ */ package com.mybatisflex.spring.boot; +import com.mybatisflex.core.FlexGlobalConfig; import com.mybatisflex.core.row.Db; import com.mybatisflex.spring.SpringRowSessionManager; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.context.annotation.Configuration; +import java.util.logging.Level; +import java.util.logging.Logger; + @ConditionalOnClass(Db.class) @Configuration(proxyBeanMethods = false) @AutoConfigureAfter({MybatisFlexAutoConfiguration.class}) public class DbAutoConfiguration { public DbAutoConfiguration() { - Db.invoker().setRowSessionManager(new SpringRowSessionManager()); + FlexGlobalConfig defaultConfig = FlexGlobalConfig.getDefaultConfig(); + if (defaultConfig == null){ + Logger.getLogger(Db.class.getName()).log(Level.WARNING,"Cannot get FlexGlobalConfig instance, Perhaps the dataSource config error."); + }else { + Db.invoker().setRowSessionManager(new SpringRowSessionManager()); + } } }