1

It seems that I searched for information from various sources over google to solve my problem, but I am still in trouble. To clarify my problem I will give you as much information as I can.

I am using NetBeans IDE 7.3.1 and Spring 3.1.1.

What I have now:

I created 6 classes and 1 xml file.

These classes are working fine (because these classes are based from book which I am currently reading).

I suppose something is wrong with my jar files (maybe somthing is wrong with versions compatibility or something (idk, i am new in this scope)).

XML FILE:

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

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

        </bean>

        <bean id="quest"
              class="castle.SlayedDragonQuest"/>      

        <bean id="mistreal"
              class="castle.Mistreal"/>
        <aop:config>
            <aop:aspect ref="mistreal">
                <aop:pointcut id="embark"
                    expression="execution(* *.embarkQuest(..))" />
                <aop:before pointcut-ref="embark"
                        method="singBeforeQuest"/>
                <aop:after pointcut-ref="embark"
                    method="signAfterQuest"/>


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

 </beans>

After compiling my code, I am getting this:

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [knight.xml]; nested exception is java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice

It is obvious that classloader cannot find my aop class which is called Advice. But why?

I added jars to my project like this:

enter image description here

Did I miss something? Can anyone give me some information. (I read related resources but result is the same).

Thank you

EDITED:

I added aopalliance.jar file, now I am getting this:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'knight' defined in class path resource [knight.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#1': Cannot create inner bean '(inner bean)' of type [org.springframework.aop.aspectj.AspectJAfterAdvice] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#2': Cannot create inner bean '(inner bean)' 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)#2': Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Unable to locate method [signAfterQuest] on bean [mistreal] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:452) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)

It seems that one solution rises another problem. How can I fix it? P.s. I am not using a Marven. And also this is a mistake in my picture, i fixed it in my project, but it didn't change anything.

Mistreal class:

    package castle;


public class Mistreal {

    public void singBeforeQuest(){
        System.out.println("Singing before quest");
    }

    public void singAfterQuest(){
        System.out.println("Singing after quest");
    }

}
solvator
  • 371
  • 1
  • 6
  • 12

2 Answers2

0

Yes, you need the aopalliance library which contains, among others, the org.aopalliance.aop.Advice class. You can get it for maven (or download the jar), here.

If you are using Maven, then spring-aop should bring it in, since it is one of its dependencies. If not, you will have to download it (and anything else) and add it (them) manually to your classpath/build path.

As others have noted, use a single spring-aop jar.

You have a typo

<aop:after pointcut-ref="embark"
                method="signAfterQuest"/>

In the above pointcut, you use signAfterQuest but your method is called

public void singAfterQuest(){
    System.out.println("Singing after quest");
}

singAfterQuest. The hint is in the logs

Unable to locate method [signAfterQuest] on bean [mistreal] at 

For the new error, Spring, by default, uses JDK proxies, but those only work on interfaces. You therefore cannot inject a Proxy into a variable of a class type. Instead you will need to use CGLIB proxies with this change to your XML configuration

<aop:config proxy-target-class="true">

This will also require you add CGLIB and related libraries to your classpath. More details in related answers like here.

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Omg, what a small mistake. I hate those little mistakes which are almost invisible to human eyes, thank you. After this i got: Exception in thread "main" java.lang.ClassCastException: castle.$Proxy0 cannot be cast to castle.BraveKnight – solvator Dec 04 '13 at 18:42
  • @user You need to start helping yourself. See my edit for how to fix it. – Sotirios Delimanolis Dec 04 '13 at 18:46
  • I will accept this answer, because problem with AOP solved (I Guess), but now there is an exception above. Can you help me figure it out? After this i will go like a winner ^^ – solvator Dec 04 '13 at 18:46
0

I notice that you have both:

spring-aop-3.0.7.RELEASE.jar and spring-aop-3.0.0.RELEASE.jar

Is this intended? Maybe there is some sort of conflict.

Edit: as @SotiriosDelimanolis pointed out, you need the aopalliance jar located here. Maven is definitely a good resource for pulling in required dependencies, and easy to use. I would recommend looking into it.

Paul Gray
  • 546
  • 1
  • 5
  • 10
  • Sorry for this, I fixed it and deleted one of these. I left only 3.0.0 version – solvator Dec 04 '13 at 18:23
  • I do not know how to use Marven, because today is my first day I am using Spring through Spring In Action book. And i faced with problem using AOP approach. And now, I spend over 3 hours for finding the problem. – solvator Dec 04 '13 at 18:28