私は道説明が必要:スレッド「メイン」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();
を以下にBeanにアクセスしてみてください。ここBeanLifeCycle.class BeanNameAwareなどのすべてのライフサイクル・インターフェースを実装しています。 ..DisposableBean等 FactoryMethodDemo.classは単純ビーン
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>
これは、スレッド "メイン" org.springframework.beans.factory.BeanNotOfRequiredTypeExceptionに例外
例外をスローし、以下:Beanの「Beanの命名しました'は型[com.demo.FactoryMethodDemo]である必要がありますが、実際には[java.lang.Object]タイプのものです
明らかに、FactoryMethodDemoの代わりにObject型が返されます。誰かがそのような例外を引き起こすためにシーンの後ろで起こっていることを説明してください。
これは、BeanはFactoryMethodDemo
Beanとアプリケーションコンテキスト内のルックアップのために利用できなくなることを意味する責任がある: は、私はcom.demo.FactoryMethodDemo
がFactoryBean
インターフェイスを実装することを疑う
更新ポスト – avinash
それはBeanLifeCycleクラスは、例外の原因となっているBeanPostProcessorを実施している... BeanPostProcessorつまるところ、私はpostProcessAfterInitialization中)(新しいオブジェクトを返しています(...)&postProcessBeforeInitialization(...)がarg0を返して問題を解決しました。私はBeanPostProcessorについて詳しく説明しています...その時までに誰かがそれを説明できたら、同じ問題に直面してこのポストに着陸する人にとっては助けになるでしょう...あらかじめありがとうございました – avinash