本文介绍了如何注入FactoryBean而不是它产生的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下Spring配置(Spring的版本为3.0.3):

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd">

   <bean id="theFactoryBean" class="wax.MyFactoryBean"/>

   <bean id="factoryBeanUser" class="wax.FactoryBeanUser">
       <!-- what should be placed here?-->
   </bean>

</beans>

我有FactoryBean实现的实例和其他一些实例.我需要Spring注入其他实例FactoryBean,而不是它生成的对象.

I have instance of FactoryBean implementation and some other instance. I need Spring to inject to other instance FactoryBean, not the object it produces.

有两种解决方法.

第一个明显的故障:

 <bean id="factoryBeanUser" class="wax.FactoryBeanUser">
    <property name="myFactoryBean" ref="&theFactoryBean"/>
</bean>

使用此配置,Spring会在启动时引发以下异常:

With this config Spring throws following exception on start:

    [skipped irrelevant part]
Caused by: org.xml.sax.SAXParseException: The reference to entity "theFactoryBean" must end with the ';' delimiter.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:174)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:388)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1414)

我找到了这个解决方案 Spring:获取FactoryBean对象而不是FactoryBean. getObject(),回答这个问题,有四个人投了赞成票.因此,我认为此解决方案可能有效,但是目前我的代码中有问题.

I found this solution Spring: Getting FactoryBean object instead of FactoryBean.getObject(), this question is maked as answered and four people voted for it. So I assume that this solution might work, but currently there is something wrong in my code.

第二个,工作却很尴尬:

Second one, working but awkward:

public class FactoryBeanUser implements ApplicationContextAware{

private MyFactoryBean myFactoryBean;

   public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        myFactoryBean = (MyFactoryBean)applicationContext.getBean("&theFactoryBean");
   }
}

我的问题是可以使第一种方法起作用还是我应该坚持第二种方法?

My question is it possible to make first approach functional or I should stick with a second way?

推荐答案

似乎XML解析器将&符号解释为XML实体的开始.您可以尝试使用ref="&amp;theFactoryBean".

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

spring docs 尚不清楚是在xml文件中允许使用此语法,还是仅在程序化查找中允许使用此语法.但是随后应用程序上下文使用了xml配置,因此我认为&amp;应该可以工作(尽管对于特殊符号来说,它似乎并不是最佳选择)

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 &amp; should work (although it seems it has not been the best choice for a special symbol)

这就是为什么我建议另一件事的原因-如果您确实需要工厂bean而不是其产品,则创建另一个未实现FactoryBean的bean,定义方法createObject()或类似的方法,然后使用它在所有需要它的工厂中.

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.

附带说明-更好地参考包含以下版本的xsd:

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

这篇关于如何注入FactoryBean而不是它产生的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 13:26