Java 使用BeanPostProcessor时在Spring中获取BeanNotOfRequiredTypeException

Java 使用BeanPostProcessor时在Spring中获取BeanNotOfRequiredTypeException,java,spring,Java,Spring,我正在尝试使用BeanPostProcessor运行一个Spring示例 下面是bean后处理器 public class DisplayNamePostProcessor implements BeanPostProcessor{ DisplayNamePostProcessor(){ System.out.println("DisplayNamePostProcessor instantiated"); } public Object postPr

我正在尝试使用
BeanPostProcessor
运行一个Spring示例

下面是bean后处理器

public class DisplayNamePostProcessor implements BeanPostProcessor{

    DisplayNamePostProcessor(){
        System.out.println("DisplayNamePostProcessor instantiated");
    }

    public Object postProcessBeforeInitialization(Object bean, String beanName)
            throws BeansException {
        System.out.println("postProcessBeforeInitialization for bean "+beanName);

        return this;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName)
            throws BeansException {
        System.out.println("postProcessAfterInitialization for bean "+beanName);
        return this;
    }

}
这里是spring配置文件

    <bean id="car" class="com.core.Car" >
            <property name="wheel" value="four" />
        </bean>
<bean class="com.core.DisplayNamePostProcessor"></bean>
在运行上述主方法时,我得到以下异常

Exception in thread "main" org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'car' must be of type [com.core.Car], but was actually of type [com.core.DisplayNamePostProcessor]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:361)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1121)
有人能告诉我我做错了什么以及如何解决这个异常吗。另外,它的根本原因是什么?

您声明的任何bean都将被
ApplicationContext
bean工厂提取并使用。您的实现可以做到这一点

public Object postProcessBeforeInitialization(Object bean, String beanName)
        throws BeansException {
    System.out.println("postProcessBeforeInitialization for bean "+beanName);

    return this;
}

它没有对目标
bean
执行任何操作,而是简单地返回自身。因此,它使用
DisplayNamePostProcessor
bean覆盖它处理的所有bean。

ohh..我的错..谢谢你让我知道。我怎么会错过呢:(
public Object postProcessBeforeInitialization(Object bean, String beanName)
        throws BeansException {
    System.out.println("postProcessBeforeInitialization for bean "+beanName);

    return this;
}