Spring BeanPostProcessor BeanCreationException运行时错误

时间:2017-02-22 11:04:16

标签: spring

我无法用Spring 4.3.6启动你好的世界应用程序请帮助,我甚至不知道可能出错了什么。 没有bean messageServiceaop部分一切正常。但我在配置中遇到了这个错误,因为它是

WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'printer' defined in class path resource [config.xml]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0': Cannot create inner bean '(inner bean)#c273d3' of type [org.springframework.aop.aspectj.AspectJMethodBeforeAdvice] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#c273d3': Cannot create inner bean '(inner bean)#1423665' of type [org.springframework.aop.config.MethodLocatingFactoryBean] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1423665': Initialization of bean failed; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [MessagePrinterService] for bean with name 'messageService' defined in class path resource [config.xml]; nested exception is java.lang.ClassNotFoundException: MessagePrinterService
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [MessagePrinterService] for bean with name 'messageService' defined in class path resource [config.xml]; nested exception is java.lang.ClassNotFoundException: MessagePrinterService
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'printer' defined in class path resource [config.xml]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0': Cannot create inner bean '(inner bean)#c273d3' of type [org.springframework.aop.aspectj.AspectJMethodBeforeAdvice] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#c273d3': Cannot create inner bean '(inner bean)#1423665' of type [org.springframework.aop.config.MethodLocatingFactoryBean] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1423665': Initialization of bean failed; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [MessagePrinterService] for bean with name 'messageService' defined in class path resource [config.xml]; nested exception is java.lang.ClassNotFoundException: MessagePrinterService
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [MessagePrinterService] for bean with name 'messageService' defined in class path resource [config.xml]; nested exception is java.lang.ClassNotFoundException: MessagePrinterService
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:479)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)

Xml config:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd">

   <bean id="printer" class="helloword.MessagePrinter">
      <constructor-arg ref="message"></constructor-arg>
   </bean>

    <bean id="message" class="helloword.HelloWordMessage">
        <constructor-arg value="Hello Spring World!"></constructor-arg>
    </bean>

    <bean id="messageService" class="MessagePrinterService">
    </bean>

    <aop:config>
        <aop:aspect ref="messageService">
            <aop:pointcut id="print" expression="execution(* *.print(..))"/>
            <aop:before pointcut-ref="print" method="preMsg"/>
            <aop:after pointcut-ref="print" method="postMsg"/>
        </aop:aspect>
    </aop:config>

</beans>

类,都是单独的文件:

    public class HelloWordMessage implements Message{

        private String msg;

        public HelloWordMessage(String s) {
            msg = s;
        }

        @Override
        public String getText() {
            return msg;     
        }

    }

public class MessagePrinter implements Printer {
    private Message msg;

    public MessagePrinter(Message m){
        msg = m;

    }

    @Override
    public void print(){
        System.out.println(msg.getText());
    }

}

public class MessagePrinterService {

    public MessagePrinterService(){
        System.out.println("MessagePrinterService created");    
    }

    public void preMsg(){
        System.out.println("Message alert!:");
    }

    public void postMsg(){
        System.out.println("End of message.");
    }
}

public class Aplication {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("config.xml"); 
        MessagePrinter printer = context.getBean(MessagePrinter.class);
        printer.print();
        context.close();

    }

}

1 个答案:

答案 0 :(得分:1)

  

java.lang.ClassNotFoundException:MessagePrinterService

Spring无法加载MessagePrinterService类。

将其放入特定包中并相应地更改配置。它应该工作。