How to inject FactoryBean instead of object it produces?

前端 未结 1 1244
南旧
南旧 2021-02-12 22:48

Let\'s say I have following Spring config (version of Spring is 3.0.3):




        
1条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-12 23:09

    It seems the XML parser interprets the ampersand (&) as a start of an XML-entity. You can try using ref="&theFactoryBean".

    The spring docs is not clear whether this syntax is allowed in an xml file, or only with programatic lookup. But then the xml configuration is used by the app context, so I assume the & should work (although it seems it has not been the best choice for a special symbol)

    Here's why I'd suggest another thing - if you really need the factory bean rather than its product, create another bean, that does not implement FactoryBean, define a method createObject() or something like that, and use it in all factories that need it.

    A sidenote - better reference the xsd with the version included:

    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

    0 讨论(0)
提交回复
热议问题