Ejemplos Prácticos de Programación Orientada a Aspectos con Spring AOP

Ejeemplos de implementación de AOP en Spring Framework

//AuditorTransversal.java
package com.ejemplo.transversal;

public class AuditorTransversal {
    public void preProcesamiento(){
        System.out.println("Antes de ejecutar la lógica de negocio, se ejecuta preProcesamiento");
    }
    
    public void postProcesamiento(){
        System.out.println("Después de ejecutar la lógica de negocio, se ejecuta postProcesamiento");
    }
}

//NegocioServicios.java
package com.ejemplo.servicio;

public class NegocioServicios {
    public void ejecutarProcesoPrincipal(){
        System.out.println("Servicio de negocio principal");
    }
    
    public void ejecutarProcesoSecundario(){
        System.out.println("Servicio de negocio secundario");
    }
    
    public NegocioServicios() {
    }
}

//PruebaFuncionamiento.java
package com.ejemplo.prueba;

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

import com.ejemplo.servicio.NegocioServicios;

public class PruebaFuncionamiento {
    @Test
    public void verificarFuncionalidad(){
        ApplicationContext contexto= new ClassPathXmlApplicationContext("configuracionSpring.xml");
        NegocioServicios servicio=contexto.getBean("servicioPrincipal",NegocioServicios.class);
        servicio.ejecutarProcesoPrincipal();
        System.out.println();
        servicio.ejecutarProcesoSecundario();
    }
}

一:Configuración mediante XML

①Configuración en configuracionSpring.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:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"  
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<bean id="servicioPrincipal" class="com.ejemplo.servicio.NegocioServicios"></bean>
<bean id="auditor" class="com.ejemplo.transversal.AuditorTransversal"></bean>
<aop:config>
    <aop:aspect ref="auditor">
        <aop:pointcut expression="execution(* com.ejemplo.servicio.*.*(..))" id="puntoCorte"/>
        <aop:after method="postProcesamiento" pointcut-ref="puntoCorte"/>
        <aop:before method="preProcesamiento" pointcut-ref="puntoCorte"/>
    </aop:aspect>
</aop:config>
</beans>

②Al ejecutar el método verificarFuncionalidad de la clase PruebaFuncionamiento mediante JUnit, se obtineen los siguientes resultados

二:Configuración mediante anotaciones

①Configurcaión en configuracionSpring.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:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"  
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
        
<bean id="servicioPrincipal" class="com.ejemplo.servicio.NegocioServicios"></bean>
<bean id="auditor" class="com.ejemplo.transversal.AuditorTransversal"></bean>
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
</beans>

②Código de AuditorTransversal.java, las demás clases permanecen sin cambios

package com.ejemplo.transversal;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
public class AuditorTransversal {
    @Before("execution(* com.ejemplo.servicio.*.*(..))")
    public void preProcesamiento(){
        System.out.println("Antes de ejecutar la lógica de negocio, se ejecuta preProcesamiento");
    }
    
    @After("execution(* com.ejemplo.servicio.*.*(..))")
    public void postProcesamiento(){
        System.out.println("Después de ejecutar la lógica de negocio, se ejecuta postProcesamiento");
    }
}

③Al ejecutar el método verificarFuncionalidad de la clase PruebaFuncionamiento mediante JUnit, se obtienen los siguientes resultados

Etiquetas: spring-aop programación-orientada-aspectos dependency-injection AspectJ spring-framework

Publicado el 9-5 16:20