最近在探索Quartz的定时任务以数据库方式进行存储获取,其中用到了Spring的MethodInvokingJobDetailFactoryBean。在注入MethodInvokingJobDetailFactoryBean的时候,发现总是出现异常参数的错误。

Caused by: java.lang.IllegalArgumentException: Cannot convert value of type [org.quartz.JobDetail] to required type [org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean] for property 'fileTypeJobDetailFactoryBean': no matching editors or conversion strategy foundat org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:231)at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:138)at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:386)... 33 more

    原因是MethodInvokingJobDetailFactoryBean实现了FactoryBean的接口,而FactoryBean在Spring进行注入的时候,使用的是FactoryBean的getObject()方法,而不是把FactoryBean自身的实例进行注入。查找了FactoryBean自身的说明也没有找到相关解决办法。

上网搜索终于找到了解决办法,那就是使用【&】符号,如果需要注入FactoryBean的实例时,使用&beanName进行注入即可,这个在ApplicationContext的getBean方法中使用是没有问题的,但是在XML中使用就会报出来这样的错误。

Caused by: org.xml.sax.SAXParseException: The reference to entity "XXXXXXXX" 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)at com.sun.org.apache.xerces.internal.impl.XMLScanner.scanAttributeValue(XMLScanner.java:868)

  这样的话,就需要进行转义,所以在XML中就需要写成这样&beanName,这样的话,FactoryBean就可以顺利注入了。

另外回答中也提到了是参考Spring的官方文档【 Customizing instantiation logic using FactoryBeans】,我把关键的地方用红字进行了标注。

http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-factory-extension-factorybean

3.7.3. Customizing instantiation logic using FactoryBeans

The org.springframework.beans.factory.FactoryBean interface is to be implemented by objects that are themselves factories.

The FactoryBean interface is a point of pluggability into the Spring IoC containers instantiation logic. If you have some complex initialization code that is better expressed in Java as opposed to a (potentially) verbose amount of XML, you can create your own FactoryBean, write the complex initialization inside that class, and then plug your customFactoryBean into the container.

The FactoryBean interface provides three methods:

  • Object getObject(): has to return an instance of the object this factory creates. The instance can possibly be shared (depending on whether this factory returns singletons or prototypes).

  • boolean isSingleton(): has to return true if this FactoryBean returns singletons, false otherwise

  • Class getObjectType(): has to return either the object type returned by the getObject() method or null if the type isn't known in advance

The FactoryBean concept and interface is used in a number of places within the Spring Framework; at the time of writing there are over 50 implementations of the FactoryBeaninterface that ship with Spring itself.

Finally, there is sometimes a need to ask a container for an actual FactoryBean instance itself, not the bean it produces. This may be achieved by prepending the bean id with '&'(sans quotes) when calling the getBean method of the BeanFactory (including ApplicationContext). So for a given FactoryBean with an id of myBean, invokinggetBean("myBean") on the container will return the product of the FactoryBean, but invoking getBean("&myBean") will return the FactoryBean instance itself.

本文参照:

【Spring: Getting FactoryBean object instead of FactoryBean.getObject()】

http://stackoverflow.com/questions/1655140/spring-getting-factorybean-object-instead-of-factorybean-getobject

转载于:https://www.cnblogs.com/kaka/archive/2013/04/01/2993503.html

