BeanPostProcessor的问题

时间:2013-08-16 06:12:58

标签: java spring

使用BeanPostProcessor时出现问题。错误日志表示缺少productFactory类中的方法josh(),但该类确实有这样的方法,并且它是非静态的。以下是代码段。

的web.xml

<bean class="demoproject.productPostProcessor" /> 
    <context:annotation-config />
<context:component-scan base-package="demoproject" />
<bean name="ProductFactory" class="demoproject.ProductFactory" />

ProductFactory.java

public class ProductCreater{
  public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("web.xml");

        Product copyProduct = (Product) context.getBean("joshs");
        System.out.println(copyProduct.getId());
        System.out.println(copyProduct.getPrice());
   }
}

ProductFactory.java

package demoproject;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

@Configuration
public class ProductFactory {


    public ProductFactory() {
        // TODO Auto-generated constructor stub
    }

    @Bean(name="joshs") 
    public Product josh(){
        Product josh = new Battery();
        josh.setId("cdrw");
        return josh;
    }


}

productPostProcessor.java

package demoproject;

import org.springframework.beans.factory.config.BeanPostProcessor;

public class productPostProcessor  implements BeanPostProcessor{

    public Object postProcessBeforeInitialization(Object bean, String beanName){
        System.out.println("Before initializing .. : "+beanName);
        return beanName;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName){
        System.out.println("After initializing .. : "+beanName);
        return beanName;

    }   
}

错误日志

INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7b09df06: defining beans [demoproject.productPostProcessor#0,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,productFactory,ProductFactory,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,joshs]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'joshs' defined in class path resource [demoproject/ProductFactory.class]: No matching factory method found: factory bean 'ProductFactory'; factory method 'josh()'. Check that a method with the specified name exists and that it is non-static.

某种程度上,web.xml中的行<bean class="demoproject.productPostProcessor" />导致了这个问题,因为当我删除它时,每个工作正常。 我该如何调试该程序并修复它?

2 个答案:

答案 0 :(得分:2)

您将返回bean的名称。 postProcess方法应该返回实际的bean本身。您告诉Spring将Product bean替换为包含名称的String

答案 1 :(得分:0)

作为您的代码,我认为您正在尝试使用工厂方法初始化Product对象。如果我是对的,那么首先应该是您的方法应该是静态的,并且应该在bean定义中指定该方法的第二件事,如下所示

<bean name="ProductFactory" class="demoproject.ProductFactory" factory-method="joshs"/>