XML Schema base AOP con Spring

0
1488

Per utilizzare i tag namespace AOP descritte in questa sezione, è necessario importare lo schema spring-AOP come descritto di seguito:

<?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 definition & AOP specific configuration -->

</beans>

Sarà inoltre necessario le seguenti librerie AspectJ sul CLASSPATH dell’applicazione. Queste librerie sono disponibili nella directory ‘lib’ di installazione AspectJ, altrimenti è possibile scaricarli da internet.

  • aspectjrt.jar
  • aspectjweaver.jar
  • aspectj.jar
  • aopalliance.jar

Dichiarare un aspetto

Un aspetto è dichiarato utilizzando il tag <aop:aspect>, e il backing bean  utilizzando l’attributo ref  come segue:

<aop:config>
   <aop:aspect id="myAspect" ref="aBean">
   ...
   </aop:aspect>
</aop:config>

<bean id="aBean" class="...">
...
</bean>

Qui il “Bean” sarà configurato e iniettato come qualsiasi altro bean Spring.

Dichiarare un pointcut

Un pointcut contribuisce a determinare i punti di join (vale a dire metodi) di interesse che deve essere eseguiti con diversi advice. Mentre si lavora con la configurazione su XML Schema, pointcut sarà definito come segue:

<aop:config>
   <aop:aspect id="myAspect" ref="aBean">

   <aop:pointcut id="businessService"
      expression="execution(* com.xyz.myapp.service.*.*(..))"/>
   ...
   </aop:aspect>
</aop:config>

<bean id="aBean" class="...">
...
</bean>

L’esempio seguente definisce un pointcut chiamato ‘businessService’ che abbinerà l’esecuzione di getName () metodo disponibile in classe Student sotto il pacchetto com.quickgo:

<aop:config>
   <aop:aspect id="myAspect" ref="aBean">

   <aop:pointcut id="businessService"
      expression="execution(* com.quickgo.Student.getName(..))"/>
   ...
   </aop:aspect>
</aop:config>

<bean id="aBean" class="...">
...
</bean>

Dichiarare consigli

È possibile dichiarare qualsiasi dei cinque advices all’interno di un <aop:aspect> con il tag <aop: {ADVICE NAME}> come indicato di seguito:

<aop:config>
   <aop:aspect id="myAspect" ref="aBean">
      <aop:pointcut id="businessService"
         expression="execution(* com.xyz.myapp.service.*.*(..))"/>

      <!-- a before advice definition -->
      <aop:before pointcut-ref="businessService" 
         method="doRequiredTask"/>

      <!-- an after advice definition -->
      <aop:after pointcut-ref="businessService" 
         method="doRequiredTask"/>

      <!-- an after-returning advice definition -->
      <!--The doRequiredTask method must have parameter named retVal -->
      <aop:after-returning pointcut-ref="businessService"
         returning="retVal"
         method="doRequiredTask"/>

      <!-- an after-throwing advice definition -->
      <!--The doRequiredTask method must have parameter named ex -->
      <aop:after-throwing pointcut-ref="businessService"
         throwing="ex"
         method="doRequiredTask"/>

      <!-- an around advice definition -->
      <aop:around pointcut-ref="businessService" 
         method="doRequiredTask"/>
   ...
   </aop:aspect>
</aop:config>

<bean id="aBean" class="...">
...
</bean>

È possibile utilizzare lo stesso doRequiredTask o metodi diversi per i diversi advices. Questi metodi saranno definiti come una parte del modulo di aspetto.

XML Schema Based AOP Esempio

Per comprendere i concetti sopra citati relativi a XML Schema Based AOP, scriviamo un esempio che attuerà alcuni dei advices. Per scrivere il nostro esempio con alcuni advices, apriamo il nostro IDE Eclipse e seguire la seguente procedura per creare un’applicazione Spring:

Passo Descrizione
1 Creare un progetto con un nome SpringExample e creare un pacchetto com.quickgo sotto cartella src nel progetto creato.
2 Aggiungere librerie Spring richieste utilizzando Aggiungi JAR esterni .
3 Aggiungere Spring AOP librerie specifiche aspectjrt.jar, aspectjweaver.jar e aspectj.jar nel progetto.
4 Creare classi Java registrazione , studenti e MainApp sotto il pacchetto com.quickgo.
5 Creare file di  configurazione Bean di beans.xml sotto la cartella src.
6 Il passo finale è quello di creare il contenuto di tutti i file Java e file di configurazione Bean ed eseguire l’applicazione come spiegato di seguito.

