Announcement Announcement Module
Collapse
No announcement yet.
Request Scoped Bean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Request Scoped Bean

    Hi
    I've a singleton factory class that is autowired with bean in request scope. The request scoped bean returns a new instance of an implementation. FOr instance:

    My Factory:

    public class MyFactory {

    @Autowired
    private MyObjectCreator myObjectCreator;

    public MyImpl getMyImpl() {
    return myObjectCreator.getImpl();
    }
    }


    public class MyObjectCreatorImpl implements MyObjectCreator () {

    public MyImpl getMyImpl() {
    if (some condition)
    return new MyImpl1(); // MyImpl1 implements MyImpl
    else
    return new MyImpl2(); //MyImpl2 implements MyImpl

    }


    }


    public class MyImpl1 implements MyImpl {

    @Resource(name="resource1")
    private Resource1 resource1;

    @Resource(name="resource2")
    private Resource2 resource2;

    public somereturntype doSomething() {
    ....
    ......
    }

    }

    Everything works fine and the bean in request scope is initialized. The issue here is, if I invoke any method in MyImpl1, I noticed all the fields, resource1 and resource2 are null. Any reason why all those resources are not initialized? Because of this the method doSomething() throws null pointer exception, invoking any method on the resources.

    The xml configuration is:

    <bean id="myObjectCreator" class="com.xyz.MyObjectCreatorImpl">
    <aop:scoped-proxy/>

    <bean id="myFactory" class="com.xyz.MyFactory">
    </bean>

    <bean id="resource1" class="com.abc.Resource1">
    </bean>
    Last edited by chockbal; Feb 21st, 2012, 03:01 PM.

  • #2
    Please use [ code][/code ] tags when posting code.

    You are constructing the objects yourself not spring, spring will not inject beans it doesn't know.

    Comment


    • #3
      Beans that are instantiated using "new" are not managed by spring and @Resource (or any other annotation) is not processed.

      To solve that, you have 2 solutions:

      The easy one is to put the resources in MyObjectCreatorImpl, and inject them in MyImpl1 or MyImpl2 using a setter

      Code:
      public class MyObjectCreatorImpl implements MyObjectCreator () {
      
      	@Resource(name="resource1")
      	private Resource1 resource1;
      
      	@Resource(name="resource2")
      	private Resource2 resource2;
      
      	public MyImpl getMyImpl() {
      		MyImpl myImpl = null;
      		if (some condition) {
      			myImpl = new MyImpl1(); // MyImpl1 implements MyImpl
      		} else {
      			myImpl = new MyImpl2(); //MyImpl2 implements MyImpl
      		}
      		myImpl.setResource1(resource1);
      		myImpl.setResource2(resource2);
      		return myImpl;
      	}
      }
      
      
      public class MyImpl1 implements MyImpl {
      
      	@Resource(name="resource1")
      	private Resource1 resource1;
      
      	@Resource(name="resource2")
      	private Resource2 resource2;
      
      	//getters and setters
      
      	public somereturntype doSomething() {
      	....
      	......
      }
      
      }
      The difficult one is to use AspectJ and annotate the classes MyImpl1 and MyImpl2 with @Configurable (See "8.8 Using AspectJ with Spring applications" in spring documentation)

      Comment

      Working...
      X