2013-08-20 6 views
7

Jestem nowy w Spring Framework, więc postanowiłem kupić książkę ("Spring in action", wydanie trzecie). Obecnie jestem na pierwszym rozdziale, który obejmuje podstawy - Dependency Injection (DI) i programowanie zorientowane na aspekt (AOP).SpringFramework: wyjątek tworzenia instancji

Udało mi się uruchomić ten kod, który pokazał DI.

1 # Interfejs realizowane przez rycerza

package com.springinaction.knights; 

public interface Knight { 
    void embarkOnQuest() throws QuestException; 
} 

1 # klasa rycerz

package com.springinaction.knights; 

public class BraveKnight implements Knight { 
    private Quest quest; 

    public BraveKnight(Quest quest) { 
    this.quest = quest;  //<co id="co_injectedQuest"/> 
    } 

    public void embarkOnQuest() throws QuestException { 
    quest.embark(); 
    } 
} 

# 2Quest interfejs

package com.springinaction.knights; 

public interface Quest { 
    void embark() throws QuestException; 
} 

# 2Quest Wyjątek

package com.springinaction.knights; 

public class QuestException extends RuntimeException { 
    private static final long serialVersionUID = 1L; 
} 

# 2Quest realizacja klasa typ

package com.springinaction.knights; 

public class SlayDragonQuest implements Quest { 

    public void embark() throws QuestException { 
    System.out.println("Slaying Dragon!"); 
    } 

} 

# 3Testing dzielny rycerz DI

package com.springinaction.knights; 

import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class KnightMain { 
    public static void main(String[] args) { 
    ApplicationContext context = 
     new ClassPathXmlApplicationContext("knights.xml"); //<co id="co_loadKnightContext"/> 

    Knight knight = (Knight) context.getBean("knight"); //<co id="co_getBeanKnight"/> 

    knight.embarkOnQuest();//<co id="co_useKnight"/> 
    } 
} 

To jest kontekst aplikacji XML, który wstrzykuje dążenie do rycerza

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> 

    <bean id="quest" class="com.springinaction.knights.SlayDragonQuest" /><!--<co 
     id="co_quest_bean"/> --> 

    <bean id="knight" class="com.springinaction.knights.BraveKnight"> 
     <constructor-arg ref="quest" /> <!--<co id="co_inject_quest_bean"/> --> 
    </bean> 

</beans> 

do tej chwili wszystko działa, ale gdy autor chce pokazać AOP to coś jest nie tak w pliku XML. Chodzi o to, że minstrel śpiewa (loguje), zanim rycerz rozpocznie misję i śpiewa (wylogowuje się), gdy rycerz wraca z zadania.

klasa Minstrel

package com.springinaction.knights; 

public class Minstrel { 
    public void singBeforeQuest() {  //<co id="co_singBefore"/> 
    System.out.println("Fa la la; The knight is so brave!"); 
    } 

    public void singAfterQuest() {  //<co id="co_singAfter"/> 
    System.out.println(
      "Tee hee he; The brave knight did embark on a quest!"); 
    } 
} 

plik XML AOP, myślę, że istnieje jakiś rodzaj błędu tutaj, ale nie wiem co ...

<?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-3.2.xsd 
     http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> 

    <bean id="knight" class="com.springinaction.knights.BraveKnight"> 
    <constructor-arg ref="quest" />  
    </bean> 

    <bean id="quest" 
     class="com.springinaction.knights.SlayDragonQuest" /> 

    <bean id="minstrel" 
    class="com.springinaction.knights.Minstrel" /> <!--<co id="co_minstrel_bean"/>--> 

    <aop:config> 
    <aop:aspect ref="minstrel"> 

     <aop:pointcut id="embark" 
      expression="execution(* *.embarkOnQuest(..))" /> <!--<co id="co_define_pointcut"/>--> 

     <aop:before pointcut-ref="embark" 
        method="singBeforeQuest"/> <!--<co id="co_minstrel_before_advice"/>--> 

     <aop:after pointcut-ref="embark" 
       method="singAfterQuest"/>  <!--<co id="co_minstrel_after_advice"/>--> 

    </aop:aspect> 
    </aop:config> 
