Reputation: 6269
There is an issue using BeanPostProcessor. The error log says the method josh() in productFactory class is missing but the class does have such a method and it is non-static. Below are the code snippets.
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;
}
}
Error log
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.
Somehow the line <bean class="demoproject.productPostProcessor" />
in web.xml is causing this problem since when I remove it everyting works normally.
How can I debug this program and fix it?
Upvotes: 0
Views: 1739
Reputation: 190
As your code , I think you are trying to initalize the Product object using the factory method.If I am right , then first thing is that your method should be static and second thing that method should be specified in the bean definition,like below
<bean name="ProductFactory" class="demoproject.ProductFactory" factory-method="joshs"/>
Upvotes: 0
Reputation: 77177
You're returning the name of the bean. The postProcess
methods are supposed to return the actual bean itself. You're telling Spring to replace the Product
bean with a String
containing the name.
Upvotes: 2