FactoryBean在XML中的依赖注入方法相关推荐

  1. spring中的依赖注入——构造函数注入、set方法注入( 更常用的方式)、复杂类型的注入/集合类型的注入

    spring中的依赖注入 依赖注入: Dependency Injection IOC的作用:降低程序间的耦合(依赖关系) 依赖关系的管理:以后都交给spring来维护.在当前类需要用到其他类的对象, ...

  2. JavaEE开发之Spring中的依赖注入与AOP编程

    上篇博客我们系统的聊了<JavaEE开发之基于Eclipse的环境搭建以及Maven Web App的创建>,并在之前的博客中我们聊了依赖注入的相关东西,并且使用Objective-C的R ...

  3. dagger2 注入_如何使用Dagger 2在您的应用程序中实现依赖注入

    dagger2 注入 Kriptofolio应用程序系列-第4部分 (Kriptofolio app series - Part 4) Dependency injection will signif ...

  4. 转: 理解AngularJS中的依赖注入

    理解AngularJS中的依赖注入 AngularJS中的依赖注入非常的有用,它同时也是我们能够轻松对组件进行测试的关键所在.在本文中我们将会解释AngularJS依赖注入系统是如何运行的. Prov ...

  5. 理解AngularJS中的依赖注入

    作者 CraftsCoder 冷月无声 - 博客频道 - CSDN.NET http://blog.csdn.net/jaytalent/article/details/50986402 本文结合一些 ...

  6. Angular 中的依赖注入link

    Angular 中的依赖注入link 依赖注入(DI)是一种重要的应用设计模式. Angular 有自己的 DI 框架,在设计应用时常会用到它,以提升它们的开发效率和模块化程度. 依赖,是当类需要执行 ...

  7. ASP.NET CORE MVC 2.0 如何在Filter中使用依赖注入来读取AppSettings

    问: ASP.NET CORE MVC 如何在Filter中使用依赖注入来读取AppSettings 答: Dependency injection is possible in filters as ...

  8. SAP Spartacus 中的依赖注入 Dependency Injection 介绍

    先了解 Angular 中的依赖注入 依赖项是指某个类执行其功能所需的服务或对象.依赖项注入(DI)是一种设计模式,在这种设计模式中,类会从外部源请求依赖项而不是让类自己来创建它们. Angular ...

  9. ASP.NET Core中的依赖注入(4): 构造函数的选择与服务生命周期管理

    ServiceProvider最终提供的服务实例都是根据对应的ServiceDescriptor创建的,对于一个具体的ServiceDescriptor对象来说,如果它的ImplementationI ...

  10. 更优雅的在 Xunit 中使用依赖注入

    Xunit.DependencyInjection 7.0 发布了 Intro 上次我们已经介绍过一次大师的 Xunit.DependencyInjection  在 Xunit 中使用依赖注入 ,最 ...

最新文章

  1. live555源码分析----RSTPServer创建过程分析
  2. 计算机视觉开源库OpenCV之照明和色彩空间
  3. hpm1216nfh驱动程序_惠普HP LaserJet Pro M1216nfh 一体机驱动
  4. 边框border属性总结
  5. ubuntu下chrome以代理模式启动
  6. double转整数问题
  7. java面向对象之父类的引用指向子类的对象
  8. php object 对象不存在。增加对象_PHP核心
  9. Learning Scrapy笔记(零) - 前言
  10. 计算机函数填写评价,信息技术应用 用计算机画函数图象教学评价实录
  11. Java三种连接池(druid、c3p0、dbcp)
  12. OJ0428 二分查找F701
  13. 球体重量在线计算机,用图形计算器研究热气球.doc
  14. 阿里云OCR:(二)银行卡号识别
  15. javascript实现常用的设计模式
  16. 超清楚!麦克风阵列学习笔记(一)——线性麦克风阵列的时间延迟Beamforming算法(Time-Delay Beamforming of Microphone ULA Array)
  17. win10重装应用商店
  18. 信创办公–基于WPS的EXCEL最佳实践系列 (筛选重要数据)
  19. edu教育邮箱免费申请注册Google drive无限网盘和微软OneDrive经验分享
  20. 10个iPhone开发网站、论坛、博客

热门文章

  1. python 3 并发编程之多进程 multiprocessing模块
  2. Linux RedHat 5.2 构建PostFix邮件服务器
  3. Java线程间通信方式
  4. vue routes路由
  5. 新手学JavaScript都要学什么?
  6. [BJOI2017]开车
  7. POJ-3468-A Simple Problem with integers
  8. memcached群集
  9. sar —— Linux 上最为全面的系统性能分析工具之一
  10. Django 框架之 URL