2012-04-23 61 views
0

當我嘗試訪問下列方式說明:異常線程「main」 org.springframework.beans.factory.BeanNotOfRequiredTypeException:

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.ja VA

 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:豆命名爲「豆'必須是[com.demo.FactoryMethodDemo]類型,但實際上是類型[java.lang.Object]

顯然返回的是對象類型而不是FactoryMethodDemo,請解釋某人在場景後面發生了什麼情況以導致此類異常。 感謝您抽出時間&的幫助提前

+0

更新後與BeanLifeCycleDemo.java&FactoryMethodDemo.java碼 – avinash 2012-04-23 12:30:05

+0

它歸結爲BeanPostProcessor的... BeanLifeCycle類BeanPostProcessor的實現,這是造成異常,我在postProcessAfterInitialization返回新的對象() (...)&postProcessBeforeInitialization(...)返回arg0解決了這個問題。我將詳細閱讀關於BeanPostProcessor ...到那個時候如果有人可以解釋它,那麼這將有助於面臨同樣的問題和登陸這個職位上的人...預先感謝 – avinash 2012-04-23 13:36:21

回答

1

我懷疑com.demo.FactoryMethodDemo將實施FactoryBean接口:

這意味着,在爲FactoryMethodDemo bean的應用程序上下文查找bean將無法使用負責創建另一個bean(ID爲bean)。

如果你看看FactoryMethodDemo實施你最有可能看到有一種getObject的實現將返回豆,將提供與ID bean的一個實例。

由於「工廠bean」與普通bean不同,因此無法使用getBean(String beanId, Class beanType)方法查找它,因爲它不會被註冊。您應該能夠執行以下操作:

context.getBean("&bean"); 

這將返回創建bean「bean」的「工廠」。

又見Spring Reference Docs

+0

你好beny23,FactoryMethodDemo。類只是一個普通的豆抱歉的誤導類名,我正在試圖學習如何在彈簧中實現工廠方法,因此創建了具有給定名稱的類,但正如我遇到的例外我剝奪了一切,並有類作爲一個簡單bean,但仍然有相同的異常。我已經用兩個java類更新了這篇文章。我認爲它與實現所有生命週期接口的BeanLifeCycle類相似。如果我剝離BeanLifeCycle的所有接口實現,代碼將正常運行。 – avinash 2012-04-23 12:37:35

相關問題