Results 1 to 3 of 3

Thread: dependency injection question from factory-method

  1. #1
    Join Date
    Sep 2012
    Posts
    5

    Default 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?

    Code:
        <!--bean id="fooBean"
              scope="request"
              factory-bean="someFactoryBean"
              factory-method="getObject">
        </bean-->
    
         <bean id="someController"
              <property name="foo" ref="fooBean" />
            ......
          </bean>
    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. #2
    Join Date
    Oct 2012
    Posts
    1

    Default

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

    package net.*;

    import java.io.IOException;

    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. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    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).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •