当我尝试以下列方式访问 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,请有人解释导致此类异常的幕后发生的事情。提前感谢您的时间和帮助