diff --git a/jte-spring-boot-starter-3/pom.xml b/jte-spring-boot-starter-3/pom.xml index 4a5f9896..bfd6982d 100644 --- a/jte-spring-boot-starter-3/pom.xml +++ b/jte-spring-boot-starter-3/pom.xml @@ -50,6 +50,10 @@ spring-webflux true + + org.springframework.boot + spring-boot-devtools + jakarta.servlet jakarta.servlet-api diff --git a/jte-spring-boot-starter-3/src/main/java/gg/jte/springframework/boot/autoconfigure/JteAutoConfiguration.java b/jte-spring-boot-starter-3/src/main/java/gg/jte/springframework/boot/autoconfigure/JteAutoConfiguration.java index 8164e7e9..449496da 100644 --- a/jte-spring-boot-starter-3/src/main/java/gg/jte/springframework/boot/autoconfigure/JteAutoConfiguration.java +++ b/jte-spring-boot-starter-3/src/main/java/gg/jte/springframework/boot/autoconfigure/JteAutoConfiguration.java @@ -4,11 +4,18 @@ import gg.jte.ContentType; import gg.jte.TemplateEngine; import gg.jte.resolve.DirectoryCodeResolver; +import java.io.File; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.devtools.filewatch.FileSystemWatcher; +import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; +import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.web.servlet.view.AbstractTemplateViewResolver; import java.nio.file.FileSystems; @@ -24,6 +31,9 @@ public JteAutoConfiguration(JteProperties jteProperties) { this.jteProperties = jteProperties; } + + Logger logger = LoggerFactory.getLogger(JteAutoConfiguration.class); + @Bean @ConditionalOnMissingBean(JteViewResolver.class) public JteViewResolver jteViewResolver(TemplateEngine templateEngine) { @@ -49,4 +59,21 @@ public TemplateEngine jteTemplateEngine() { } throw new JteConfigurationException("You need to either set gg.jte.usePrecompiledTemplates or gg.jte.developmentMode to true "); } + + @Bean + @ConditionalOnProperty(name = "gg.jte.developmentMode", havingValue = "true") + FileSystemWatcher viewComponentFileSystemWatcher(ApplicationContext applicationContext, JteProperties jteProperties) { + var fileSystemWatcher = new FileSystemWatcher(); + File jteDir = new File(jteProperties.getTemplateLocation()); + fileSystemWatcher.addSourceDirectory(jteDir); + logger.info("Watching for template changes at: {}", jteDir.getAbsoluteFile().getPath()); + fileSystemWatcher.addListener( + changeSet -> { + logger.debug("Detected change to template: {}", changeSet); + applicationContext.publishEvent(new ContextRefreshedEvent(applicationContext)); + } + ); + fileSystemWatcher.start(); + return fileSystemWatcher; + } }