> 文档中心 > spring--AOP的五种通知方式(强制使用cglib)--xml配置

spring--AOP的五种通知方式(强制使用cglib)--xml配置

  • pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>nadoutong</groupId>    <artifactId>spring_day04_adviceType</artifactId>    <version>1.0-SNAPSHOT</version>    <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target>    </properties>    <dependencies> <dependency>     <groupId>org.springframework</groupId>     <artifactId>spring-context</artifactId>     <version>5.3.6</version> </dependency> <dependency>     <groupId>org.aspectj</groupId>     <artifactId>aspectjweaver</artifactId>     <version>1.9.6</version> </dependency> <dependency>     <groupId>junit</groupId>     <artifactId>junit</artifactId>     <version>4.12</version>     <scope>test</scope> </dependency>    </dependencies></project>
  • bean.xml
<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:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation=" http://www.springframework.org/schema/beans  https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">    <bean id="accountService" class="com.nadoutong.service.impl.AccountServiceImpl" ></bean>    <bean id="logger" class="com.nadoutong.util.Logger"></bean>  <aop:config>      <aop:pointcut id="pt1" expression="execution(* com.nadoutong..service.impl.*.*(..))"/>      <aop:aspect id="logAdvice" ref="logger"><!--   --><!--   --><!--   --><!--   -->     <aop:around method="aroudadvice" pointcut-ref="pt1"></aop:around>      </aop:aspect>  </aop:config>    <aop:aspectj-autoproxy proxy-target-class="true"/></beans>
  • service包
package com.nadoutong.service;public interface AccountService {    void transfer(int a, double b);}
package com.nadoutong.service.impl;import com.nadoutong.service.AccountService;public class AccountServiceImpl implements AccountService {    public void transfer(int a,double b) { System.out.println("转账成功");    }}
  • utils包
package com.nadoutong.util;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.reflect.CodeSignature;import java.util.HashMap;import java.util.Map;public class Logger {    public void beforeadvice(){ System.out.println("前置通知");    }    public void afteradvice(){ System.out.println("后置通知");    }    public void throwingadvice(){ System.out.println("异常通知");    }    public void finnaladvice(){ System.out.println("最终通知");    }    //    环绕通知//    ProceedingJoinPoint 类似于method.invoke()    public Object aroudadvice(ProceedingJoinPoint pjp){ try {     System.out.println("前置通知");//     获取参数名称和参数值     Object[] args = pjp.getArgs();     String[] paramNames = ((CodeSignature) pjp.getSignature()).getParameterNames();    for (int i = 0; i < args.length; i++) { System.out.println("参数名称"+paramNames[i]+";参数值:"+args[i]);     }     Object proceed = pjp.proceed(args);//执行业务层逻辑代码     System.out.println("后置通知");     return proceed; } catch (Throwable throwable) {     System.out.println("异常通知");     throw new RuntimeException(); } finally {     System.out.println("最终通知"); }    }}
  • 测试
package com.nadoutong;import com.nadoutong.service.AccountService;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class AOPTest {    @org.junit.Test    public void test01(){// ClassPathXmlApplicationContext:只能读放在web-info/classes目录下的配置文件 ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml"); AccountService accountService=(AccountService)applicationContext.getBean("accountService"); accountService.transfer(1,2);    }}
  • 测试结果
    在这里插入图片描述