【问题标题】:Spring factorybean error in Spring 3.1.1 but not in 3.1.0Spring 3.1.1 中的 Spring factorybean 错误,但 3.1.0 中没有
【发布时间】:2012-11-09 14:30:19
【问题描述】:

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

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

【问题讨论】:

标签: spring spring-3


【解决方案1】:

我认为您混淆了 factory beanFactory Beans 指定 in the documentation.

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

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

【讨论】:

  • 太棒了@鲍里斯!我猜这解释了它 - 非常感谢。我认为这种二元性一直有效到 3.1.0,但后来得到了修复。将在测试设置中使用它并确认。
  • 创建 jira.springsource.org/browse/SPR-9981 以更明确地通知尝试使用这两种范式的 bean。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-05
  • 1970-01-01
  • 2012-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多