Elementos de configuración de AOP
En el espacio de nombres aop de Spring, se proporcionan múltiples elementos para declarar aspectos en XML.
1) <aop: advisor>: definir notificador AOP
2) <aop: after>: define la notificación posterior de AOP (independientemente de si el método notificado se ejecuta correctamente)
3) <aop: after-return>: define la notificación de devolución de AOP
4) <aop: after-throwing>: Definir notificación de excepción AOP
5) <aop: around>: definir AOP en torno a la notificación
6) <aop: aspecto>: definir un aspecto
7) <aop: aspectj-autoproxy>: Habilita aspectos basados en anotaciones @Aspect
8) <aop: before>: Defina la notificación previa de AOP
9) <aop: config>: el elemento de configuración de AOP de nivel superior, la mayoría de los elementos <asop: *> deben estar en este elemento
10) <aop: declare-parent>: introduzca interfaces adicionales para el objeto notificado de forma transparente
11) <aop: pointcut>: definir un pointcut
Caso:
Uno: definir una interfaz
package springaop.xml; public interface Performance { void perform(String performContent); }
Dos: implementar la interfaz
package springaop.xml.impl; import org.springframework.stereotype.Component; import springaop.xml.Performance; @Component public class Concert implements Performance { @Override public void perform(String performContent) { System.out.println(performContent + "表演中。。。"); } }
Tres: fideos de Aop
package springaop.xml; import org.aspectj.lang.ProceedingJoinPoint; public class Audience { public void performance(String performContent) {}; public void sliencingPhones(String performContent){ System.out.println(performContent); System.out.println("Sliencing cell phones"); } public void sliencingPhonesTest(){ System.out.println("Sliencing cell phones"); } public void countTrack(int trackNumber){ System.out.println(trackNumber); System.out.println("带有参数的测试!"); } public void watchPerformance(ProceedingJoinPoint jp) { try { System.out.println("Taking seats"); jp.proceed(); System.out.println("CLAP CLAP CLAP!!!"); } catch (Throwable e) { System.out.println("Demand a refund"); e.printStackTrace(); } } // public void silenceCellPhone(){ // System.out.println("开始表演"); // } // // // // public void takeSeates(){ // System.out.println("Taking seats"); // } // // // public void applause(){ // System.out.println("CLAP CLAP CLAP"); // } // // public void demandRefund(){ // System.out.println("Demanding a refund"); // } }
Cuatro: declaración XML de aop
<?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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="springaop.xml"/> <bean id="defaultEncoreable" class="springaop.xml.impl.DefaultEncoreable"/> <bean id="audience" class="springaop.xml.Audience" /> <aop:config> <!-- <aop:around pointcut="execution(springaop.xml.Audience.zachary.aop.Performance.perform(..))" method="watchPerformance"/> --> <aop:aspect ref="audience"> <aop:pointcut id="performance" expression="execution(* springaop.xml.Performance.perform(..))" /> <!--<aop:before --> <!--pointcut-ref="performance"--> <!--method="sliencingPhones"/>--> <aop:around pointcut-ref="performance" method="watchPerformance"/> <aop:after pointcut-ref="performance" method="sliencingPhonesTest" /> <aop:before pointcut-ref="performance" method="sliencingPhonesTest"/> <!--<aop:declare-parents--> <!--types-matching="springaop.xml.impl.Concert"--> <!--implement-interface="springaop.xml.Encoreable"--> <!--delegate-ref="defaultEncoreable" />--> <!-- <aop:declare-parents types-matching="com.zachary.aop.Concert" implement-interface="com.zachary.aop.Encoreable" default-impl="com.zachary.aop.DefaultEncoreable" /> --> </aop:aspect> </aop:config> <aop:aspectj-autoproxy /> </beans>
Cinco: entorno de prueba:
package springaop.springaop; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.test.context.ContextConfiguration; import springaop.aopservice.Animal; import springaop.aopservice.HelloInterface; import springaop.aopservice.Person; import springaop.aopservice.AnnotationConfig; import springaop.xml.Encoreable; import springaop.xml.Performance; @SpringBootTest @ContextConfiguration(locations = "classpath:aop-config.xml") class SpringaopApplicationTests { @Autowired private Performance concert; ///xml中声明切面 @Test public void test() { concert.perform("测试-"); // concert.perform("perform1"); // Encoreable encoreable = (Encoreable) concert; // encoreable.performEncore(); } }