Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6908073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:30:56+00:00 2026-05-27T08:30:56+00:00

I have a Spring app which launches a REST Service within an embedded instance

  • 0

I have a Spring app which launches a REST Service within an embedded instance of Jetty, which itself is launched from Spring.

The initial Spring context has an integration and database layer, and launches the Jetty instance. Jetty then calls its own application context file which exposes the REST service.

I would like to know if there’s some way of exposing the initial Spring context to the web context run from within Jetty. Unfortunately I can’t deploy a full J2EE server, and I’m hesitant to run everything from the Web context, relying on Jetty to manage threading and such.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:30:57+00:00Added an answer on May 27, 2026 at 8:30 am

    So I found yet a better answer based off the one from ericacm above. The only reason that it’s better is that you can still use <load-on-startup> for the servlets in the web.xml file.

    When embedding the Jetty server, you need to create a WebAppContext. The super-class ContextHandler lets you set an array of EventListener which includes ServletContextListener.

    So the solution is extend ContextLoader and implement both Spring’s ApplicationContextAware and the ServletContextListener interface. The loader lets you return the parent context set by the contextaware interface, and the listener provides you the ServletContext via contextInitialized().

    You can then initialize this before any of the Jetty components, and get access to the fully populated ServletContext as the Jetty server is loading, which gets called before any of the web app’s themselves are initialized.

    Listener implementation:

    import javax.servlet.ServletContext;
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    
    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.web.context.ContextLoader;
    
    public final class EmbeddedJettySpringContextLoaderListener 
        extends ContextLoader 
        implements ApplicationContextAware,
                   ServletContextListener
    {
        private ApplicationContext applicationContext;
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            this.applicationContext = applicationContext;
        }
    
        /**
         * Returns the parent application context as set by the
         * {@link ApplicationContextAware} interface.
         * 
         * @return The initial ApplicationContext that loads the Jetty server.
         */
        @Override
        protected ApplicationContext loadParentContext(ServletContext servletContext) {
            return this.applicationContext;
        }
    
        @Override
        public void contextInitialized(ServletContextEvent sce) {
            super.initWebApplicationContext(sce.getServletContext());
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent sce) {
            //not needed
        }
    }
    

    Jetty config for WebAppContext (eventually referenced by the server):

    <!-- Loads this application context as the parent of the web application context. -->
    <bean id="parentContextLoaderListener" class="com.citi.matrix.server.restapi.EmbeddedJettySpringContextLoaderListener" />
    
    <bean id="restApiWebAppContext" class="org.mortbay.jetty.webapp.WebAppContext">
      <property name="displayName" value="RestApi" />
      <property name="contextPath" value="/" />
      <!-- the value for war, must be a relative path to the root of the application, and does not use the classpath. -->
      <property name="war" value="${WEBAPPS_HOME}/rest-api" />
      <property name="eventListeners">
        <ref  local="parentContextLoaderListener" />
      </property>
      <property name="configurationClasses">
        <list>
          <value>org.mortbay.jetty.webapp.WebInfConfiguration</value>
          <value>org.mortbay.jetty.webapp.WebXmlConfiguration</value>
          <value>org.mortbay.jetty.webapp.JettyWebXmlConfiguration</value>
        </list>
      </property>
      <property name="logUrlOnStart" value="true" />
    </bean>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom DownloadFiles class which extends AsyncTask . The app launches itself
I have an AsyncTask which launches a DatabaseHelper class from DoinBackground which copies an
I have what seems like a simple problem. I have a Spring web app,
We have a (non-web app) Spring application that throws a NoSuchBeanDefinitionException when running tests
We have a Spring + Ibatis based J2EE app. I planned to wrap around
I have an app that uses Spring security and BlazeDS. Flex 3.2 is used
I have a Spring Interceptor which attempts to add an HTTP header in the
I have a alarm thing going on in my app and it launches a
I have a strange problem with my Android app. When I start it from
I have a home page activity that I got from an example, which works

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.