当我尝试以下列方式访问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,有人请解释在场景背后发生的事情导致此类异常。 谢谢你的时间和提前帮助
答案 0 :(得分:1)
我怀疑com.demo.FactoryMethodDemo
将实现FactoryBean
接口:
这意味着bean在应用程序上下文中不可用于查找,因为FactoryMethodDemo
bean负责创建另一个bean(标识为bean
)。
如果你看一下FactoryMethodDemo
的实现,你很可能会发现有一个getObject
的实现,它将返回一个id为{{1}的bean实例。 }。
因为“工厂bean”与普通bean不同,所以您将无法使用bean
方法查找它,因为它不会被注册。但是,您应该能够执行以下操作:
getBean(String beanId, Class beanType)
这将返回创建bean“bean”的“工厂”。