Ecco il contenuto della Logging.java file. Questo è in realtà un esempio di modulo di aspetto che definisce i metodi da chiamare in vari punti.

package com.quickgo;

public class Logging {

   /** 
    * This is the method which I would like to execute
    * before a selected method execution.
    */
   public void beforeAdvice(){
      System.out.println("Going to setup student profile.");
   }

   /** 
    * This is the method which I would like to execute
    * after a selected method execution.
    */
   public void afterAdvice(){
      System.out.println("Student profile has been setup.");
   }

   /** 
    * This is the method which I would like to execute
    * when any method returns.
    */
   public void afterReturningAdvice(Object retVal){
      System.out.println("Returning:" + retVal.toString() );
   }

   /**
    * This is the method which I would like to execute
    * if there is an exception raised.
    */
   public void AfterThrowingAdvice(IllegalArgumentException ex){
      System.out.println("There has been an exception: " + ex.toString());   
   }
   
}

Di seguito è riportato il contenuto del Student.java di file:

package com.quickgo;

public class Student {
   private Integer age;
   private String name;

   public void setAge(Integer age) {
      this.age = age;
   }
   public Integer getAge() {
	  System.out.println("Age : " + age );
      return age;
   }

   public void setName(String name) {
      this.name = name;
   }
   public String getName() {
      System.out.println("Name : " + name );
      return name;
   }
   
   public void printThrowException(){
	   System.out.println("Exception raised");
       throw new IllegalArgumentException();
   }
}

Di seguito è riportato il contenuto del MainApp.java di file:

package com.quickgo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = 
             new ClassPathXmlApplicationContext("Beans.xml");

      Student student = (Student) context.getBean("student");

      student.getName();
      student.getAge();
      
      student.printThrowException();
   }
}

In seguito  il file di configurazione beans.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/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

   <aop:config>
      <aop:aspect id="log" ref="logging">
         <aop:pointcut id="selectAll" 
         expression="execution(* com.quickgo.*.*(..))"/>
         <aop:before pointcut-ref="selectAll" method="beforeAdvice"/>
         <aop:after pointcut-ref="selectAll" method="afterAdvice"/>
         <aop:after-returning pointcut-ref="selectAll" 
                              returning="retVal"
                              method="afterReturningAdvice"/>
         <aop:after-throwing pointcut-ref="selectAll" 
                             throwing="ex"
                             method="AfterThrowingAdvice"/>
      </aop:aspect>
   </aop:config>

   <!-- Definition for student bean -->
   <bean id="student" class="com.quickgo.Student">
      <property name="name"  value="Zara" />
      <property name="age"  value="11"/>      
   </bean>

   <!-- Definition for logging aspect -->
   <bean id="logging" class="com.quickgo.Logging"/> 
      
</beans>

Una volta che hai finito con la creazione di file di origine e di configurazione di fagioli, cerchiamo di eseguire l’applicazione. Se tutto va bene con l’applicazione, questa stamperà il seguente messaggio:

Going to setup student profile.
Name : Zara
Student profile has been setup.
Returning:Zara
Going to setup student profile.
Age : 11
Student profile has been setup.
Returning:11
Going to setup student profile.
Exception raised
Student profile has been setup.
There has been an exception: java.lang.IllegalArgumentException
.....
other exception content

Mi spiego che sopra definita <aop: pointcut> seleziona tutti i metodi definiti nel quadro del pacchetto com.quickgo. Supponiamo, si desidera eseguire il vostro consiglio, prima o dopo un particolare metodo, è possibile definire la vostra pointcut per restringere la esecuzione sostituendo stelle (*) nel significato pointcut con nomi reali di classe e metodo. Di seguito è riportato un file di configurazione XML modificato per mostrare il concetto:

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

   <aop:config>
   <aop:aspect id="log" ref="logging">
      <aop:pointcut id="selectAll" 
      expression="execution(* com.quickgo.Student.getName(..))"/>
      <aop:before pointcut-ref="selectAll" method="beforeAdvice"/>
      <aop:after pointcut-ref="selectAll" method="afterAdvice"/>
   </aop:aspect>
   </aop:config>

   <!-- Definition for student bean -->
   <bean id="student" class="com.quickgo.Student">
      <property name="name"  value="Zara" />
      <property name="age"  value="11"/>      
   </bean>

   <!-- Definition for logging aspect -->
   <bean id="logging" class="com.quickgo.Logging"/> 
      
</beans>

Se si esegue l’applicazione di esempio con queste modifiche di configurazione, questo stampa il seguente messaggio:

Going to setup student profile.
Name : Zara
Student profile has been setup.
Age : 11
Exception raised
.....
other exception content