View Javadoc
1   /*
2    *    Copyright 2015-2022 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.spring.boot.autoconfigure;
17  
18  import org.apache.ibatis.scripting.LanguageDriver;
19  import org.mybatis.scripting.freemarker.FreeMarkerLanguageDriver;
20  import org.mybatis.scripting.freemarker.FreeMarkerLanguageDriverConfig;
21  import org.mybatis.scripting.thymeleaf.ThymeleafLanguageDriver;
22  import org.mybatis.scripting.thymeleaf.ThymeleafLanguageDriverConfig;
23  import org.mybatis.scripting.velocity.VelocityLanguageDriver;
24  import org.mybatis.scripting.velocity.VelocityLanguageDriverConfig;
25  import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
26  import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
27  import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
28  import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
29  import org.springframework.boot.context.properties.ConfigurationProperties;
30  import org.springframework.context.annotation.Bean;
31  import org.springframework.context.annotation.Configuration;
32  
33  /**
34   * {@link EnableAutoConfiguration Auto-Configuration} for MyBatis's scripting language drivers.
35   *
36   * @author Kazuki Shimizu
37   *
38   * @since 2.1.0
39   */
40  @Configuration(proxyBeanMethods = false)
41  @ConditionalOnClass(LanguageDriver.class)
42  public class MybatisLanguageDriverAutoConfiguration {
43  
44    private static final String CONFIGURATION_PROPERTY_PREFIX = "mybatis.scripting-language-driver";
45  
46    /**
47     * Configuration class for mybatis-freemarker 1.1.x or under.
48     */
49    @Configuration(proxyBeanMethods = false)
50    @ConditionalOnClass(FreeMarkerLanguageDriver.class)
51    @ConditionalOnMissingClass("org.mybatis.scripting.freemarker.FreeMarkerLanguageDriverConfig")
52    public static class LegacyFreeMarkerConfiguration {
53      @Bean
54      @ConditionalOnMissingBean
55      FreeMarkerLanguageDriver freeMarkerLanguageDriver() {
56        return new FreeMarkerLanguageDriver();
57      }
58    }
59  
60    /**
61     * Configuration class for mybatis-freemarker 1.2.x or above.
62     */
63    @Configuration(proxyBeanMethods = false)
64    @ConditionalOnClass({ FreeMarkerLanguageDriver.class, FreeMarkerLanguageDriverConfig.class })
65    public static class FreeMarkerConfiguration {
66      @Bean
67      @ConditionalOnMissingBean
68      FreeMarkerLanguageDriver freeMarkerLanguageDriver(FreeMarkerLanguageDriverConfig config) {
69        return new FreeMarkerLanguageDriver(config);
70      }
71  
72      @Bean
73      @ConditionalOnMissingBean
74      @ConfigurationProperties(CONFIGURATION_PROPERTY_PREFIX + ".freemarker")
75      public FreeMarkerLanguageDriverConfig freeMarkerLanguageDriverConfig() {
76        return FreeMarkerLanguageDriverConfig.newInstance();
77      }
78    }
79  
80    /**
81     * Configuration class for mybatis-velocity 2.0 or under.
82     */
83    @Configuration(proxyBeanMethods = false)
84    @ConditionalOnClass(org.mybatis.scripting.velocity.Driver.class)
85    @ConditionalOnMissingClass("org.mybatis.scripting.velocity.VelocityLanguageDriverConfig")
86    @SuppressWarnings("deprecation")
87    public static class LegacyVelocityConfiguration {
88      @Bean
89      @ConditionalOnMissingBean
90      org.mybatis.scripting.velocity.Driver velocityLanguageDriver() {
91        return new org.mybatis.scripting.velocity.Driver();
92      }
93    }
94  
95    /**
96     * Configuration class for mybatis-velocity 2.1.x or above.
97     */
98    @Configuration(proxyBeanMethods = false)
99    @ConditionalOnClass({ VelocityLanguageDriver.class, VelocityLanguageDriverConfig.class })
100   public static class VelocityConfiguration {
101     @Bean
102     @ConditionalOnMissingBean
103     VelocityLanguageDriver velocityLanguageDriver(VelocityLanguageDriverConfig config) {
104       return new VelocityLanguageDriver(config);
105     }
106 
107     @Bean
108     @ConditionalOnMissingBean
109     @ConfigurationProperties(CONFIGURATION_PROPERTY_PREFIX + ".velocity")
110     public VelocityLanguageDriverConfig velocityLanguageDriverConfig() {
111       return VelocityLanguageDriverConfig.newInstance();
112     }
113   }
114 
115   @Configuration(proxyBeanMethods = false)
116   @ConditionalOnClass(ThymeleafLanguageDriver.class)
117   public static class ThymeleafConfiguration {
118     @Bean
119     @ConditionalOnMissingBean
120     ThymeleafLanguageDriver thymeleafLanguageDriver(ThymeleafLanguageDriverConfig config) {
121       return new ThymeleafLanguageDriver(config);
122     }
123 
124     @Bean
125     @ConditionalOnMissingBean
126     @ConfigurationProperties(CONFIGURATION_PROPERTY_PREFIX + ".thymeleaf")
127     public ThymeleafLanguageDriverConfig thymeleafLanguageDriverConfig() {
128       return ThymeleafLanguageDriverConfig.newInstance();
129     }
130 
131     // This class provides to avoid the https://github.com/spring-projects/spring-boot/issues/21626 as workaround.
132     @SuppressWarnings("unused")
133     private static class MetadataThymeleafLanguageDriverConfig extends ThymeleafLanguageDriverConfig {
134 
135       @ConfigurationProperties(CONFIGURATION_PROPERTY_PREFIX + ".thymeleaf.dialect")
136       @Override
137       public DialectConfig getDialect() {
138         return super.getDialect();
139       }
140 
141       @ConfigurationProperties(CONFIGURATION_PROPERTY_PREFIX + ".thymeleaf.template-file")
142       @Override
143       public TemplateFileConfig getTemplateFile() {
144         return super.getTemplateFile();
145       }
146 
147     }
148 
149   }
150 
151 }