gpt4 book ai didi

java - BeanPostProcessor 的问题

转载 作者:行者123 更新时间:2023-12-02 06:48:58 24 4
gpt4 key购买 nike

使用 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.

不知怎的,这条线<bean class="demoproject.productPostProcessor" /> web.xml 中的内容导致了这个问题,因为当我删除它时,一切正常。我该如何调试这个程序并修复它?

最佳答案

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

关于java - BeanPostProcessor 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18266905/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com