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

  • pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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
<?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: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">

<!--将连接点,通知纳入到ioc容器中-->
    <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">

<!--&lt;!&ndash;          前置通知&ndash;&gt;-->
<!--          <aop:before method="beforeadvice" pointcut-ref="pt1"></aop:before>-->
<!--&lt;!&ndash;          最终通知&ndash;&gt;-->
<!--          <aop:after method="finnaladvice" pointcut-ref="pt1"></aop:after>-->
<!--&lt;!&ndash;          异常通知&ndash;&gt;-->
<!--          <aop:after-throwing method="throwingadvice" pointcut-ref="pt1"></aop:after-throwing>-->
<!--&lt;!&ndash;          后置通知&ndash;&gt;-->
<!--          <aop:after-returning method="afteradvice" pointcut-ref="pt1"></aop:after-returning>-->


<!--          环绕通知 配置了环绕通知就不需要配置上述四个通知-->
            <aop:around method="aroudadvice" pointcut-ref="pt1"></aop:around>
      </aop:aspect>
  </aop:config>

<!--    强制使用cglib-->
    <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);
    }

}

  • 测试结果
    在这里插入图片描述
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈行恩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值