001/**
002 * Copyright (c) 2022-2023, 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 */
016package com.mybatisflex.spring.boot;
017
018import org.apache.ibatis.scripting.LanguageDriver;
019import org.mybatis.scripting.freemarker.FreeMarkerLanguageDriver;
020import org.mybatis.scripting.freemarker.FreeMarkerLanguageDriverConfig;
021import org.mybatis.scripting.thymeleaf.ThymeleafLanguageDriver;
022import org.mybatis.scripting.thymeleaf.ThymeleafLanguageDriverConfig;
023import org.mybatis.scripting.velocity.VelocityLanguageDriver;
024import org.mybatis.scripting.velocity.VelocityLanguageDriverConfig;
025import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
026import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
027import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
028import org.springframework.boot.context.properties.ConfigurationProperties;
029import org.springframework.context.annotation.Bean;
030import org.springframework.context.annotation.Configuration;
031
032/**
033 * 脚本语言驱动的自动配置,平常一般项目用不到,只为了同步 mybatis 自带的 MybatisLanguageDriverAutoConfiguration
034 */
035@Configuration(proxyBeanMethods = false)
036@ConditionalOnClass(LanguageDriver.class)
037public class MybatisLanguageDriverAutoConfiguration {
038
039  private static final String CONFIGURATION_PROPERTY_PREFIX = "mybatis-flex.scripting-language-driver";
040
041  /**
042   * Configuration class for mybatis-freemarker 1.1.x or under.
043   */
044  @Configuration(proxyBeanMethods = false)
045  @ConditionalOnClass(FreeMarkerLanguageDriver.class)
046  @ConditionalOnMissingClass("org.mybatis.scripting.freemarker.FreeMarkerLanguageDriverConfig")
047  public static class LegacyFreeMarkerConfiguration {
048    @Bean
049    @ConditionalOnMissingBean
050    FreeMarkerLanguageDriver freeMarkerLanguageDriver() {
051      return new FreeMarkerLanguageDriver();
052    }
053  }
054
055  /**
056   * Configuration class for mybatis-freemarker 1.2.x or above.
057   */
058  @Configuration(proxyBeanMethods = false)
059  @ConditionalOnClass({ FreeMarkerLanguageDriver.class, FreeMarkerLanguageDriverConfig.class })
060  public static class FreeMarkerConfiguration {
061    @Bean
062    @ConditionalOnMissingBean
063    FreeMarkerLanguageDriver freeMarkerLanguageDriver(FreeMarkerLanguageDriverConfig config) {
064      return new FreeMarkerLanguageDriver(config);
065    }
066
067    @Bean
068    @ConditionalOnMissingBean
069    @ConfigurationProperties(CONFIGURATION_PROPERTY_PREFIX + ".freemarker")
070    public FreeMarkerLanguageDriverConfig freeMarkerLanguageDriverConfig() {
071      return FreeMarkerLanguageDriverConfig.newInstance();
072    }
073  }
074
075  /**
076   * Configuration class for mybatis-velocity 2.0 or under.
077   */
078  @Configuration(proxyBeanMethods = false)
079  @ConditionalOnClass(org.mybatis.scripting.velocity.Driver.class)
080  @ConditionalOnMissingClass("org.mybatis.scripting.velocity.VelocityLanguageDriverConfig")
081  @SuppressWarnings("deprecation")
082  public static class LegacyVelocityConfiguration {
083    @Bean
084    @ConditionalOnMissingBean
085    org.mybatis.scripting.velocity.Driver velocityLanguageDriver() {
086      return new org.mybatis.scripting.velocity.Driver();
087    }
088  }
089
090  /**
091   * Configuration class for mybatis-velocity 2.1.x or above.
092   */
093  @Configuration(proxyBeanMethods = false)
094  @ConditionalOnClass({ VelocityLanguageDriver.class, VelocityLanguageDriverConfig.class })
095  public static class VelocityConfiguration {
096    @Bean
097    @ConditionalOnMissingBean
098    VelocityLanguageDriver velocityLanguageDriver(VelocityLanguageDriverConfig config) {
099      return new VelocityLanguageDriver(config);
100    }
101
102    @Bean
103    @ConditionalOnMissingBean
104    @ConfigurationProperties(CONFIGURATION_PROPERTY_PREFIX + ".velocity")
105    public VelocityLanguageDriverConfig velocityLanguageDriverConfig() {
106      return VelocityLanguageDriverConfig.newInstance();
107    }
108  }
109
110  @Configuration(proxyBeanMethods = false)
111  @ConditionalOnClass(ThymeleafLanguageDriver.class)
112  public static class ThymeleafConfiguration {
113    @Bean
114    @ConditionalOnMissingBean
115    ThymeleafLanguageDriver thymeleafLanguageDriver(ThymeleafLanguageDriverConfig config) {
116      return new ThymeleafLanguageDriver(config);
117    }
118
119    @Bean
120    @ConditionalOnMissingBean
121    @ConfigurationProperties(CONFIGURATION_PROPERTY_PREFIX + ".thymeleaf")
122    public ThymeleafLanguageDriverConfig thymeleafLanguageDriverConfig() {
123      return ThymeleafLanguageDriverConfig.newInstance();
124    }
125
126    // This class provides to avoid the https://github.com/spring-projects/spring-boot/issues/21626 as workaround.
127    @SuppressWarnings("unused")
128    private static class MetadataThymeleafLanguageDriverConfig extends ThymeleafLanguageDriverConfig {
129
130      @ConfigurationProperties(CONFIGURATION_PROPERTY_PREFIX + ".thymeleaf.dialect")
131      @Override
132      public DialectConfig getDialect() {
133        return super.getDialect();
134      }
135
136      @ConfigurationProperties(CONFIGURATION_PROPERTY_PREFIX + ".thymeleaf.template-file")
137      @Override
138      public TemplateFileConfig getTemplateFile() {
139        return super.getTemplateFile();
140      }
141
142    }
143
144  }
145
146}