• spring框架学习6:spring-aop的五种通知类型


    使用springaop时需要注意,如果bean对象,即service层的对象没有实现接口的话,使用spring-aop的话会报错,因此需要在service层创建接口。

    spring-aop的基层是基于动态代理来实现的,动态代理的实现有两种方式:

    1.jdk动态代理

      spring模式默认使用jdk动态代理,jdk动态代理要求目标类的对象必须实现一个接口,而且获取目标类对象的时候要做向上转型为接口。

    2.cglib动态代理

      cglib代理方式spring aop也支持,cglib实现动态代理的时候,不需要目标类实现接口,如果要把spring aop的代理方式改为cglib,需要如下配置:

      

      proxy-target-class如果设置成true使用cglib动态代理,默认是false使用jdk动态代理。
    spring的五种通知类型:
    通知类型 说明
    aop:before 前置通知 在目标方法调用之前执行
    aop:after-returning后置通知 在目标方法调用之后执行,一旦目标方法产生异常不会执行
    aop:after 最终通知 在目标调用方法之后执行,无论目标方法是否产生异常,都会执行
    aop:after-throwing 异常通知 在目标方法产生异常时执行
    aop:around 环绕通知 在目标方法执行之前和执行之后都会执行,可以写一些非核心的业务逻辑,一般用来替代前置通知和后置通知
     
     
     
     
     
     
     
     
     
     
     
     
    使用注解的方式配置spring-aop:
    1.创建通知类
    package com.zs.advice;
    
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.AfterThrowing;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.springframework.stereotype.Component;
    
    /**
     * 用户业务通知
     */
    @Component
    @Aspect
    public class UserAdvice {
    
        @After("execution(void *User(..))")
        public void log() {
            System.out.println("生成日志文件");
        }
    
        @Before("execution(void *User(..))")
        public void yanzheng() {
            System.out.println("验证数据");
        }
    
        @AfterThrowing(pointcut = "execution(void *User(..))",throwing = "e")
        public void error(Exception e) {
            System.out.println("异常处理");
        }
    }
     
    2.spring配置文件中声明使用注解的方式配置spring-aop;
    <?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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!--声明service对象-->
        <bean id="userService" class="com.zs.service.impl.UserServiceImpl"/>
    
        <!--扫描通知类包-->
        <context:component-scan base-package="com.zs.advice"/>
        <!--声明以注解的方式配置spring-aop-->
        <aop:aspectj-autoproxy/>
    </beans>

    3.测试

    import com.zs.entity.User;
    import com.zs.service.UserService;
    import org.junit.Test;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class AopTest {
        private ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        @Test
        public void fun1() {
            UserService us = (UserService) context.getBean("userService");
            us.saveUser(new User());
        }
    }

    结果如下:

     

     
  • 相关阅读:
    Spring配置文件中的那些标签意味着什么(持续更新)
    转 spring配置文件
    Web.xml配置详解之context-param
    web.xml 中的listener、 filter、servlet 加载顺序及其详解
    spring mvc 中web.xml配置信息解释
    转 一个web项目web.xml的配置中<context-param>配置作用
    在web.xml中通过contextConfigLocation配置spring
    (转)web.xml中的contextConfigLocation在spring中的作用
    Android菜鸟的成长笔记(20)——IntentService
    PhotoSwipe源码解读系列(二)
  • 原文地址:https://www.cnblogs.com/Zs-book1/p/11073415.html
Copyright © 2020-2023  润新知