2017-02-22 56 views
0

我無法使用Spring 4.3.6啓動Hello World應用程序請幫助,我甚至不知道可能會出錯。 沒有豆messageServiceaop部分一切正常。但因爲它是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

春天不能加載類MessagePrinterService 。

將其放入特定的包中並相應地更改配置。它應該工作。

+0

它與所有其他類的包。以及配置應該如何看起來像? – krund

+0

在你的配置中,你有下面一行,它不包含任何包,它應該是class =「helloworld.MessagePrinterService」 – galovics

+0

感謝你的迴應。現在我有不同的錯誤「線程中的異常」main「org.springframework.beans.factory.NoSuchBeanDefinitionException:沒有定義類型爲[helloword.MessagePrinter]的唯一bean:期望單個bean,但找到0:」in line「context.getBean( ...)在類應用程序的主要方法中「。 Bean打印機呈現在上下文中,但它的類型是「com.sun.proxy。$ Proxy」如何解決此問題? – krund