Skip to content

Commit 84954de

Browse files
authored
Add template change watcher for livereload (#398)
Include Spring Boot DevTools dependency for improved development experience. Implement a file system watcher to detect template changes and refresh the application context automatically in development mode.
1 parent c3c3e46 commit 84954de

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

jte-spring-boot-starter-3/pom.xml

+4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
<artifactId>spring-webflux</artifactId>
5151
<optional>true</optional>
5252
</dependency>
53+
<dependency>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-devtools</artifactId>
56+
</dependency>
5357
<dependency>
5458
<groupId>jakarta.servlet</groupId>
5559
<artifactId>jakarta.servlet-api</artifactId>

jte-spring-boot-starter-3/src/main/java/gg/jte/springframework/boot/autoconfigure/JteAutoConfiguration.java

+27
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@
44
import gg.jte.ContentType;
55
import gg.jte.TemplateEngine;
66
import gg.jte.resolve.DirectoryCodeResolver;
7+
import java.io.File;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
710
import org.springframework.boot.autoconfigure.AutoConfiguration;
811
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
912
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
13+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
1014
import org.springframework.boot.context.properties.EnableConfigurationProperties;
15+
import org.springframework.boot.devtools.filewatch.FileSystemWatcher;
16+
import org.springframework.context.ApplicationContext;
1117
import org.springframework.context.annotation.Bean;
18+
import org.springframework.context.event.ContextRefreshedEvent;
1219
import org.springframework.web.servlet.view.AbstractTemplateViewResolver;
1320

1421
import java.nio.file.FileSystems;
@@ -24,6 +31,9 @@ public JteAutoConfiguration(JteProperties jteProperties) {
2431
this.jteProperties = jteProperties;
2532
}
2633

34+
35+
Logger logger = LoggerFactory.getLogger(JteAutoConfiguration.class);
36+
2737
@Bean
2838
@ConditionalOnMissingBean(JteViewResolver.class)
2939
public JteViewResolver jteViewResolver(TemplateEngine templateEngine) {
@@ -49,4 +59,21 @@ public TemplateEngine jteTemplateEngine() {
4959
}
5060
throw new JteConfigurationException("You need to either set gg.jte.usePrecompiledTemplates or gg.jte.developmentMode to true ");
5161
}
62+
63+
@Bean
64+
@ConditionalOnProperty(name = "gg.jte.developmentMode", havingValue = "true")
65+
FileSystemWatcher viewComponentFileSystemWatcher(ApplicationContext applicationContext, JteProperties jteProperties) {
66+
var fileSystemWatcher = new FileSystemWatcher();
67+
File jteDir = new File(jteProperties.getTemplateLocation());
68+
fileSystemWatcher.addSourceDirectory(jteDir);
69+
logger.info("Watching for template changes at: {}", jteDir.getAbsoluteFile().getPath());
70+
fileSystemWatcher.addListener(
71+
changeSet -> {
72+
logger.debug("Detected change to template: {}", changeSet);
73+
applicationContext.publishEvent(new ContextRefreshedEvent(applicationContext));
74+
}
75+
);
76+
fileSystemWatcher.start();
77+
return fileSystemWatcher;
78+
}
5279
}

0 commit comments

Comments
 (0)