spring之AOP

AOP面向切面编程 思想
这里写图片描述

spring中有用到aop

spring中的AOP

原理:底层利用动态代理(两种动态代理技术都使用了)
两种实现方案:
第一种:JDK动态代理技术
实现的InvocationHandler接口,要想实现某个类的动态代理对象,必须有接口有实现类。
第二种:cglib动态代理技术(需要导入spring包–4个核心包)
实现了MethodInterceptor接口,原理是继承要代理的类。

AOP中的名词解释:
连接点JoinPoint:
切入点PointCut:
通知/增强Advice:
目标对象Target:
代理Proxy:
织入Weaving:
切面Aspect:

spring中aop实例 举例

第一步:导入包 4 + 2 + 2 + 2

4 个核心包+2个日志包+2个jar包:aop和切面+2织入
这里写图片描述

第二步:准备被代理对象,即目标对象
编写一个接口UserService 和 UserServiceImpl,具体详见代码

第三步:编写通知类

public class MyAdvice {
    //Spring中的代码增强,方式非常多
    // 前置通知
       //--目标对象方法(切入点)执行之前调用
    // 后置通知(如果出现异常不会执行)
        //--目标对象方法(切入点)执行之后调用
    // 环绕通知
        //--目标对象方法(切入点)执行之前和之后调用
    // 异常拦截通知
        //--切入点执行异常才会调用
    // 后置通知(不论是否出现异常都会执行)
        //--目标对象方法(切入点)执行之后调用

        // 前置通知
        public void before(){
            System.out.println("这是前置通知代码");
        }

        // 后置通知
        public void after01(){
            System.out.println("这是后置通知代码(出现异常不调用)");
        }

        // 后置通知
        public void after02(){
                    System.out.println("这是后置通知代码(都调用)");
        }

        //异常通知
        public void afterException(){
            System.out.println("妈呀,出异常了,怎么办?");
        }

        //环绕通知
        public Object  around(ProceedingJoinPoint pip) throws Throwable{
            System.out.println("这是环绕通知前");
            Object obj=pip.proceed();
            System.out.println("这是环绕通知后");
            return obj;
        }

}

第四步:书写配置文件 applicationContext

<!-- 不要忘了 aop约束 -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop    http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">

  <bean name="userService" class="com.ygjedu.service.UserServiceImpl"></bean>
  <bean name="myAdvice" class="com.ygjedu.advice.MyAdvice"></bean>

  <!-- weaving -->
  <aop:config>
      <!--  表达式
        public void  com.ygjedu.service.UserServiceImpl.save()
        void  com.ygjedu.service.UserServiceImpl.save()
        *  com.ygjedu.service.UserServiceImpl.*()
        *  com.ygjedu.service.*ServiceImpl.*(..)
        *  com.ygjedu.service..*ServiceImpl.*(..)

       -->
       <!-- 切入點: 想要增強的方法 -->
      <aop:pointcut expression="execution(* com.ygjedu.service..*ServiceImpl.*(..))" id="pc"/>
      <!-- 切面:增強、通知  +  切入點   -->
      <aop:aspect ref="myAdvice">
          <!-- 前置增強 -->
          <aop:before method="before" pointcut-ref="pc"/>
          <aop:after-returning method="after01" pointcut-ref="pc"/>
          <aop:after method="after02" pointcut-ref="pc"/>
          <aop:after-throwing method="afterException" pointcut-ref="pc"/>
          <aop:around method="around"  pointcut-ref="pc"/>

      </aop:aspect>

  </aop:config>

</beans>

第五步:测试 如果使用junit和spring整合的方式测试,需要导入包(3个)

package com.ygjedu.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.ygjedu.service.UserService;
//帮我们创建容器
@RunWith(SpringJUnit4ClassRunner.class)
//创建容器时,需要指定配置文件
@ContextConfiguration("classpath:applicationContext02.xml")
public class SpringJUnit {

    //代理對象
    @Autowired
    private UserService userService;

    @Test
    public void  testAop(){

        userService.save();
    }

}

注解方式:以上是通过xml配置方式书写,以下是通过注解方法书写。

package com.ygjedu.advice;

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;

@Aspect //表示该类为通知类、增强类
public class MyAdvice02 {

    //Spring中的代码增强,方式非常多
    // 前置通知
       //--目标对象方法(切入点)执行之前调用
    // 后置通知(如果出现异常不会执行)
        //--目标对象方法(切入点)执行之后调用
    // 环绕通知
        //--目标对象方法(切入点)执行之前和之后调用
    // 异常拦截通知
        //--切入点执行异常才会调用
    // 后置通知(不论是否出现异常都会执行)
        //--目标对象方法(切入点)执行之后调用

        // 前置通知
        @Before("execution(* com.ygjedu.service..*ServiceImpl.*(..))")
        public void before(){
            System.out.println("这是前置通知代码");
        }

        // 后置通知
        @AfterReturning("execution(* com.ygjedu.service..*ServiceImpl.*(..))")
        public void after01(){
            System.out.println("这是后置通知代码(出现异常不调用)");
        }

        // 后置通知
        @After("execution(* com.ygjedu.service..*ServiceImpl.*(..))")
        public void after02(){
                    System.out.println("这是后置通知代码(都调用)");
        }

        //异常通知
        @AfterThrowing("execution(* com.ygjedu.service..*ServiceImpl.*(..))")
        public void afterException(){
            System.out.println("妈呀,出异常了,怎么办?");
        }

        //环绕通知
        @Around("execution(* com.ygjedu.service..*ServiceImpl.*(..))")
        public Object  around(ProceedingJoinPoint pip) throws Throwable{
            System.out.println("这是环绕通知前");
            Object obj=pip.proceed();
            System.out.println("这是环绕通知后");
            return obj;
        }

}

配置文件: applicationContext

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">

  <bean name="userService" class="com.ygjedu.service.UserServiceImpl"></bean>
  <bean name="myAdvice" class="com.ygjedu.advice.MyAdvice02"></bean>

  <!-- 开启使用注解织入 -->
  <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

错误总结:
不要忘记写execution单词
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值