基于xml的aspectJ

一、知识点总结
spring基于 AspectJ完成对 aop的操作

熟练掌握spring中aop的术语:
1)目标类: target —> UserServiceImpl
2)连接点: JoinPoint 是 目标类中 需要添加功能的方法
3)切入点: PointCut 是 目标类中 添加了功能的方法
4)通知/ 增强: advice 将要添加的 功能;
5)切面类:aspect 就是持有 功能的类;
6)织入: weaving 将 目标类和 切面类 整合到 代理类的过程叫 织入
7)代理类: proxy

4+1 ,core(核心包), beans,context,expression, common-loggin(依赖包)
spring-aop(注解包), spring-aspect(aspectj框架包) ,weaving.jar 织入包;

aspectJ框架 给我们提供了 5种 通知,增强类型:
1)前置通知 before, 在执行目标方法前执行
2)后置通知 afterReturning(); 在执行目标方法后执行
3) 环绕通知 around(); 在目标方法前后都执行
4)异常通知 afterThrowing 出线异常时执行
5)最终通知: after();

切入点表达式:
execution(访问修饰符 返回值类型 包路径名.类名.方法名(…) 异常);

execution( * com.oracle.serviceImpl.UserServiceImpl.add (…));
}
二、工程架构
(1)架构图
在这里插入图片描述
(2)实例Code

1.com.oracle.aspect包:
package com.oracle.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

public class MyAspect {
    //前置通知
    public void before(JoinPoint joinPoint){
        System.out.println("方法名:"+joinPoint.getSignature().getName());
        System.out.println("我是前置通知");
    }
    //后置通知
    public void afterReturning(JoinPoint joinPoint,Object obj){
        System.out.println("方法名:"+joinPoint.getSignature().getName()+",返回值:"+obj);
        System.out.println("我是后置通知");
    }
    //环绕通知
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("事务开启!");
        Object proceed = proceedingJoinPoint.proceed();
        System.out.println("事务关闭!");
        System.out.println(proceed);
        return proceed;

    }
    //最终通知
    public void after(JoinPoint joinPoint){
        System.out.println("方法名:"+joinPoint.getSignature().getName());
        System.out.println("我是最终通知");
    }
    //异常通知
    public void afterThrowing(JoinPoint joinPoint,Throwable e){
        System.out.println("方法名:"+joinPoint.getSignature().getName()+"异常:"+e.getMessage());
        System.out.println("我是异常通知");
    }
}
2.com.oracle.service
package com.oracle.service;

public interface UserService {
    public void addUser();
    public boolean deleteUser();
    public void updateUser();
}
3.com.oracle.serviceImpl包:
package com.oracle.serviceImpl;

import com.oracle.service.UserService;

public class UserServiceImpl implements UserService {
    @Override
    public void addUser() {
       System.out.println("添加。。。");
    }

    @Override
    public boolean deleteUser() {
        System.out.println("删除。。。");
        return false;
    }

    @Override
    public void updateUser() {
        //int a = 1/0;
        System.out.println("修改。。。");
    }
}
4.com.oracle.test
package com.oracle.test;

import com.oracle.service.UserService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Demo {

    @Test
    public void Testbefore(){
        ClassPathXmlApplicationContext c = new ClassPathXmlApplicationContext("config/applicationContext.xml");
        UserService userService = (UserService) c.getBean("userServiceId");
        userService.addUser();
        userService.deleteUser();
        userService.updateUser();

    }
    @Test
    public void TestafterReturning(){
        ClassPathXmlApplicationContext c = new ClassPathXmlApplicationContext("config/applicationContext.xml");
        UserService userService = (UserService) c.getBean("userServiceId");
        userService.addUser();
        userService.deleteUser();
        userService.updateUser();
    }
    @Test
    public void Testaround(){
        ClassPathXmlApplicationContext c = new ClassPathXmlApplicationContext("config/applicationContext.xml");
        UserService userService = (UserService) c.getBean("userServiceId");
        userService.addUser();
        userService.deleteUser();
        userService.updateUser();
    }
    @Test
    public void Testafter(){
        ClassPathXmlApplicationContext c = new ClassPathXmlApplicationContext("config/applicationContext.xml");
        UserService userService = (UserService) c.getBean("userServiceId");
        userService.addUser();
        userService.deleteUser();
        userService.updateUser();
    }
    @Test
    public void afterThrowing(){
        ClassPathXmlApplicationContext c = new ClassPathXmlApplicationContext("config/applicationContext.xml");
        UserService userService = (UserService) c.getBean("userServiceId");
        userService.addUser();
        userService.deleteUser();
        userService.updateUser();
    }
}
5.config包:
<?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/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="userServiceId" class="com.oracle.serviceImpl.UserServiceImpl"></bean>
        <!--切面类-->
        <bean id="myAspect" class="com.oracle.aspect.MyAspect"></bean>
   <!--aop的配置-->
    <aop:config>
        <aop:pointcut id="mypointcut" expression="execution(* com.oracle.serviceImpl.UserServiceImpl.*(..))"></aop:pointcut>
        <aop:aspect ref="myAspect">
            <!--前置通知-->
            <!--<aop:before method="before" pointcut-ref="mypointcut"></aop:before>-->
            <!--后置通知-->
            <!--<aop:after-returning method="afterReturning" pointcut-ref="mypointcut" returning="obj"></aop:after-returning>-->
            <!--环绕通知-->
            <!--<aop:around method="around" pointcut-ref="mypointcut"></aop:around>-->
            <!--最终通知-->
            <!--<aop:after method="after" pointcut-ref="mypointcut"></aop:after>-->
            <!--异常通知-->
            <aop:after-throwing method="afterThrowing" throwing="e" pointcut-ref="mypointcut"></aop:after-throwing>

        </aop:aspect>

    </aop:config>


    <context:annotation-config/>

</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值