惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cloudbric
Cloudbric
有赞技术团队
有赞技术团队
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
Threat Research - Cisco Blogs
L
LangChain Blog
Simon Willison's Weblog
Simon Willison's Weblog
Project Zero
Project Zero
Latest news
Latest news
S
Schneier on Security
Cisco Talos Blog
Cisco Talos Blog
MyScale Blog
MyScale Blog
C
Check Point Blog
IT之家
IT之家
P
Palo Alto Networks Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
CERT Recently Published Vulnerability Notes
Scott Helme
Scott Helme
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
G
Google Developers Blog
T
Tor Project blog
T
Threatpost
D
DataBreaches.Net
博客园 - 【当耐特】
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Troy Hunt's Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Vercel News
Vercel News
云风的 BLOG
云风的 BLOG
NISL@THU
NISL@THU
P
Privacy & Cybersecurity Law Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
Cisco Blogs
博客园_首页
S
Securelist
T
The Exploit Database - CXSecurity.com
Last Week in AI
Last Week in AI
量子位
U
Unit 42
Know Your Adversary
Know Your Adversary
Hugging Face - Blog
Hugging Face - Blog
S
Security Affairs
Google Online Security Blog
Google Online Security Blog
Hacker News: Ask HN
Hacker News: Ask HN
Webroot Blog
Webroot Blog
S
SegmentFault 最新的问题
Engineering at Meta
Engineering at Meta
N
News and Events Feed by Topic
P
Proofpoint News Feed
阮一峰的网络日志
阮一峰的网络日志

博客园 - 南郭先生kaka

待处理数据的两种模型 nginx重启无法找到PId的解决办法 Wiki版产品需求---产品需求文档到底是谁的?产品到底是谁的? 为什么说private方法是有罪的 XML2JSON 的【net.sf.json.JSONException: nu.xom.ParsingException must be followed by either attribute specifications, ">" or "/>"】问题解决办法 继承的第一原则 致那些不甘寂寞的人 上传File时,浏览器总是添加<pre>的解决办法 使用XSLT转换XML2XML Java 模拟 Http Post WITH AS SQL语句的用法 【Code Style】多余判断 java.lang.NoClassDefFoundError错误 Spring 中Quartz配置数据库化 Camel FTP中文目录解决办法 配置CentOS6.3 NFS Bean复制的几种框架性能比较(Apache BeanUtils、PropertyUtils,Spring BeanUtils,Cglib BeanCopier) 重读《目标》---目标 重读《目标》---序
FactoryBean在XML中的依赖注入方法
南郭先生kaka · 2013-04-01 · via 博客园 - 南郭先生kaka

     最近在探索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 found
	at 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