Results 1 to 9 of 9

Thread: Manually creating a new bean instance from a BeanDefinition

  1. #1

    Question Manually creating a new bean instance from a BeanDefinition

    I've got a slightly out-of-the-ordinary situation regarding lazy initialization...

    I've got a specific singleton (root) bean that has a nested graph of dependencies on other inner beans. I want the 'leaf' inner beans to be lazy-initialized, since they acquire expensive resources, and not all of them are necessarily used in each application run.

    However, it is obvious that with the standard container/bean lifecycle, this entire bean 'tree' is initialized in one go, so the lazy-init setting is only really meaningful at the root bean level.

    It's a pity Spring doesn't provide some sort of lazy-init-placholder mechanism that caches the BeanDefinition and only instantiates the bean instance on demand. I'm actually considering doing this myself, with the use of a custom BeanDefinitionParser: the only problem I have is that none of the BeanFactory types expose a public method to create a bean instance from an already-created BeanDefinition. The only way to get hold of that kind of method is to subclass a BeanFactory implementation.

    Anyone done something like this before?

  2. #2
    Join Date
    Feb 2005
    Location
    Boston, MA
    Posts
    1,142

    Default

    How about using a LazyInitTargetSource. It creates a little overhead for your leaf beans, but it might be worth it. I can't imagine doing it any other way because any time you want an object property to not exist when its parent object exists is to create some sort of proxy.
    Bill

  3. #3

    Default

    Quote Originally Posted by wpoitras View Post
    How about using a LazyInitTargetSource. It creates a little overhead for your leaf beans, but it might be worth it. I can't imagine doing it any other way because any time you want an object property to not exist when its parent object exists is to create some sort of proxy.
    Ah, that looks interesting. Didn't think of looking in the AOP packagaes .
    However, even if this works it's still not ideal "aesthetically", because I'll have to move all the target beans - currently inner beans - to be top-level beans (in order for the idref to work). No?

  4. #4
    Join Date
    May 2007
    Posts
    10

    Default

    Just a thought but maybe the <aoproxy/> tag would help? I'm not 100% this will solve your problem but Spring will create a proxy for you and inject that into your parent bean, and then hopefully only when you invoke the proxy would your lazy singletons be instantiated. Maybe give it a try!

    -a

  5. #5
    Join Date
    Feb 2005
    Location
    Boston, MA
    Posts
    1,142

    Default

    Quote Originally Posted by cornel.masson View Post
    Ah, that looks interesting. Didn't think of looking in the AOP packagaes .
    However, even if this works it's still not ideal "aesthetically", because I'll have to move all the target beans - currently inner beans - to be top-level beans (in order for the idref to work). No?
    You are correct. It has the use the idref because if you don't, once the object is referenced (ie using ref) then the object gets created. So an idref really really is the only way to specify a bean which will be lazy loaded.

    It does sound a little uglier but not as ugly as a slow loading app
    Bill

  6. #6

    Default

    Quote Originally Posted by wpoitras View Post
    You are correct. It has the use the idref because if you don't, once the object is referenced (ie using ref) then the object gets created. So an idref really really is the only way to specify a bean which will be lazy loaded.

    It does sound a little uglier but not as ugly as a slow loading app
    OK, I've fixed it . In the XML, my leaf beans are now still defined as inner beans in the tree, but the bean that contains them has a custom BeanDefinitionParser that reads these inner beans, registers them as top-level beans (each with a unique name), and then sets a LazyInitTargetSource on itself to reference the top-level bean as target.

    This allows those faux top-level beans to be lazy initialized, but in the XML they sit neatly in the tree as inner beans.

    Nasty, but it works

  7. #7
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    Sorry, but I do not see <aop:proxy> neither in Spring spring-aop-2.0.xsd/spring-aop-2.5.xsd nor in spring reference. Where can I find info about it?

    Quote Originally Posted by andymorris View Post
    Just a thought but maybe the <aop:proxy/> tag would help? I'm not 100% this will solve your problem but Spring will create a proxy for you and inject that into your parent bean, and then hopefully only when you invoke the proxy would your lazy singletons be instantiated. Maybe give it a try!

    -a

  8. #8

    Default

    Quote Originally Posted by al0 View Post
    Sorry, but I do not see <aoproxy> neither in Spring spring-aop-2.0.xsd/spring-aop-2.5.xsd nor in spring reference. Where can I find info about it?
    I think he's referring to <aop:scoped-proxy/>.

  9. #9
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    Spring reference states in chapter 3.4.4.5

    You do not need to use the <aop:scoped-proxy/> in conjunction with beans that are scoped as
    singletons or prototypes. It is an error to try to create a scoped proxy for a singleton bean (and
    the resulting BeanCreationException will certainly set you straight in this regard).
    (bolding is mine).

    But LazyInitTargetSource suggested by wpoitras shoud do a trick. BTW, conversion of inner beans to top-level beans is not so big disadvantage, in worst case you may separate them to their own file for clarity.

    Quote Originally Posted by cornel.masson View Post
    I think he's referring to <aop:scoped-proxy/>.
    Last edited by al0; Mar 28th, 2008 at 02:45 AM.

Posting Permissions

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