2017-02-22 8 views
0

Я не могу запустить приложение hello world с Spring 4.3.6 Пожалуйста, помогите, я даже не знаю, что может быть неправильным. Без боба messageService и aop раздел все работает. Но я получил эту ошибку с конфигурацией, как этоSpring BeanPostProcessor BeanCreationException ошибка времени выполнения

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 конфигурация:

<?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

java.lang.ClassNotFoundException: MessagePrinterService

Spring не может загрузить класс MessagePrinterService ,

Поместите его в определенный пакет и соответствующим образом измените конфигурацию. Тогда он должен работать.

+0

Это в пакете со всеми другими классами. И как должна выглядеть конфигурация? – krund

+0

В вашей конфигурации у вас есть следующая строка , которая не рассматривает какой-либо пакет, он должен быть class = "helloworld.MessagePrinterService" – galovics

+0

Спасибо за ответ. Теперь у меня есть другая ошибка «Исключение в потоке» main »org.springframework.beans.factory.NoSuchBeanDefinitionException: не определен уникальный бит типа [helloword.MessagePrinter]: ожидаемый одиночный бит, но найден 0:« in line »context.getBean (...) в основном методе в классе Application ". Бин-принтер присутствует в контексте, но его тип «com.sun.proxy. $ Proxy». Как это исправить? – krund

Смежные вопросы