Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jetty-users] Using Spring inside Jetty

Given this web.xml

    <web-app 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
        xmlns="http://java.sun.com/xml/ns/javaee"; 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd";
        metadata-complete="false"
        version="3.1">
        <!-- Spring Context Parameters -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:/org/nwea/etl/spring/applicationContext-web.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    </web-app>

and this applicationContext-web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans";
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
           xmlns:context="http://www.springframework.org/schema/context";
           xsi:schemaLocation=
            "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd";>

        <bean id="quartzSchedulerFactory" class="org.quartz.impl.StdSchedulerFactory"/>
        <bean id="coreScheduler" factory-bean="quartzSchedulerFactory" factory-method="getScheduler" init-method="start"/>
        <bean id="helloServlet" class="org.nwea.etl.helloworld.HelloWorld"/>
    </beans>
    
With helloServlet defined as

    @WebServlet(name="hello", urlPatterns={"/hello"})
    public class HelloWorld extends HttpServlet
    {
        @Autowired
        private Scheduler coreScheduler;  
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
        {
            etc...

The HelloWorld class gets instantiated and coreScheduler is injected by Spring,
but then gets instantiated again by Jetty (without coreScheduler)
when the client sends a GET /hello.

Is there a way to configure Jetty to retrieve the servlet instance
from Spring?

Is the correct solution to use Spring's DispatcherServlet in front of everything I want configured via Spring, or is there a more JEE-6 way to do this with Jetty?


Back to the top