SpringAop 异常通知ThrowsAdvice 模拟登录小案例及解析

Aop原理

SpringAop 原理就是动态代理

对于实现接口的目标类使用的是jdk动态代理

对于没有实现任何接口的目标类,使用的是cglib的动态代理

代理类是程序在运行期间由JVM根据反射等机制动态生成的自动生成代理类和代理对象。

所谓动态就是指在程序运行前不存在代理类的字节码文件。

SpringAop的配置方式

三种配置方式

一:SpringAop1.x 使用ProxyFactoryBean手动配置

二:SpringAop2.x 基于命名控件的配置

三:Annotation 基于注解的配置(推荐)

Advice类型

SpringAop支持五种类型的通知(增强)
在这里插入图片描述
注意:多个Advice之间不允许有耦合,即多个Advice之间不允许有业务交叉。

(1):SpringAop1.x 使用ProxyFactoryBean 手动代理

配置方式:

基本用法: 添加jar包

 <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <!--ioc01-core-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </dependency>
        <!--ioc01-bean-->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>
        <!--ioc01-context-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <!--ioc01-expression-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
        </dependency>
        <!--Aop依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>
        <!--cglib技术-->
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
        </dependency>
    </dependencies>

1:配置Advice通知

1定义增强类,实现相应的接口

package springaop05.advice;

        import org.springframework.aop.ThrowsAdvice;

/**
 * package_name:springaop05.advice
 *
 * @author:徐亚远 Date:2020/2/20 11:16
 * 项目名:springDemo01
 * Description:TODO
 * Version: 1.0
 **/

public class ExceptionAdvice implements ThrowsAdvice {
    //自定义异常类型这里自己随便编写注意方法名要写成 afterThrowing
    public void afterThrowing(Exception e){
        System.out.println("异常:"+e);
    }
    public void afterThrowing(NullPointerException e){
        System.out.println("空指针异常:"+e);
    }
    public void afterThrowing(ArithmeticException e){
        System.out.println("算数异常:"+e);
    }

}

配置sping.xml

  <!--配置异常通知-->
    <bean id="exceptionAdvice" class="springaop05.advice.ExceptionAdvice"/>

书写UserService接口

package springaop05.service;


/**
 * package_name:springaop01.service
 *
 * author:徐亚远 Date:2020/2/18 18:29
 * 项目名:springDemo01
 * Description:
 **/
public interface UserService {

    /**
     * @Author : 徐亚远
     * @Date : 2020/2/18 20:34
     * @param username
     * @param password
     * @Description :
     */
    void login(String username, String password);
}

书写UserService实现类

package springaop05.service.impl;

import springaop05.service.UserService;

/**
 * package_name:springaop01.service.impl
 * Author:徐亚远
 * Date:2020/2/18 18:29
 * 项目名:springDemo01
 * Desription:
 **/
public class UserServlceImpl implements UserService {


    /**
     * @param password
     * @param username
     * @Author : 徐亚远
     * @Date : 2020/2/18 21:03
     * @Description :
     */

    @Override
    public void login(String username, String password) {

        System.out.println("loginUserServiceImpl登录方法执行:" + username + "    " + password);
        int i = 10/0;
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

<!--配置目标类实例-->
    <bean id="userServiceTarget" class="springaop05.service.impl.UserServlceImpl"/>
<!--配置advisor-->
    <bean id="exceptionAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
        <!--配置通知-->
        <property name="advice" ref="exceptionAdvice"/>
        <!--配置切点-->
        <property name="mappedNames">
            <list>
                <value>login</value>
            </list>
        </property>
    </bean>
 <!--配置代理-->
    <bean id="userService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--配置目标类-->
        <property name="target" ref="userServiceTarget"/>
        <!--配置目标类接口列表-->
        <property name="interfaces">
            <list>
                <value>springaop05.service.UserService</value>
            </list>
        </property>
        <!--配置交叉业务-->
        <property name="interceptorNames">
            <list>
                <value>exceptionAdvisor</value>
            </list>
        </property>
    </bean>

完整的spring.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--springAop 1.x配置-->

    <!--配置目标类实例-->
    <bean id="userServiceTarget" class="springaop05.service.impl.UserServlceImpl"/>
    <!--配置异常通知-->
    <bean id="exceptionAdvice" class="springaop05.advice.ExceptionAdvice"/>
    <!--配置advisor-->
    <bean id="exceptionAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
        <!--配置通知-->
        <property name="advice" ref="exceptionAdvice"/>
        <!--配置切点-->
        <property name="mappedNames">
            <list>
                <value>login</value>
            </list>
        </property>
    </bean>
    <!--配置代理-->
    <bean id="userService" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--配置目标类-->
        <property name="target" ref="userServiceTarget"/>
        <!--配置目标类接口列表-->
        <property name="interfaces">
            <list>
                <value>springaop05.service.UserService</value>
            </list>
        </property>
        <!--配置交叉业务-->
        <property name="interceptorNames">
            <list>
                <value>exceptionAdvisor</value>
            </list>
        </property>
    </bean>
</beans>

书写测试类

package springaop05.controller;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import springaop05.service.UserService;

/**
 * package_name:springaop05.controller
 *
 * @author:徐亚远 Date:2020/2/20 11:31
 * 项目名:springDemo01
 * Description:TODO
 * Version: 1.0
 **/

public class ExceptionTest {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("aop05/spring.xml");
        UserService userService = (UserService) ac.getBean("userService");
        userService.login("admin", "admin");
        System.out.println(userService.getClass());
    }
}

测试结果如图:
在这里插入图片描述

发布了64 篇原创文章 · 获赞 1 · 访问量 886

猜你喜欢

转载自blog.csdn.net/weixin_43311650/article/details/104409074