Announcement Announcement Module
Collapse
No announcement yet.
Help required in AOP Programming Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help required in AOP Programming

    I am a newbie in Spring and trying to learn. I am using the Spring in Action 3rd edition as starting point. I implemented the following example.

    Knight.java
    ========
    package com.springinaction.knights;
    public abstract class Knight {
    public abstract void embarkQuest() ;
    }
    DragonSlayingKnights.java
    ===================
    package com.springinaction.knights;
    public class DragonSlayingKnights extends Knight {
    Quest quest;
    public DragonSlayingKnights(Quest quest){
    this.quest = quest ;
    }
    @Override
    public void embarkQuest() {
    quest.embark();
    }
    }
    FightingKnight.java
    ==============
    package com.springinaction.knights;
    public class FightingKnight extends Knight {
    Quest quest ;
    public FightingKnight(Quest quest){
    this.quest = quest;
    }
    @Override
    public void embarkQuest() {
    quest.embark();
    }
    }
    Quest.java
    ========
    package com.springinaction.knights;
    public abstract class Quest {
    public abstract void embark();
    }
    DragonSlayingQuest.java
    ===================
    package com.springinaction.knights;
    public class DragonSlayingQuest extends Quest {
    @Override
    public void embark() {
    System.out.println("I am on a Dragon Slaying Quest");
    }
    }
    FightingQuest.java
    ==============
    package com.springinaction.knights;
    public class FightingQuest extends Quest {
    @Override
    public void embark() {
    System.out.println("I am on a Fighting Quest");
    }
    }
    Minstrel.java
    =========
    package com.springinaction.knights;
    public class Minstrel {
    public void singBeforeQuest(){
    System.out.println("Falala;Theknightissobrave!");
    }

    public void singAfterQuest(){
    System.out.println("Tee heehe;Thebraveknightdidembarkonaquest!");
    }
    }
    configuration.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/schem...-beans-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    <bean id="knight" class="com.springinaction.knights.DragonSlayingKni ghts">
    <constructor-arg ref="quest"/>
    </bean>
    <bean id="quest" class="com.springinaction.knights.DragonSlayingQue st"/>
    <bean id="minstrel" class="com.springinaction.knights.Minstrel"/>
    <aop:config>
    <aop:aspect ref="minstrel">
    <aopointcut id="embark" expression="execution(* com.springinaction.knights.Knight.embarkQuest(..)) " />
    <aop:before pointcut-ref="embark" method="singBeforeQuest"/>
    <aop:after pointcut-ref="embark" method="singAfterQuest"/>
    </aop:aspect>
    </aop:config>
    </beans>

    Test File - TestKnights.java
    =====================
    package com.test.spring;

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlAp plicationContext;
    import com.springinaction.knights.Knight;

    public class TestKnights {
    public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("configuration.xml" );
    Knight knight = (Knight) context.getBean("knight");
    knight.embarkQuest();
    }
    }

    When I execute the TestKnights.java, I get the following error:

    Nov 5, 2011 2:31:09 AM org.springframework.context.support.AbstractApplic ationContext prepareRefresh
    INFO: Refreshing org.springframework.context.support.ClassPathXmlAp plicationContext@1f5a3d6b: startup date [Sat Nov 05 02:31:09 PDT 2011]; root of context hierarchy
    Nov 5, 2011 2:31:09 AM org.springframework.beans.factory.xml.XmlBeanDefin itionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from class path resource [configuration.xml]
    Nov 5, 2011 2:31:09 AM org.springframework.beans.factory.support.DefaultL istableBeanFactory preInstantiateSingletons
    INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultL istableBeanFactory@6da28362: defining beans [knight,quest,minstrel,org.springframework.aop.conf ig.internalAutoProxyCreator,org.springframework.ao p.aspectj.AspectJPointcutAdvisor#0,org.springframe work.aop.aspectj.AspectJPointcutAdvisor#1,embark]; root of factory hierarchy
    Nov 5, 2011 2:31:09 AM org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry destroySingletons
    INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultL istableBeanFactory@6da28362: defining beans [knight,quest,minstrel,org.springframework.aop.conf ig.internalAutoProxyCreator,org.springframework.ao p.aspectj.AspectJPointcutAdvisor#0,org.springframe work.aop.aspectj.AspectJPointcutAdvisor#1,embark]; root of factory hierarchy
    Exception in thread "main" org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'knight' defined in class path resource [configuration.xml]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAd visor#0': Cannot create inner bean '(inner bean)' of type [org.springframework.aop.aspectj.AspectJMethodBefor eAdvice] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name '(inner bean)': Cannot resolve reference to bean 'embark' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'embark': Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException
    .........

    The complete error is available in the attached file.

    I am not able to fix this error. Can somebody help me? Thanks in advance.

  • #2
    Please use [ code][/code ] tags when posting code, that way it remains readable.

    Next read the stack trace... which quite clearly indicates which class you are missing and with that you can find the jar you need to include.

    Code:
    java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException

    Comment

    Working...
    X