Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spring-boot: allow h2 console to be enabled/disabled using spring.h2.console.enabled property #27649

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 %> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) { _%>
Expand Down Expand Up @@ -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.
Expand All @@ -118,8 +120,6 @@ public class WebConfigurer implements <% if (!reactive) { %>ServletContextInitia
}
<%_ } _%>
}


<%_ if (!reactive) { _%>

@Override
Expand All @@ -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);
}
<%_ } _%>
Expand Down Expand Up @@ -170,7 +170,6 @@ public class WebConfigurer implements <% if (!reactive) { %>ServletContextInitia
}
return extractedPath.substring(0, extractionEndIndex);
}

<%_ } _%>
<%_ } _%>

Expand Down Expand Up @@ -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);
}
<%_ } _%>
<%_ } _%>
}
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) { _%>
Expand All @@ -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;
Expand Down Expand Up @@ -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) { _%>
Expand All @@ -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);
Expand Down
Loading