View content of embedded H2 database started by Spring

前端 未结 7 1029
忘掉有多难
忘掉有多难 2020-12-04 09:53

I would like to view in a web browser the content of the H2 database started by Spring thanks to the following configuration:



        
7条回答
  •  不思量自难忘°
    2020-12-04 10:16

    For those wanting a Java Config setup it's fairly easy to do as well initializing the TCP server when implementing ServletContextInitializer and chaining the Console Server...

    @Configuration
    public class WebConfig implements ServletContextInitializer{
    ...
    
    @Override
    public void onStartup( ServletContext servletContext )
    //do stuff onStartUp...
    initH2TCPServer( servletContext );
    ....    
    
    @Bean(initMethod="start", destroyMethod="stop")
    public Server initH2TCPServer(ServletContext servletContext) {
        log.debug( "Initializing H2 TCP Server" );
        try {
            server = Server.createTcpServer( "-tcp", "-tcpAllowOthers", "-tcpPort", "9092" );
        } catch( SQLException e ) {
            e.printStackTrace();
        } finally {
            //Always return the H2Console...
            initH2Console( servletContext );
        }
        return server;
    }
    
    public void initH2Console( ServletContext servletContext ) {
        log.debug( "Initializing H2 console" );
        ServletRegistration.Dynamic h2ConsoleServlet = servletContext.addServlet(
        "H2Console", new org.h2.server.web.WebServlet() );
        h2ConsoleServlet.addMapping( "/console/*" );
     );
    }
    

提交回复
热议问题