+ * Automatically registers a {@link ComponentDialect} bean with a + * {@link StandardThymeleafComponentParser} that scans for component fragments + * based on {@link ComponentDialectProperties}. + *
+ * + *+ * This auto-configuration is only active when Thymeleaf is on the classpath. To + * fully customize the dialect, define your own {@link ComponentDialect} bean + * and this auto-configuration will back off. + *
+ * + * @see ComponentDialectProperties + */ +@AutoConfiguration +@ConditionalOnClass(name = "org.thymeleaf.spring6.SpringTemplateEngine") +@EnableConfigurationProperties(ComponentDialectProperties.class) +public class ComponentDialectAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + public ComponentDialect componentDialect(ComponentDialectProperties properties) { + var dialect = new ComponentDialect(); + dialect.addParser(new StandardThymeleafComponentParser(properties.getTemplatePrefix(), + properties.getTemplateSuffix(), properties.getComponentDirectory())); + return dialect; + } +} diff --git a/src/main/java/de/morphbit/thymeleaf/autoconfigure/ComponentDialectProperties.java b/src/main/java/de/morphbit/thymeleaf/autoconfigure/ComponentDialectProperties.java new file mode 100644 index 0000000..39799b6 --- /dev/null +++ b/src/main/java/de/morphbit/thymeleaf/autoconfigure/ComponentDialectProperties.java @@ -0,0 +1,49 @@ +package de.morphbit.thymeleaf.autoconfigure; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Configuration properties for the Thymeleaf Component Dialect. + * + *+ * Example {@code application.yml}: + *
+ * + *{@code
+ * thymeleaf-component-dialect:
+ * template-prefix: templates/
+ * template-suffix: .html
+ * component-directory: components
+ * }
+ */
+@ConfigurationProperties(prefix = "thymeleaf-component-dialect")
+public class ComponentDialectProperties {
+
+ private String templatePrefix = "templates/";
+ private String templateSuffix = ".html";
+ private String componentDirectory = "components";
+
+ public String getTemplatePrefix() {
+ return templatePrefix;
+ }
+
+ public void setTemplatePrefix(String templatePrefix) {
+ this.templatePrefix = templatePrefix;
+ }
+
+ public String getTemplateSuffix() {
+ return templateSuffix;
+ }
+
+ public void setTemplateSuffix(String templateSuffix) {
+ this.templateSuffix = templateSuffix;
+ }
+
+ public String getComponentDirectory() {
+ return componentDirectory;
+ }
+
+ public void setComponentDirectory(String componentDirectory) {
+ this.componentDirectory = componentDirectory;
+ }
+}
diff --git a/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
new file mode 100644
index 0000000..9233e5f
--- /dev/null
+++ b/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
@@ -0,0 +1 @@
+de.morphbit.thymeleaf.autoconfigure.ComponentDialectAutoConfiguration