开发者

How do I catch Spring errors when starting up a Tomcat webapp?

开发者 https://www.devze.com 2023-04-12 13:48 出处:网络
I have a webapp running in Tomcat which uses Spring for dependency injection. (It\'s a GWT application, but I don\'t think that makes much of a difference to the solution I\'m looking for.)

I have a webapp running in Tomcat which uses Spring for dependency injection. (It's a GWT application, but I don't think that makes much of a difference to the solution I'm looking for.)

My web.xml file is of the following format:

<web-app>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Servlets -->
<servlet>
    <servlet-name>dispatch</servlet-name>
    <servlet-class>com.example.my.gwt.dispatch.DispatchServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatch</servlet-name>
    <url-pattern>/my_gwt/dispatch</url-pattern>
</servlet-mapping>

    ... more servlets ...

</web-app>

One of the things my Spring configuration does is to connect to a databse via Hibernate:

<bean id="datasource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${db.driver}" />
    <property name="url"
        value="${db.url}" />
    <property name="username" value="${db.username}" />
    <property name="password" value="${db.password}" />
</bean>
<bean id="databaseSessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="datasource" />
    <property name="packagesToScan">
        <array>
            <value>com.example.my.gwt.model</value>
        </array>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        开发者_运维问答</props>
    </property>
</bean>

If the database is unavailable, this causes an org.h2.jdbc.JdbcSQLException to be thrown, so the Spring initialisation does not continue, so the rest of the webapp cannot be used. Navigating to the webapp's URL gives an HTTP 503 'Service Unavailable' error.

What I want to do is to catch that error and display a page to the user (when they first navigate to the app) explaining what the problem is likely to be and suggested fixes. How can I do this?

I have tried using a custom ContextLoaderListener class that delegates to the one in the XML above, but catches any exceptions. This allows me to catch the exception, but there is not much I can do - the web.xml is still pointing the user's request to a servlet that is not running after the Spring initialisation has failed. Is there any way that I can change the webapp config when I catch that exception, so that it doesn't try to load the servlets from the web.xml and perhaps changes the welcome file to point to a page about the error? Or is there any other way that I can make the webapp gracefully handle this exception?

Thanks


Basically you're asking if you can have a functioning web application after the web application fails to start up.

You could try configuring a 503 handler page and/or have a welcome page, not dependent on Spring, that checks for something in the application context that's set only on a good spin up. If it didn't spin up, the exception you've already captured could be placed into the app context.

Not sure if anything in the app, even web.xml-only resources, if Spring doesn't spin up, though.


You could add a Servlet Filter to your web app that would intercept all the requests to the Spring servlet and forward to your custom error page if the Spring initialization has failed.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号