spring init -a=gs-spring-boot -g=grace.guides -n="Grace Guide for Spring Boot" --description="Spring Boot Application with Grace Plugins" --package-name=grace.guides -l=groovy --build=gradle --format=project -t=gradle-project -d=devtools,actuator,web -x
In this guide, I will use Spring Boot 3.3.3
, Grace 2023.0.0
is built upon version 3.0.13
, but we can upgrade it.
plugins {
id 'groovy'
id 'org.springframework.boot' version '3.3.3'
id 'io.spring.dependency-management' version '1.1.6'
}
ext['spring-boot.version'] = '3.3.3'
ext['spring-framework.version'] = '6.1.12'
group = 'grace.guides'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = '17'
}
repositories {
mavenCentral()
}
configurations {
developmentOlny
}
dependencies {
developmentOlny 'org.springframework.boot:spring-boot-starter-devtools'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-tomcat'
implementation 'org.apache.groovy:groovy'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
./gradlew wrapper --gradle-version=8.10
First, import grace-bom
, then adding the dependencies,
dependencyManagement {
imports {
mavenBom 'org.graceframework:grace-bom:2023.0.0'
}
}
dependencies {
// Grace dependencies
implementation 'org.graceframework:grace-boot'
implementation 'org.graceframework:grace-core'
implementation 'org.graceframework:grace-plugin-api'
implementation 'org.graceframework:grace-plugin-core'
implementation 'org.graceframework:grace-plugin-dynamic-modules'
implementation 'org.graceframework:grace-plugin-management'
...
}
grace-plugin-management
include a built-in endpoint /plugins
.
management.endpoints.enabled-by-default=true
management.endpoints.web.exposure.include=*
LanguageGrailsPlugin
is a DynamicPlugin
, you can register dynamic modules by providing providedModules
def providedModules = [LanguageModuleDescriptor]
class LanguageGrailsPlugin extends DynamicPlugin {
def version = "1.0.0"
def providedModules = [LanguageModuleDescriptor]
Closure doWithSpring() { {->
// You can also use Spring AutoConfiguration
languageManager(DefaultLanguageManager)
}
}
Closure doWithDynamicModules() { {->
// Supported Languages
language(key: 'en_US', title: 'English', i18nNameKey: 'languages.en_US')
language(key: 'zh_CN', title: 'Chinese (Simplified Chinese)', i18nNameKey: 'languages.zh_CN')
language(key: 'zh_TW', title: 'Chinese (Traditional Chinese)', i18nNameKey: 'languages.zh_TW', enabled: true)
}}
}
@SpringBootApplication
class GraceBootApplication implements CommandLineRunner {
@Autowired
LanguageManager languageManager
static void main(String[] args) {
SpringApplication.run(GraceBootApplication, args)
}
@Override
void run(String... args) throws Exception {
List<LanguageModuleDescriptor> languageModuleDescriptors = this.languageManager.getLanguages()
languageModuleDescriptors.each { md ->
println "Language: key=$md.key, title=$md.title"
}
}
}
➜ gs-spring-boot git:(main) ✗ ./gradlew bootRun
> Task :bootRun
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.3.3)
2024-08-23T20:27:39.293+08:00 INFO 74059 --- [ main] grace.guides.GraceBootApplication : Starting GraceBootApplication using Java 17.0.12 with PID 74059 (/Users/rain/Development/github/grace/grace-guides/gs-spring-boot/build/classes/groovy/main started by rain in /Users/rain/Development/github/grace/grace-guides/gs-spring-boot)
2024-08-23T20:27:39.294+08:00 INFO 74059 --- [ main] grace.guides.GraceBootApplication : No active profile set, falling back to 1 default profile: "default"
2024-08-23T20:27:39.677+08:00 INFO 74059 --- [ main] g.plugins.DefaultGrailsPluginManager : Total 3 plugins loaded successfully, take in 45 ms
2024-08-23T20:27:39.885+08:00 INFO 74059 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http)
2024-08-23T20:27:39.890+08:00 INFO 74059 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2024-08-23T20:27:39.890+08:00 INFO 74059 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.28]
2024-08-23T20:27:39.911+08:00 INFO 74059 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2024-08-23T20:27:39.911+08:00 INFO 74059 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 598 ms
2024-08-23T20:27:40.190+08:00 INFO 74059 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 16 endpoints beneath base path '/actuator'
2024-08-23T20:27:40.219+08:00 INFO 74059 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/'
2024-08-23T20:27:40.228+08:00 INFO 74059 --- [ main] grace.guides.GraceBootApplication : Started GraceBootApplication in 1.06 seconds (process running for 1.332)
2024-08-23T20:27:40.229+08:00 DEBUG 74059 --- [ main] PluginsInfoApplicationContextInitializer :
----------------------------------------------------------------------------------------------
Order Plugin Name Plugin Version Enabled
----------------------------------------------------------------------------------------------
1 Core 2023.0.0 Y
2 DynamicModules 1.0.0-M1 Y
3 Language 1.0.0 Y
----------------------------------------------------------------------------------------------
Language: key=en_US, title=English
Language: key=zh_CN, title=Chinese (Simplified Chinese)
Language: key=zh_TW, title=Chinese (Traditional Chinese)
2024-08-23T20:27:43.314+08:00 INFO 74059 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2024-08-23T20:27:43.314+08:00 INFO 74059 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2024-08-23T20:27:43.315+08:00 INFO 74059 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
➜ gs-spring-boot git:(main) ✗ http :8080/actuator/plugins
HTTP/1.1 200
Connection: keep-alive
Content-Type: application/json
Date: Fri, 23 Aug 2024 12:28:07 GMT
Keep-Alive: timeout=60
Transfer-Encoding: chunked
{
"plugins": [
{
"dependencies": [],
"name": "core",
"type": "org.grails.plugins.core.CoreGrailsPlugin",
"version": "2023.0.0"
},
{
"dependencies": [],
"name": "dynamicModules",
"type": "org.grails.plugins.modules.DynamicModulesGrailsPlugin",
"version": "1.0.0-M1"
},
{
"dependencies": [],
"name": "language",
"type": "grace.guides.plugins.LanguageGrailsPlugin",
"version": "1.0.0"
}
]
}