我有一个上下文文件如下:
<?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();
}
}
特殊对象.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)