【SSM框架入门】spring基于xml配置AOP

1. 创建一个maven项目

1.1 导入jar包
<?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>com.spring</groupId>
    <artifactId>springAop</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <!-- 解析切入点表达式 -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.7</version>
        </dependency>
    </dependencies>

</project>
1.2 创建模拟操作账户的接口和实现类
package com.springAoc.service;

/**
 * 账户的业务层接口
 */
public interface IAccountService {

    /**
     * 模拟保存账户
     */
    void saveAccount();

    /**
     * 模拟更新账户
     * @param i
     */
    void updateAccount(int i);

    /**模拟删除账户
     * @return
     */
    int deleteAccount();
}

package com.springAoc.service.impl;

import com.springAoc.service.IAccountService;

public class AccountServiceImpl implements IAccountService {
    public void saveAccount() {
        System.out.println("执行了保存操作");
    }

    public void updateAccount(int i) {
        System.out.println("执行了更新操作"+i);
    }

    public int deleteAccount() {
        System.out.println("执行了删除操作");
        return 0;
    }
}

3. 创建一个公共日志类,用于AOP的前置增强

package com.springAoc.utils;

/**
 * 用于记录日记的工具类,它里面提供了公共的代码
 */
public class Logger {

    /**
     * 用于打印日志,计划让其在切入点方法执行之前执行(切入点方法就是业务层方法)
     */
    public void printLog(){
        System.out.println("Logger类中的printLog方法开始记录日志。。。");
    }
}

4. 创建bean配置文件

在配置文件中导入aop的约束,配置AccountServiceImpl和日志类Logger的bean,放入spring容器。使用<aop: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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 配置spring的Ioc,把service对象配置进来 -->
    <bean id="accountService" class="com.springAop.service.impl.AccountServiceImpl"></bean>

    <!-- spring基于xml的AOP配置步骤
         1、把增强bean也交给spring管理
         2、使用aop:config标签表明开始AOP的配置
         3、使用aop:aspect标签表明配置切面
                id属性:是给切面提供一个唯一标识
                ref属性:是指定增强类bean的id
         4、在aop:aspect标签的内部使用对应标签来配置增强的类型
                我们现在的示例是让printLog方法在切入点方法执行之前执行,所以是前置增强
                aop:before:表示配置前置增强
                    method:用于指定Logger类中哪个方法是前置增强 
                    pointcut属性:用于指定切入点表达式,该表达式的含义指的是对业务层中哪些方法增强
                    
            切入点表达式的写法:
                    关键字:execution(表达式)
                    表达式:访问修饰符 返回值 包名.类名.方法名(参数列表)                    
    -->

    <!-- 配置Logger类 -->
    <bean id="logger" class="com.springAop.utils.Logger"></bean>

    <!-- 配置AOP -->
    <aop:config>
        <!-- 配置切面 -->
        <aop:aspect id="logAdvice" ref="logger">
            <!-- 配置增强的类型,并且建立增强方法和切入点方法的关联 -->
            <aop:before method="printLog" pointcut="execution(public void com.springAop.service.impl.AccountServiceImpl.saveAccount())"></aop:before>
        </aop:aspect>
    </aop:config>
</beans>

5. 新建一个测试类

package com.springAop.test;

import com.springAop.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class aopTest {
    public static void main(String[] args) {
        //1.获取容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2.获取对象
        IAccountService as = (IAccountService)ac.getBean("accountService");
        //3.执行方法
        as.saveAccount();
    }
}

6. 运行结果

在这里插入图片描述

8. 配置四个增强(前置、后置、异常、最终)

8.1 修改日志Logger类
package com.springAop.utils;

/**
 * 用于记录日记的工具类,它里面提供了公共的代码
 */
public class Logger {

    /**
     * 用于打印日志,计划让其在切入点方法执行之前执行(切入点方法就是业务层方法)
     * 前置增强
     */
    public void beforePrintLog(){
        System.out.println("前置增强————Logger类中的beforePrintLog方法开始记录日志。。。");
    }

