Home java How to connect the H2-console in Spring-Webmvc without Spring-Boot?

How to connect the H2-console in Spring-Webmvc without Spring-Boot?

Author

Date

Category

The application works on Spring-Webmvc and Spring JDBC without Spring-Boot . In the settings application.properties indicates the following:

spring.h2.console.enabled = true
Spring.h2.Console.Path = / H2-Console
datasource.dbname = users.
datasource.script = Classpath: Resources / Users.sql

But it does not start H2-Console because I do not use Spring-boot-DevTools , but do they need me? Therefore, I added bin Server from the Org package. H2.Tools as follows:

// The Web Server Is a Simple Standalone Http Server That
// IMPLEMENTS THE H2 CONSOLE Application. LocalHost: 8082.
@Bean (initmethod = "start", DestroyMethod = "Stop")
Public Server H2Server () Throws SQLEXception {
  Return Server.CreateWebServer ();
}

Now I can open a Localhost: 8082 and connect to the JDBC: H2: Mem: Users , just in my opinion, this is not a solution, but bypass Problems, because BIN Datasource I connect with EmbeddedDatabaseBuilder as follows:

@ bean
Public DataSource DataSource (
    @Value ("$ {datasource.dbname}") String DBName,
    @Value ("$ {datasource.script}") String Script) {
  Return New EmbeddedDatabaseBuilder ()
      .setType (embeddeddatabasetype.h2)
      .setname (DBName)
      .addscript (script)
      .build ();
}
@ bean
Public JdbCtemplate JdbCtemplate (DataSource DataSource) {
  RETURN NEW JDBCECTEMPLATE (Datasource);
}

Is there any Scringe method connecting h2-console in spring-webmvc without Spring-boot . Or is it normal – to connect it in this way?

pom.xml :

& lt;! - Spring - & gt;
& lt; dependency & gt;
  & lt; groupid & gt; org.springframework & lt; / groupid & gt;
  & lt; artifactid & gt; Spring-Webmvc & lt; / artifactid & gt;
  & lt; Version & gt; 5.2.9.release & lt; / version & gt;
& lt; / dependency & gt;
& lt; dependency & gt;
  & lt; groupid & gt; org.springframework & lt; / groupid & gt;
  & lt; artifactid & gt; Spring-JDBC & LT; / artifactid & gt;
  & lt; Version & gt; 5.2.9.release & lt; / version & gt;
& lt; / dependency & gt;
& lt;! - H2 Database - & gt;
& lt; dependency & gt;
  & lt; groupid & gt; com.h2database & lt; / groupid & gt;
  & lt; artifactid & gt; h2 & lt; / artifactid & gt;
  & lt; version & gt; 1.4.200 & lt; / version & gt;
& lt; / dependency & gt;
& lt;! - Servlet-API - & gt;
& lt; dependency & gt;
  & lt; groupid & gt; javax.servlet & lt; / groupid & gt;
  & lt; artifactid & gt; javax.servlet-API & lt; / artifactid & gt;
  & lt; version & gt; 4.0.1 & lt; / version & gt;
& lt; / dependency & gt;

Answer 1, Authority 100%

cm help – h2 tutorial Using the H2 Console Servlet :

The H2 Console IS A Standalone Application and Includes Its Own Web Server, But It Can Be Used As A Servers AS Well. To Do That, Include The H2 * .jar File in Your Application, and Add The Following Configuration to Your Web.xml :

& lt; servlet & gt;
  & lt; Servlet-Name & gt; H2CONSOLE & LT; / SERVLET-NAME & GT;
  & lt; servlet-class & gt; Org.h2.server.Web.WebServlet & LT; / Servlet-Class & GT;
  & lt; Load-on-startup & gt; 1 & lt; / load-on-startup & gt;
& lt; / servlet & gt;
& lt; servlet-mapping & gt;
  & lt; Servlet-Name & gt; H2CONSOLE & LT; / SERVLET-NAME & GT;
  & lt; URL-PATTERN & GT; / CONSOLE / * & LT; / URL-PATTERN & GT; 
& lt; / servlet-mapping & gt;

t. e. Instead of the server, you can connect only one servlet to the application, and Scringe method may look something like this:

@ configuration
Public Class WebAppconfig Implements WebAPPlicationInitializer {
   @Override
   Public Void OnStartContext (SERVLETCONTEXT SERVLETCONTEXT) {
     . . .
     ServletContext.
         .addServlet ("H2CONSOLE", WebServlet.Class)
         .addmapping ("/ Console / *");
     . . .
   }
}

Now you can connect to the Web console as usual http: // Localhost: 8080 / Console

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions