Announcement Announcement Module
No announcement yet.
confused about factory-bean, factory-method Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • confused about factory-bean, factory-method

    I've been using Spring for several weeks, and thought I understood what has been
    going on, but I've suddenly realised I'm totally confused about using static &
    instance factories vs using constructor/setter injection.

    Having read the relevant parts of the reference (section 3, but in particular
    " Bean creation via static factory method" and " Bean creation
    via instance factory method"), I thought I could just switch constructor
    injection to a static/instance factory method and the same thing would happen.

    But there is some major differences which I don't really understand:

    1) when I do appContext.getBeanNamesForType(Object.class) (or
    appContext.getBeanNamesForType(Object.class,true, true)) I would expect to get
    all bean names, including ones set by an instance factory method. However I
    don't - I only get names of "regular" beans (ie with class param) and static
    factory methods (However I can get them nonetheless with a getBean(name), they
    just aren't listed...)

    2) when I do appContext.getBeansOfType(X.class) - I would expect to a
    comprehensive and exclusive Map of objects - ie. all are defined objects of type
    X and ONLY objects of type X (and subtypes). However, again instance factory
    methods are excluded and (strangely of strangley) a object of type "String"
    which happens to be created by a factory-bean of type X.

    NB: I don't really understand the sentance in the api docs on getBeansOftype():
    "Does consider objects created by FactoryBeans but rather the FactoryBean
    classes themselves, which means that FactoryBeans will get instantiated."

    3) Is something then that is created with using the <bean factory-bean="..."
    factory-method="..."/> itself a FactoryBean? (Why? doesn't suggest
    this at all).

    Can anyone help with this -- I'm very confused. If there's some docs I've
    missed reading, please point me to them.

    -- Tim

    (duplicate on gmane. apologies - trying to work out where best to post.)