001/*
002 *  Copyright (c) 2022-2025, Mybatis-Flex (fuhai999@gmail.com).
003 *  <p>
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *  <p>
008 *  http://www.apache.org/licenses/LICENSE-2.0
009 *  <p>
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016
017package com.mybatisflex.spring.boot;
018
019import com.mybatisflex.core.audit.AuditManager;
020import com.mybatisflex.core.audit.MessageFactory;
021import com.mybatisflex.core.audit.MessageReporter;
022import com.mybatisflex.core.audit.http.HttpMessageReporter;
023import org.springframework.beans.factory.InitializingBean;
024import org.springframework.beans.factory.ObjectProvider;
025import org.springframework.boot.autoconfigure.AutoConfigureAfter;
026import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
027import org.springframework.boot.context.properties.EnableConfigurationProperties;
028import org.springframework.context.annotation.Configuration;
029
030/**
031 * MyBatis-Flex-Admin 自动配置。
032 *
033 * @author 王帅
034 * @since 2023-07-01
035 */
036@Configuration(proxyBeanMethods = false)
037@AutoConfigureAfter(MybatisFlexAutoConfiguration.class)
038@EnableConfigurationProperties(MybatisFlexProperties.class)
039@ConditionalOnProperty(prefix = "mybatis-flex.admin-config", name = "enable", havingValue = "true")
040public class MybatisFlexAdminAutoConfiguration implements InitializingBean {
041
042    private final MessageFactory messageFactory;
043    private final MybatisFlexProperties properties;
044    private final HttpMessageReporter.JSONFormatter jsonFormatter;
045
046    public MybatisFlexAdminAutoConfiguration(ObjectProvider<MessageFactory> messageFactory,
047                                             ObjectProvider<HttpMessageReporter.JSONFormatter> jsonFormatter,
048                                             MybatisFlexProperties properties) {
049        this.properties = properties;
050        this.jsonFormatter = jsonFormatter.getIfAvailable();
051        this.messageFactory = messageFactory.getIfAvailable();
052    }
053
054    @Override
055    public void afterPropertiesSet() {
056        AuditManager.setAuditEnable(true);
057        if (messageFactory != null) {
058            AuditManager.setMessageFactory(messageFactory);
059        }
060        MybatisFlexProperties.AdminConfig adminConfig = properties.getAdminConfig();
061        MessageReporter messageReporter = new HttpMessageReporter(
062            adminConfig.getEndpoint(),
063            adminConfig.getSecretKey(),
064            jsonFormatter
065        );
066        AuditManager.setMessageReporter(messageReporter);
067    }
068
069}