</beans> 

Testowanie tego wygląda dokładnie tak samo, jak w przypadku testu nr 3 Test BraveKnight

Wyjątek, który jest bardzo długi.

sie 20, 2013 3:06:58 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh 
INFO: Refreshing org[email protected]14d5bc9: startup date [Tue Aug 20 15:06:58 CEST 2013]; root of context hierarchy 
sie 20, 2013 3:06:58 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 
INFO: Loading XML bean definitions from class path resource [knights-aop.xml] 
sie 20, 2013 3:06:58 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 
INFO: Pre-instantiating singletons in org.s[email protected]46aa61: defining beans [knight,quest,minstrel,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,org.springframework.aop.aspectj.AspectJPointcutAdvisor#1,embark]; root of factory hierarchy 
sie 20, 2013 3:06:58 PM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons 
INFO: Destroying singletons in org.s[email protected]46aa61: defining beans [knight,quest,minstrel,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0,org.springframework.aop.aspectj.AspectJPointcutAdvisor#1,embark]; root of factory hierarchy 
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'knight' defined in class path resource [knights-aop.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)' 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)': Cannot resolve reference to bean 'embark' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'embark': Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/BCException 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:454) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626) 
    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 com.springinaction.knights.KnightAopMain.main(KnightAopMain.java:10) 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0': Cannot create inner bean '(inner bean)' 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)': Cannot resolve reference to bean 'embark' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'embark': Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/BCException 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:282) 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:126) 
    at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:629) 
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:148) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1045) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:949) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:487) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198) 
    at org.springframework.aop.framework.autoproxy.BeanFactoryAdvisorRetrievalHelper.findAdvisorBeans(BeanFactoryAdvisorRetrievalHelper.java:86) 
    at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findCandidateAdvisors(AbstractAdvisorAutoProxyCreator.java:101) 
    at org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator.shouldSkip(AspectJAwareAdvisorAutoProxyCreator.java:103) 
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessBeforeInstantiation(AbstractAutoProxyCreator.java:276) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:890) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:862) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:448) 
    ... 10 more 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)': Cannot resolve reference to bean 'embark' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'embark': Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/BCException 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:329) 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107) 
    at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:615) 
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:148) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1045) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:949) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:487) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:271) 
    ... 28 more 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'embark': Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/BCException 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1007) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:953) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:487) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:314) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:323) 
    ... 36 more 
Caused by: java.lang.NoClassDefFoundError: org/aspectj/weaver/BCException 
    at java.lang.Class.getDeclaredConstructors0(Native Method) 
    at java.lang.Class.privateGetDeclaredConstructors(Unknown Source) 
    at java.lang.Class.getConstructor0(Unknown Source) 
    at java.lang.Class.getDeclaredConstructor(Unknown Source) 
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1000) 
    ... 42 more 
Caused by: java.lang.ClassNotFoundException: org.aspectj.weaver.BCException 
    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) 
    ... 48 more 

Odpowiedz

18

pobierania AspectJ i dodać aspectjrt.jar & aspectjweaver.jar na ścieżce klas. Spring aop config z aop namespace opiera się na AspectJ, który nie jest już dostarczany w pakiecie ze słojem org.springframework.aop.

Poniżej Maven zależność w przypadku jego projekt

<dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-aop</artifactId> 
      <version>${org.springframework.version}</version> 
</dependency> 
<dependency> 
     <groupId>org.aspectj</groupId> 
     <artifactId>aspectjweaver</artifactId> 
     <version>1.6.1</version> 
</dependency> 
+0

Uhh oparte Maven! Miałem problemy z zaimportowaniem autorskiego projektu do mojego pakietu narzędzi Spring, więc zdecydowałem się zrobić własny projekt z jego plików i zapomniałem o niektórych zależnościach. Stukrotne dzięki! – ashur