Announcement Announcement Module
No announcement yet.
dependency injection question from factory-method Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • dependency injection question from factory-method

    If a bean is using setter based DI for a particular attribute and the attribute ref is to a factory-method that has a scope of 'request', will that factory-method be called each time the bean is used or will the factory-method be called only once when the bean is initialized at context startup?

        <!--bean id="fooBean"
         <bean id="someController"
              <property name="foo" ref="fooBean" />
    So I would expect that the fooBean.getObject() would be called for each new invocation of someController within a Request but I do not see that happening. It appears as though foo is set on someController only once (at the Bean Container startup).

    Am I expecting the wrong behavior?

  • #2
    How to write a JUnit Test case for following code.?

    package net.*;


    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.Controller;

    public class HelloWorldController implements Controller {

    public ModelAndView handleRequest(HttpServletRequest request,
    HttpServletResponse response) throws ServletException, IOException {

    String Mess = "Hello";

    ModelAndView modelAndView = new ModelAndView("hello");
    modelAndView.addObject("message", Mess);

    return modelAndView;


    • #3
      I suggest a read of the reference guide and how to use scoped beans with dependency injection and how they work. In short you get a proxied object which does the lookup, so injection is once but delegated at runtime, however your configuration is flawed (check the reference guide for that).