-1

I am leargnin AOP in Spring Framework studying on this tutorial: http://www.tutorialspoint.com/spring/schema_based_aop_appoach.htm

Differently from the previous tutorial I am not adding manually the needed jars file but I am using Maven.

Initially I have added this dependencies in my pom.xml (in addition to those relating spring-core, spring-bean, spring-context, spring-context-support Spring modules)

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>3.1.1.RELEASE</version>
    </dependency>

But, in this way don't work and raises me the following exception:

Caused by: java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException

Reading online I have found the solution: I have to add these two dependencies in my pom.xml:

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjtools</artifactId>
        <version>1.6.2</version>
    </dependency>

    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>2.2</version>
    </dependency>

So now I have two doubt:

  1. Why I have to add this org.aspectj.aspectjtools dependencies if I have yet org.springframework.spring-aop? (Also...I noticed that I could delete the org.springframework.spring-aop, this is not used) What is the difference between them?

  2. Why I have to add cglib dependecies? I know that I have to use cglib when I use annotations like @Configuration and @Bean...but why I need this dependencies in this case that have not these annotation?

Tnx Andrea

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596

1 Answers1

0

You could have used aopalliance instead of cglib, and aspectjrt & aspectjweaver dependencies in your pom.xml. What I use and suggest is compile team weaving of your target code using Maven aspectj-maven-plugin.

As to your questions, Spring AOP does not itself instrument your target code. It uses AspectJ behind the scenes to do that. CGLib is used to generate Dynamic Proxies etc. If you need to dynamically generate an interface and then apply it to your advices then, CGLib can assist. Hibernate heavily uses CGLib

Horizon
  • 548
  • 3
  • 7