9

我正在使用注释动态创建一个 Bean,我收到以下错误。

package demoproject;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ProductFactory {

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

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

web.xml

 <!-- Bean for JavaConfig -->
   <bean  class='demoproject.ProductFactory' />
   <!-- be sure to include the JavaConfig bean post-processor -->
   <bean class="org.springframework.config.java.process.ConfigurationPostProcessor"/>

错误日志

Exception in thread "main" org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.config.java.process.ConfigurationPostProcessor] for bean with name 'org.springframework.config.java.process.ConfigurationPostProcessor#0' defined in class path resource [web.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.config.java.process.ConfigurationPostProcessor
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1275)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:575)
    at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1344)
    at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:910)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at demoproject.ProductCreater.main(ProductCreater.java:31)
Caused by: java.lang.ClassNotFoundException: org.springframework.config.java.process.ConfigurationPostProcessor
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at org.springframework.util.ClassUtils.forName(ClassUtils.java:260)
    at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:416)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1296)
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1267)
    ... 9 more

我怎么知道是什么导致了这个错误,有什么我可以尝试的解决方法吗?

4

2 回答 2

19

从 spring 3.0 开始,您不再需要org.springframework.config.java.process.ConfigurationPostProcessor,只需将 Java-config 类定义为 XML-config 中的 bean。

<bean class="demoproject.ProductFactory" />

尝试删除

<bean class="org.springframework.config.java.process.ConfigurationPostProcessor"/>
于 2013-08-09T07:42:28.453 回答
0

spring-core的运行时类路径中没有。您需要使用适合您项目布局的技术来添加它,例如像 Simon 建议的那样将其添加到 Maven POM。

于 2013-08-09T06:17:48.600 回答