當我嘗試訪問下列方式說明:異常線程「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,請解釋某人在場景後面發生了什麼情況以導致此類異常。 感謝您抽出時間&的幫助提前
更新後與BeanLifeCycleDemo.java&FactoryMethodDemo.java碼 – avinash 2012-04-23 12:30:05
它歸結爲BeanPostProcessor的... BeanLifeCycle類BeanPostProcessor的實現,這是造成異常,我在postProcessAfterInitialization返回新的對象() (...)&postProcessBeforeInitialization(...)返回arg0解決了這個問題。我將詳細閱讀關於BeanPostProcessor ...到那個時候如果有人可以解釋它,那麼這將有助於面臨同樣的問題和登陸這個職位上的人...預先感謝 – avinash 2012-04-23 13:36:21