1

我有一个上下文文件如下:

<?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)
4

1 回答 1

2

我认为您混淆了文档中指定的工厂 bean工厂 Bean 。

在 Spring 文档中,工厂 bean 是指在 Spring 容器中配置的 bean,它将通过实例或静态工厂方法创建对象。相比之下, FactoryBean (注意大写)指的是 Spring 特定的 FactoryBean 。

您的类已经是FactoryBean,因此您无需指定它为工厂 bean 保留的方法 factory-method="createInstance" - 可以创建简单对象的普通旧 bean,而不是FactoryBeans集成到 bean 定义中的那些。

于 2012-11-11T18:53:45.597 回答