0

当我尝试以下列方式访问 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>

它抛出以下异常

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

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

4

1 回答 1

1

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

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

如果您查看实现,FactoryMethodDemo您很可能会看到有一个实现getObject将返回 id 可用的 bean 实例bean

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

context.getBean("&bean");

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

另请参阅Spring 参考文档

于 2012-04-23T11:23:19.470 回答