Announcement Announcement Module
Collapse
No announcement yet.
Echo framework and Spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Echo framework and Spring

    Does anyone have any tips or suggestions on how to integrate the Echo framework for the web tier with a spring app?

  • #2
    Preliminary note: I've never worked with Echo, just had a look at its tutorials and javadocs.

    As far as I see, each Echo application has to derive from the EchoServer servlet, which in turn has to provide a new subclass of EchoInstance on each newInstance() callback. I'll also assume that a Spring root application context is loaded in the web application, via Spring's ContextLoaderListener in web.xml.

    Code:
    <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    As EchoServer derives from HttpServlet, you have access to the ServletContext via getServletContext(). This means that you can fetch a reference to the Spring root application context via the usual WebApplicationContextUtils.getWebApplicationContex t(servletContext) method, getting access to any beans there.

    You could either pass the WebApplicationContext reference itself to each new EchoInstance, or specific beans that you have fetched from the ServletContext. As an EchoInstance seems to be a pretty coarse-grained object, very central to an Echo application, it's probably preferable to pass the WebApplicationContext reference itself in.

    Code:
    public class NumberGuessServlet extends EchoServer &#123;
    
      public EchoInstance newInstance&#40;&#41; &#123;
        WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext&#40;getServletContext&#40;&#41;&#41;;
        return new NumberGuess&#40;wac&#41;;
        // or new NumberGuess&#40;myBeanFromWac, myOtherBeanFromWac&#41;;
      &#125;
    &#125;
    Alternatively, you could define the EchoInstance itself as a Spring prototype bean, probably in a separate XML file joined into your Spring root context. This would allow you to declaratively wire it with middle tier beans, using all of Spring's bean setup power. You would then simply call getBean on the Spring context to get the EchoInstance:

    Code:
    <beans>
      
      <bean id="numberGuess" class="numberguess.NumberGuess">
        <property name="myProperty">...</property>
      </bean>
    
    </beans>
    Code:
    public class NumberGuessServlet extends EchoServer &#123;
    
      public EchoInstance newInstance&#40;&#41; &#123;
        WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext&#40;getServletContext&#40;&#41;&#41;;
        return &#40;NumberGuess&#41; wac.getBean&#40;"numberGuess", NumberGuess.class&#41;;
      &#125;
    &#125;
    So in total, integration between an Echo web tier and a Spring middle tier should be straightforward, with a variety of options.

    Juergen

    Comment

    Working...
    X