WELCOME Abdennour : Software engineer

Apr 5, 2012

AOP Spring with practices(PART4):Defining an aspect Using XML Configuration

  In  previous tutos, we have seen the main concepts of AOP.
It's time to invade the domain of AOP with practices...
After installing maven ,follow these steps :
1-Execute this command:


$ mvn archetype:generate -DgroupId=slm.Abdennour.springaop -DartifactId=SpringExampleAOP -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false


2-a folder will be created , its name is ${DartifactId}.
$ cd ./SpringExampleAOP/

3-then ,open the file "pom.xml" and replace <dependencies> .... </dependencies> by:
----------------------------------------------------------------

 <properties>
        <spring.version>3.0.5.RELEASE</spring.version>
        <spring.group>org.springframework</spring.group>
    </properties>
  <dependencies>
    <dependency>
      <groupId>${spring.group}</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>${spring.group}</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>${spring.group}</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>${spring.version}</version>
    </dependency>
    
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.6.2</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.6.2</version>
    </dependency>


    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
    </dependency>
    
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.6</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
-------------------
4-Then, Execute this Command :
$ mvn eclipse:eclipse


5-Open this Project by NetBeans :
6-Create the Class which will be transformed to Aspect:
package slm.Abdennour.springaop;



7- It’s a basic Java class . And we can register it as a bean in the Spring application context like any other class:
   <bean id="competition" class="slm.Abdennour.springaop.Competition" />

* Competition Class has all the makings of an aspect:
8-Using Spring’s AOP configuration elements,you can turn the competition bean into an aspect:
 <aop:config>
      <aop:aspect ref="competition" >
          <aop:before pointcut="execution(* slm.Abdennour.springaop.Performance.perform(..))"             method="enterToRing" />
          <aop:before pointcut="execution(* slm.Abdennour.springaop.Performance.perform(..))" method="preparedHimself" />
          <aop:after-returning pointcut="execution(* slm.Abdennour.springaop.Performance.perform(..))" method="wrestling" />
          <aop:after-throwing pointcut="execution(* slm.Abdennour.springaop.Performance.perform(..))" method="surrender" />
       </aop:aspect>   
    
    </aop:config>



=> when it comes to declaring beans as aspects you’ll alwaysstart with the <aop:config> element.
* We say that the Aspect competition has 4 different  bits  of  advice. 
=>2 <aop:before> define "method before advice"
=> The  <aop:after-returning>  element  defines an after-returning advice
=>the <aop:after-throwing> element defines an after-throwing advice .

* the advice is being applied to the same pointcut(execution(* slm.Abdennour.springaop.Performance.perform(..))

9-Now, We will change the Code to use pointcut-ref which refer to pointcut's bean.
----------------------------------
<aop:config>
      <aop:aspect ref="competition" >
          <aop:pointcut id="mypointcut" expression="execution(* slm.Abdennour.springaop.Performance.perform(..))" />
          <aop:before pointcut-ref="mypointcut" method="enterToRing" />
          <aop:before pointcut-ref="mypointcut" method="preparedHimself" />
          <aop:after-returning pointcut-ref="mypointcut" method="wrestling" />
          <aop:after-throwing pointcut-ref="mypointcut" method="surrender" />
       </aop:aspect>   
    
    </aop:config>
----------------------------------











2 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. you're right. You can view this new tutorial. This tutorial that I started writing, it may be simple and understandable.
    http://abdennour-insat.blogspot.com/2012/04/mongodb-helper-hibernate-for-mongodb.html

    ReplyDelete