2013-08-20 21 views
7

Spring Framework'e yeni geldim, bu yüzden bir kitap almaya karar verdim ("Spring in action" 3. baskı). Şu anda temelleri kapsayan ilk bölümde - Bağımlılık Enjeksiyonu (DI) ve Aspect yönelimli programlama (AOP).SpringFramework: örnekleme istisnası

DI gösteren bu kodu çalıştırmayı başardım.

1 # Arayüz şövalye

package com.springinaction.knights; 

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

1 # Şövalye sınıfı

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 arayüzü

package com.springinaction.knights; 

public interface Quest { 
    void embark() throws QuestException; 
} 
tarafından uygulanan

# 2Quest İstisna

package com.springinaction.knights; 

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

# 2Quest tipi sınıf uygulaması

package com.springinaction.knights; 

public class SlayDragonQuest implements Quest { 

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

} 

# 3Testing cesur şövalye 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"/> 
    } 
} 

Bu anın göre herşey knight

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

içine arayışı enjektebudur XML uygulama bağlamı çalışır, ancak yazar AOP'yi göstermek istediğinde sonra bir şey XML dosyasındaki yanlıştır. Buradaki fikir şuydu ki, şövalye öncesi şövalye (günlüğü) bir arayışa girer ve şövalye bir görevden döndüğünde şarkı söyler.

Âşık sınıf

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!"); 
    } 
} 

XML AOP dosyası, orada hata çeşit burada, ama ben bilmiyorum sanırım ...

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

Bunu test etmek, # 3 Testing in BraveKnightÇok uzun olan özel durum.

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 

cevap

18

İndir AspectJ ve sınıf için aspectjrt.jar & aspectjweaver.jar ekleyin. Spring'in aop isim alanı ile aop yapılandırması, org.springframework.aop jar ile birlikte paketlenmeyen AspectJ'e dayanır.Takip

onun bir maven tabanlı proje

<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

Ahh durumda maven bağımlılık olduğunu! Yazarlar projemi Spring takım takımına aktarma konusunda problemlerim vardı, bu yüzden kendi projemi kendi dosyalarından yapmaya ve bazı bağımlılıkları unutmaya karar verdim. Bir milyona teşekkürler! – ashur