我有一个上下文文件如下:
<?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-3.0.xsd">
<bean id="aFactoryBean" class="com.kilo.SpecialFactoryBean"
factory-method="createInstance">
</bean>
</beans>
使用Spring v 3.1.1.RELEASE加载上下文失败时出现异常Exception in thread "main" org.springframework.beans.factory.BeanIsNotAFactoryException: Bean named 'aFactoryBean' must be of type [org.springframework.beans.factory.FactoryBean], but was actually of type [com.kilo.SpecialObject]
,而v 3.1.0.RELEASE同样正常。以为我可能会在这里询问我是否遗漏了配置此内容的明显错误,然后再将其声明为错误:)
SpecialFactoryBean.java
package com.kilo;
import org.springframework.beans.factory.FactoryBean;
public class SpecialFactoryBean implements FactoryBean<SpecialObject> {
private static SpecialObject ourInstance;
@Override
public SpecialObject getObject() throws Exception {
return ourInstance;
}
@Override
public Class<?> getObjectType() {
return SpecialObject.class;
}
@Override
public boolean isSingleton() {
return true;
}
public static SpecialObject createInstance() {
if (ourInstance == null) {
init();
}
return ourInstance;
}
private static void init() {
ourInstance = new SpecialObject();
}
}
SpecialObject.java
package com.kilo;
import org.apache.log4j.Logger;
public class SpecialObject {
private static final Logger LOG = Logger.getLogger(SpecialObject.class);
public void doSomething() {
LOG.info("Did something special");
}
}
SpringFBDriver.java
package com.kilo;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringFBDriver {
private static final Logger LOG = Logger.getLogger(SpringFBDriver.class);
private static ApplicationContext applicationContext;
public static void main(String[] args) {
applicationContext = new ClassPathXmlApplicationContext(
"classpath:com/kilo/applicationContext.xml");
LOG.info("Inited");
doSomething();
}
private static void doSomething() {
SpecialObject specialObject = applicationContext
.getBean(SpecialObject.class);
specialObject.doSomething();
}
}
Exception in thread "main" org.springframework.beans.factory.BeanIsNotAFactoryException: Bean named 'aFactoryBean' must be of type [org.springframework.beans.factory.FactoryBean], but was actually of type [com.kilo.SpecialObject]
at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1420)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:305)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.kilo.SpringFBDriver.main(SpringFBDriver.java:15)
答案 0 :(得分:2)
我认为您将工厂bean 和工厂豆混淆为指定in the documentation.
在Spring文档中,工厂bean引用的是bean 在Spring容器中配置,将通过一个创建对象 实例或静态工厂方法。相比之下,FactoryBean(通知 大写)指的是Spring特定的FactoryBean。
您的类已经是 FactoryBean ,因此您无需指定它为工厂bean保留的方法factory-method =“createInstance” - 可以创建简单对象的普通旧bean,而不是集成到bean定义的FactoryBeans
。