【问题标题】:Explanation required: Exception in thread "main" org.springframework.beans.factory.BeanNotOfRequiredTypeException:需要解释:线程“主”org.springframework.beans.factory.BeanNotOfRequiredTypeException中的异常:
【发布时间】:2012-04-23 11:13:24
【问题描述】:

当我尝试通过以下方式访问 bean 时

ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("factoryMethodDemo.xml");
    System.out.println(context.getBean("bean2",BeanLifeCycle.class));
    System.out.println(context.getBean("bean",FactoryMethodDemo.class));
    context.close();

这里 BeanLifeCycle.class 实现了所有生命周期接口,如 BeanNameAware...DisposableBean 等。 FactoryMethodDemo.class 是一个简单的 bean

FactoryMethodDemo.java

        package com.demo;

    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    public class FactoryMethodDemo {
        private String message;

        public FactoryMethodDemo() {
        }

        public void setMessage(String message) {
            System.out.println("setMessage Called");
            this.message = message;
        }

        public void defaultInit() {
            System.out.println("defaultInit");

        }

        public static void main(String[] args) {
            ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("factoryMethodDemo.xml");
            System.out.println(context.getBean("bean2",BeanLifeCycle.class));
            System.out.println(context.getBean("&bean",FactoryMethodDemo.class));
            context.close();
        }
    }

BeanLifeCycle.java

        package com.demo;

    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.BeanFactoryAware;
    import org.springframework.beans.factory.BeanNameAware;
    import org.springframework.beans.factory.DisposableBean;
    import org.springframework.beans.factory.InitializingBean;
    import org.springframework.beans.factory.config.BeanPostProcessor;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;

    public class BeanLifeCycle implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, BeanPostProcessor, InitializingBean, DisposableBean {

        private String property;

        public void setProperty(String property) {
            System.out.println("setProperty");
        }

        @Override
        public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
            System.out.println("setBeanFactory");
        }

        @Override
        public void setBeanName(String beanName) {
            System.out.println("setBeanName");
        }

        @Override
        public void setApplicationContext(ApplicationContext arg0) throws BeansException {
            System.out.println("setApplicationContext");

        }

        @Override
        public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {
            System.out.println("postProcessAfterInitialization");
            return new Object();
        }

        @Override
        public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException {
            System.out.println("postProcessBeforeInitialization");
            return new Object();
        }

        @Override
        public void afterPropertiesSet() throws Exception {
            // THis is treated as init method
            System.out.println("afterPropertiesSet");
        }

        @Override
        public void destroy() throws Exception {
            System.out.println("destroy");
        }


    }

factoryMethodDemo.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans  xmlns="http://www.springframework.org/schema/beans" x        mlns: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="bean" class="com.demo.FactoryMethodDemo">
    <property name="message" value="message"></property>
    </bean>
    <bean id="bean2" class="com.demo.BeanLifeCycle">
    <property name="property" value="property"></property></bean>
    </beans>

抛出以下异常

线程 "main" org.springframework.beans.factory.BeanNotOfRequiredTypeException 中的异常:名为 'bean' 的 Bean 必须是 [com.demo.FactoryMethodDemo] 类型,但实际上是 [java.lang.Object] 类型

显然返回的是Object类型而不是FactoryMethodDemo,请有人解释一下幕后发生的事情导致这种异常。 提前感谢您的时间和帮助

【问题讨论】:

  • 使用 BeanLifeCycleDemo.java 和 FactoryMethodDemo.java 代码更新帖子
  • 归结为 BeanPostProcessor... BeanLifeCycle 类正在实现导致异常的 BeanPostProcessor,我在 postProcessAfterInitialization(...) 和 postProcessBeforeInitialization(...) 中返回 new Object() 返回 arg0 已解决问题。我将详细阅读 BeanPostProcessor... 到那时,如果有人可以解释它,那么这对面临同样问题并登陆这篇文章的人会有所帮助...提前致谢

标签: spring


【解决方案1】:

我怀疑com.demo.FactoryMethodDemo 会实现FactoryBean 接口:

这意味着 bean 将无法在应用程序上下文中查找,因为 FactoryMethodDemo bean 负责创建另一个 bean(id 为 bean)。

如果您查看FactoryMethodDemo 的实现,您很可能会看到getObject 的实现将返回一个ID 为bean 的bean 实例。

因为“工厂 bean”与普通 bean 不同,您将无法使用 getBean(String beanId, Class beanType) 方法查找它,因为它不会被注册。但是,您应该能够执行以下操作:

context.getBean("&bean");

这将返回创建 bean“bean”的“工厂”。

另见Spring Reference Docs

【讨论】:

  • 您好 beny23,FactoryMethodDemo.class 只是一个普通的 bean,抱歉类名具有误导性,我试图学习如何在 springs 中实现工厂方法,因此创建了具有给定名称的类,但是因为我遇到异常我剥离了所有内容并将该类作为一个简单的 bean,但仍然有相同的异常。我已经用两个 java 类更新了帖子。我认为它与实现所有生命周期接口的 BeanLifeCycle 类有关。如果我从 BeanLifeCycle 中剥离所有接口实现,代码运行正常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-20
  • 2016-07-22
  • 1970-01-01
  • 2013-02-17
相关资源
最近更新 更多