diff --git a/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MybatisFlexAdminAutoConfiguration.java b/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MybatisFlexAdminAutoConfiguration.java new file mode 100644 index 00000000..88fc907b --- /dev/null +++ b/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MybatisFlexAdminAutoConfiguration.java @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.mybatisflex.spring.boot; + +import com.mybatisflex.core.audit.AuditManager; +import com.mybatisflex.core.audit.MessageFactory; +import com.mybatisflex.core.audit.MessageReporter; +import com.mybatisflex.core.audit.http.HttpMessageReporter; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +/** + * MyBatis-Flex-Admin 自动配置。 + * + * @author 王帅 + * @since 2023-07-01 + */ +@Configuration(proxyBeanMethods = false) +@AutoConfigureAfter(MybatisFlexAutoConfiguration.class) +@EnableConfigurationProperties(MybatisFlexProperties.class) +@ConditionalOnProperty(prefix = "mybatis-flex.admin-config", name = "enable", havingValue = "true") +public class MybatisFlexAdminAutoConfiguration implements InitializingBean { + + private final MessageFactory messageFactory; + private final MybatisFlexProperties properties; + private final HttpMessageReporter.JSONFormatter jsonFormatter; + + public MybatisFlexAdminAutoConfiguration(ObjectProvider messageFactory, + ObjectProvider jsonFormatter, + MybatisFlexProperties properties) { + this.properties = properties; + this.jsonFormatter = jsonFormatter.getIfAvailable(); + this.messageFactory = messageFactory.getIfAvailable(); + } + + @Override + public void afterPropertiesSet() { + AuditManager.setAuditEnable(true); + if (messageFactory != null) { + AuditManager.setMessageFactory(messageFactory); + } + MybatisFlexProperties.AdminConfig adminConfig = properties.getAdminConfig(); + MessageReporter messageReporter = new HttpMessageReporter( + adminConfig.getEndpoint(), + adminConfig.getSecretKey(), + jsonFormatter + ); + AuditManager.setMessageReporter(messageReporter); + } + +} \ No newline at end of file diff --git a/mybatis-flex-spring-boot-starter/src/main/resources/META-INF/spring.factories b/mybatis-flex-spring-boot-starter/src/main/resources/META-INF/spring.factories index d5ed7f94..7ed68ab9 100644 --- a/mybatis-flex-spring-boot-starter/src/main/resources/META-INF/spring.factories +++ b/mybatis-flex-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -5,4 +5,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.mybatisflex.spring.boot.FlexTransactionAutoConfiguration,\ com.mybatisflex.spring.boot.MultiDataSourceAutoConfiguration,\ com.mybatisflex.spring.boot.MybatisFlexAutoConfiguration,\ + com.mybatisflex.spring.boot.MybatisFlexAdminAutoConfiguration,\ com.mybatisflex.spring.boot.MybatisLanguageDriverAutoConfiguration \ No newline at end of file diff --git a/mybatis-flex-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/mybatis-flex-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index c167ba6c..664d341c 100644 --- a/mybatis-flex-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/mybatis-flex-spring-boot-starter/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1,4 +1,5 @@ com.mybatisflex.spring.boot.FlexTransactionAutoConfiguration com.mybatisflex.spring.boot.MultiDataSourceAutoConfiguration com.mybatisflex.spring.boot.MybatisFlexAutoConfiguration +com.mybatisflex.spring.boot.MybatisFlexAdminAutoConfiguration com.mybatisflex.spring.boot.MybatisLanguageDriverAutoConfiguration