AI智能
改变未来

Spring与AOP_AspectJ的AOP编程_基于XML的实现

基于XML的实现

  • 介绍
  • 代码
  • ISomeService.java
  • MyAspect.java
  • MyTest.java
  • SomeServiceImpl.java
  • applicationContext.xml
  • 结构
  • 介绍

    前置通知
    后置通知
    环绕通知
    异常通知
    最终通知

    代码

    ISomeService.java

    package com.study.xml;// 主业务接口public interface ISomeService {// 目标方法  com.study.aop10.ISomeService.doFirstvoid doFirst();// 目标方法String doSecond();// 目标方法void doThird();}

    MyAspect.java

    package com.study.xml;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;//切面public class MyAspect {public void myBefore() {System.out.println(\"执行前置通知方法\");}public void myBefore(JoinPoint jp) {System.out.println(\"执行前置通知方法jp = \" + jp);}public void myAfterReturning() {System.out.println(\"执行后置通知方法\");}public void myAfterReturning(Object result) {System.out.println(\"执行后置通知方法 result = \" + result);}public Object myAround(ProceedingJoinPoint pjp) throws Throwable {System.out.println(\"执行环绕通知方法,目标方法执行之前\");// 执行目标方法Object result = pjp.proceed();System.out.println(\"执行环绕通知方法,目标方法执行之后\");if(result!=null) {result = ((String)result).toUpperCase();}return result;}public void myAfterThrowing() {System.out.println(\"执行异常通知方法\");}public void myAfterThrowing(Exception ex) {System.out.println(\"执行异常通知方法 ex = \" + ex.getMessage());}public void myAfter() {System.out.println(\"执行最终通知\");}}

    MyTest.java

    package com.study.xml;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.net.URI;import java.net.URL;import org.junit.Test;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;import org.springframework.core.io.ClassPathResource;import org.springframework.core.io.Resource;public class MyTest {/*** 通过junit中注解的方式@Test运行函数*/@Testpublic void test01() {// 创建容器对象, 加载Spring配置文件// 会从类路径下查找配置文件String resource = \"com/study/xml/applicationContext.xml\";ApplicationContext ac = new ClassPathXmlApplicationContext(resource);//ClassPathXmlApplicationContext();ISomeService service = (ISomeService) ac.getBean(\"someService\");service.doFirst();System.out.println(\"-----------------------------\");service.doSecond();System.out.println(\"-----------------------------\");service.doThird();}}

    SomeServiceImpl.java

    package com.study.xml;// 目标类public class SomeServiceImpl implements ISomeService {@Overridepublic void doFirst() {System.out.println(\"执行doFirst()方法\");}@Overridepublic String doSecond() {System.out.println(\"执行doSecond()方法\");return \"abcde\";}@Overridepublic void doThird() {System.out.println(\"执行doThrid()方法\");throw new RuntimeException(\"这是一个测试异常\");}}

    applicationContext.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd\"> <!-- bean definitions here --><!-- 注册切面 --><bean id=\"myAspect\" class=\"com.study.xml.MyAspect\"></bean><!-- 注册目标对象 --><bean id=\"someService\" class=\"com.study.xml.SomeServiceImpl\"></bean><!-- AOP配置 --><aop:config><aop:pointcut expression=\"execution(* *..ISomeService.doFirst(..))\" id=\"doFirstPointcut\"/><aop:pointcut expression=\"execution(* *..ISomeService.doSecond(..))\" id=\"doSecondPointcut\"/><aop:pointcut expression=\"execution(* *..ISomeService.doThird(..))\" id=\"doThirdPointcut\"/><aop:aspect ref=\"myAspect\"><aop:before method=\"myBefore\" pointcut-ref=\"doFirstPointcut\"/><!-- 前置通知 --><aop:before method=\"myBefore(org.aspectj.lang.JoinPoint)\" pointcut-ref=\"doFirstPointcut\"/><aop:after-returning method=\"myAfterReturning\" pointcut-ref=\"doSecondPointcut\"/><aop:after-returning method=\"myAfterReturning(java.lang.Object)\" pointcut-ref=\"doSecondPointcut\" returning=\"result\"/><aop:around method=\"myAround\" pointcut-ref=\"doSecondPointcut\"/><aop:after-throwing method=\"myAfterThrowing\" pointcut-ref=\"doThirdPointcut\"/><aop:after-throwing method=\"myAfterThrowing(java.lang.Exception)\" pointcut-ref=\"doThirdPointcut\" throwing=\"ex\"/><aop:after method=\"myAfter\" pointcut-ref=\"doThirdPointcut\"/></aop:aspect></aop:config></beans>

    结构


    赞(0) 打赏
    未经允许不得转载:爱站程序员基地 » Spring与AOP_AspectJ的AOP编程_基于XML的实现