    /**
     * 后置增强
     */
    public void afterReturnPrintLog(){
        System.out.println("后置增强————Logger类中的afterReturnPrintLog方法开始记录日志。。。");
    }

    /**
     * 异常增强
     */
    public void afterThrowingPrintLog(){
        System.out.println("异常增强————Logger类中的afterThrowingPrintLog方法开始记录日志。。。");
    }

    /**
     * 最终增强
     */
    public void afterPrintLog(){
        System.out.println("最终增强————Logger类中的afterPrintLog方法开始记录日志。。。");
    }
}
8.2 修改配置文件
<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 配置spring的Ioc,把service对象配置进来 -->
    <bean id="accountService" class="com.springAop.service.impl.AccountServiceImpl"></bean>

    <!-- 配置Logger类 -->
    <bean id="logger" class="com.springAop.utils.Logger"></bean>

    <!-- 配置AOP -->
    <aop:config>
        <!-- 配置切面 -->
        <aop:aspect id="logAdvice" ref="logger">
            <!-- 配置增强的类型,并且建立增强方法和切入点方法的关联 -->
            <!-- 配置前置增强 -->
            <aop:before method="beforePrintLog" pointcut="execution(* com.springAop.service.impl.*.*(..))"></aop:before>
            <!-- 配置后置增强 -->
            <aop:after-returning method="afterReturnPrintLog" pointcut="execution(* com.springAop.service.impl.AccountServiceImpl.*(..))"></aop:after-returning>
            <!-- 配置异常增强 -->
            <aop:after-throwing method="afterThrowingPrintLog" pointcut="execution(* com.springAop.service.impl.AccountServiceImpl.*(..))"></aop:after-throwing>
            <!-- 配置最终增强 -->
            <aop:after method="afterPrintLog" pointcut="execution(* com.springAop.service.impl.AccountServiceImpl.*(..))"></aop:after>
        </aop:aspect>
    </aop:config>
</beans>
8.3 运行结果

在这里插入图片描述

8.4 异常运行结果

在实现类中加入一个异常
在这里插入图片描述
此时就没有后置增强,只有异常增强
在这里插入图片描述

9. 切入点表达式的写法

关键字:execution(表达式)
表达式:访问修饰符 返回值 包名.类名.方法名(参数列表)
示例
public void com.springAop.service.impl.AccountServiceImpl.saveAccount()
访问修饰符可以省略
void com.springAop.service.impl.AccountServiceImpl.saveAccount()
返回值可以使用通配符,表示任意返回值
* com.springAop.service.impl.AccountServiceImpl.saveAccount()
包名可以使用通配符,表示任意包,有几级包就要写几个*.
* *.*.*.*.AccountServiceImpl.saveAccount()
包名可以使用. .表示当前包及其子包
* *. .AccountServiceImpl.saveAccount()
类名和方法名都可以用使用*来实现通配
* *. .*.*()
方法参数列表
可以直接写数据类型:
———— 基本类型直接写名称: int
———— 引用类型写包名.类名的方式:java.lang.String
可以使用通配符表示任意类型,但是必须有参数
可以使用. .表示有无参数均可,有参数可以是任意类型
全通配写法
* *. .*.*(. .)
实际开发中切入点表达式的通常写法
切到业务层实现类下的所有方法
* com.springAop.service.impl.*.*(. .)

10. 切入点表达式简化

当我们给同一个方法配置多个增强的时候,会出现重复的切入点表达式。此时可以使用aop增强标签中的pointcut-ref属性,简化切入点表达式
在这里插入图片描述

10.1 配置切入点表达式,id属性用于指定表达式的唯一标识,expression属性用于指定表达式内容
10.2 把aop增强标签中的pointcut属性换成point-ref属性

在这里插入图片描述
<aop:pointcut>标签写在<aop:aspect>标签内只能当前切面使用,它还可以写在<aop:aspect>标签外,此时就变成了所有切面可用,但是要写在<aop:aspect>标签的前面,这是spring规定的标签顺序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值