From d9a8bc1b80865eacaf9fe208c5e7948b5b6fcba8 Mon Sep 17 00:00:00 2001 From: Marcelo Shima Date: Tue, 22 Oct 2024 16:02:58 -0300 Subject: [PATCH] spring-boot: allow h2 console to be enabled/disabled using properties (#27649) --- .../main/java/_package_/Application.java.ejs | 5 ++++- .../_package_/config/WebConfigurer.java.ejs | 21 ++++++++++++------- .../resources/config/application-dev.yml.ejs | 4 ++-- .../config/DatabaseConfiguration.java.ejs | 11 +++++++--- 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/generators/spring-boot/templates/src/main/java/_package_/Application.java.ejs b/generators/spring-boot/templates/src/main/java/_package_/Application.java.ejs index 36dcaf5f7823..1935af41e6fd 100644 --- a/generators/spring-boot/templates/src/main/java/_package_/Application.java.ejs +++ b/generators/spring-boot/templates/src/main/java/_package_/Application.java.ejs @@ -41,8 +41,11 @@ import java.net.UnknownHostException; import java.util.Arrays; import java.util.Collection; import java.util.Optional; +<%_ if (devDatabaseTypeH2Any) { _%> +import org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration; +<%_ } _%> -@SpringBootApplication +@SpringBootApplication(<% if (devDatabaseTypeH2Any) { %>exclude = { H2ConsoleAutoConfiguration.class }<% } %>) @EnableConfigurationProperties({<% if (databaseMigrationLiquibase) { %>LiquibaseProperties.class, <% } %>ApplicationProperties.class}) public class <%= mainClass %> { diff --git a/generators/spring-boot/templates/src/main/java/_package_/config/WebConfigurer.java.ejs b/generators/spring-boot/templates/src/main/java/_package_/config/WebConfigurer.java.ejs index 9517d2f62537..7abe376d965c 100644 --- a/generators/spring-boot/templates/src/main/java/_package_/config/WebConfigurer.java.ejs +++ b/generators/spring-boot/templates/src/main/java/_package_/config/WebConfigurer.java.ejs @@ -26,6 +26,7 @@ import tech.jhipster.config.JHipsterConstants; <%_ } _%> import tech.jhipster.config.JHipsterProperties; <%_ if (devDatabaseTypeH2Any) { _%> +import tech.jhipster.config.ApplicationProperties; import tech.jhipster.config.h2.H2ConfigurationHelper; <%_ } _%> <%_ if (!skipClient && reactive) { _%> @@ -102,14 +103,15 @@ public class WebConfigurer implements <% if (!reactive) { %>ServletContextInitia <%_ } _%> private final JHipsterProperties jHipsterProperties; - public WebConfigurer(<% if (!reactive || (devDatabaseTypeH2Any && reactive)) { %>Environment env, <% } %>JHipsterProperties jHipsterProperties) { + public WebConfigurer(<% if (!reactive || devDatabaseTypeH2Any) { %>Environment env, <% } %>JHipsterProperties jHipsterProperties) { <%_ if (!reactive) { _%> this.env = env; <%_ } _%> this.jHipsterProperties = jHipsterProperties; <%_ if (devDatabaseTypeH2Any && reactive) { _%> - if (env.acceptsProfiles(Profiles.of(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT))) { + if (h2ConsoleIsEnabled(env)) { try { + LOG.info("Initialize H2 console"); H2ConfigurationHelper.initH2Console(); } catch (Exception e) { // Console may already be running on another app or after a refresh. @@ -118,8 +120,6 @@ public class WebConfigurer implements <% if (!reactive) { %>ServletContextInitia } <%_ } _%> } - - <%_ if (!reactive) { _%> @Override @@ -129,7 +129,7 @@ public class WebConfigurer implements <% if (!reactive) { %>ServletContextInitia } <%_ if (devDatabaseTypeH2Any) { _%> - if (env.acceptsProfiles(Profiles.of(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT))) { + if (h2ConsoleIsEnabled(env)) { initH2Console(servletContext); } <%_ } _%> @@ -170,7 +170,6 @@ public class WebConfigurer implements <% if (!reactive) { %>ServletContextInitia } return extractedPath.substring(0, extractionEndIndex); } - <%_ } _%> <%_ } _%> @@ -230,14 +229,20 @@ public class WebConfigurer implements <% if (!reactive) { %>ServletContextInitia } <%_ } _%> <%_ } _%> -<%_ if (devDatabaseTypeH2Any && !reactive) { _%> +<%_ if (devDatabaseTypeH2Any) { _%> + + private boolean h2ConsoleIsEnabled(Environment env) { + return env.acceptsProfiles(Profiles.of(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT)) && "true".equals(env.getProperty("spring.h2.console.enabled")); + } + <%_ if (!reactive) { _%> /** * Initializes H2 console. */ private void initH2Console(ServletContext servletContext) { - LOG.debug("Initialize H2 console"); + LOG.info("Initialize H2 console"); H2ConfigurationHelper.initH2Console(servletContext); } + <%_ } _%> <%_ } _%> } diff --git a/generators/spring-boot/templates/src/main/resources/config/application-dev.yml.ejs b/generators/spring-boot/templates/src/main/resources/config/application-dev.yml.ejs index 401a520c1c1a..e4369b8d26e5 100644 --- a/generators/spring-boot/templates/src/main/resources/config/application-dev.yml.ejs +++ b/generators/spring-boot/templates/src/main/resources/config/application-dev.yml.ejs @@ -155,8 +155,8 @@ spring: <%_ if (devDatabaseTypeH2Any) { _%> h2: console: - # disable spring boot built-in h2-console since we start it manually with correct configuration - enabled: false + # JHipster uses a custom h2-console initializer + enabled: true <%_ } _%> <%_ if (databaseTypeMongodb) { _%> data: diff --git a/generators/spring-data-relational/templates/src/main/java/_package_/config/DatabaseConfiguration.java.ejs b/generators/spring-data-relational/templates/src/main/java/_package_/config/DatabaseConfiguration.java.ejs index 7c90b2d65e26..e1508b1132d8 100644 --- a/generators/spring-data-relational/templates/src/main/java/_package_/config/DatabaseConfiguration.java.ejs +++ b/generators/spring-data-relational/templates/src/main/java/_package_/config/DatabaseConfiguration.java.ejs @@ -43,6 +43,10 @@ import org.springframework.core.convert.converter.Converter; import org.springframework.data.elasticsearch.repository.config.Enable<% if (reactive) { %>Reactive<% } %>ElasticsearchRepositories; <%_ } _%> <%_ if (devDatabaseTypeH2Any) { _%> +import java.sql.SQLException; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.autoconfigure.h2.H2ConsoleProperties; import org.springframework.core.env.Environment; <%_ } _%> <%_ if (reactive) { _%> @@ -64,9 +68,6 @@ import org.springframework.data.jpa.repository.config.EnableJpaRepositories; <%_ } _%> import org.springframework.transaction.annotation.EnableTransactionManagement; -<%_ if (devDatabaseTypeH2Any) { _%> -import java.sql.SQLException; -<%_ } _%> <%_ if (reactive) { _%> import java.time.Duration; import java.time.Instant; @@ -94,6 +95,9 @@ import java.util.UUID; <%_ if (searchEngineElasticsearch) { _%> @Enable<% if (reactive) { %>Reactive<% } %>ElasticsearchRepositories("<%= packageName %>.repository.search") <%_ } _%> +<%_ if (devDatabaseTypeH2Any) { _%> +@EnableConfigurationProperties(H2ConsoleProperties.class) +<%_ } _%> public class DatabaseConfiguration { <%_ if (devDatabaseTypeH2Any) { _%> @@ -113,6 +117,7 @@ public class DatabaseConfiguration { */ @Bean(initMethod = "start", destroyMethod = "stop") @Profile(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT) + @ConditionalOnProperty(prefix = "spring.h2.console", name = "enabled", havingValue = "true") public Object h2TCPServer() throws SQLException { String port = getValidPortForH2(); LOG.debug("H2 database is available on port {}", port);