Announcement Announcement Module
Collapse
No announcement yet.
Usual servlet dependency injection? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Usual servlet dependency injection?

    Hello there,


    I am not a guru of Spring, probably even less than newbie.
    My question is the following.

    I have servlet defined in web.xml:

    ...
    <servlet>
    <servlet-name>my</servlet-name>
    <servlet-class>com.demo.servlets.MyServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    ...
    <servlet-mapping>
    <servlet-name>my</servlet-name>
    <url-pattern>/MyServlet</url-pattern>
    </servlet-mapping>
    ...

    This com.demo.servlets.MyServlet is just usual servlet, extending HttpServlet.
    But it has setter for variable Xxx.

    I want to use dependency injection to set this variable with Spring. I cannot modify servlet itself.

    How can I do that?
    Can somebody give me a link to example, or give an example what should I need to add to web.xml and my action-servlet.xml?

    Thank you in advance!

  • #2
    I have the exact same question. I am porting an existing servlet based web application to Spring. I would like to use dependency injection to bind a DAO with an existing servlet. Since the servlet is initialized by the web container, Tomcat, how do I get Spring to 'find' the servlet and bind some properties to it?

    Comment


    • #3
      spring cannot inject properties to the servlet. you can make spring aware of teh servlet by use the servletwrapper. I use a bit of ajax stuff in my apps and servlets are teh best way for me to interact with the server so here is what I do.

      I use the servlet wrapper as shown here. this makes spring aware of the servlet

      Code:
      	<bean id="appRoleToggle"
      		class="org.springframework.web.servlet.mvc.ServletWrappingController">
      		<property name="servletClass">
      			<value>edu.bju.aem.servlet.ajax.AppRoleToggle</value>
      		</property>
      
      	</bean>
      Then I create a registry with static methods to load the beans I need to expose. the first time the class is accessed it does reload all the spring beans obviously.
      Code:
      public class SpringRegistry {
      
      	private static ApplicationContext ctx;
      	private static String[] configs = {"WEB-INF/acegi-common-context.xml","WEB-INF/aemApp-context.xml","WEB-INF/aemApp-servlet.xml"};
      	
      	static {
      		ctx = new ClassPathXmlApplicationContext(configs);
      	}
      	
      	public SpringRegistry() {}
      	
      	public static AppDAOHibernate getAppDAOImpl(){
      		return (AppDAOHibernate)ctx.getBean("appDAOHibernateAjax");
      	}
      	public static AppRoleDAOHibernate getAppRoleDAOImpl(){
      		return (AppRoleDAOHibernate)ctx.getBean("appRoleDAOHibernate");
      	}
      	public static AppRightsDAOHibernate getAppRightsDAOImpl(){
      		return (AppRightsDAOHibernate)ctx.getBean("appRightsDAOHibernate");
      	}
      }
      hope this helps

      Comment


      • #4
        I ended up using the org.springframework.web.context.support.ServletCon textAttributeExporter class to push the beans that the servlet needs into the ServletContext. This is close to dependency injection, although it would be nice to be able to push beans directly into the servlet.

        It seems like it would be fairly straightforward to use the same mechanism to inject beans into the servlet, since the servlet can be obtained from the ServletContext. It seems like it must have been a conscious decision not to implement this. I like the idea of IoC and DI, I don't like the idea of tying my servlet to Spring or any other framework, other than the servlet container.

        I'm going to start a new thread to discuss this specific issue.
        Last edited by berniedurfee; Jan 20th, 2006, 02:43 PM. Reason: Update

        Comment


        • #5
          yes but if you use the textAttributeExporter you cannot use beans that have other dependencies in spring. for example what I an using in my servlets use hibernate, acegi, and declaritive transactions, all of which is controlled by spring. The registry doesn't tie your servlets to spring as the registry could be hooked up to anything. So if spring goes away in teh fiture you just make your registry use the new thing.

          Comment


          • #6
            The legacy code I'm dealing with has a base Servlet class that all concrete Servlet classes extend.

            Instead of having each subclass Servlet calling ApplicationContext.getBean() to resolve their dependencies, I think I can modify the superclass to use autowiring to inject dependencies, similar to what AbstractDependencyInjectionSpringContextTests does. But again, autowiring is best avoided in production code.

            Has anyone found a better solution to inject dependencies into Servlets?

            Comment

            Working...
            X