Java第四十天,Spring框架系列,基于XML的AOP

一、搭建项目基本步骤

1.新建Maven工程

2.在pom.xml配置文件中配置打包方式并且导入相关坐标,最终如下

<?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.huhai</groupId>
    <artifactId>demo20</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--设置打包方式-->
    <packaging>jar</packaging>

    <!--导入spring坐标-->
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.7.RELEASE</version>
        </dependency>

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


</project>

3.在resources目录下新建bean.xml配置文件,并填写以下内容;可参考

spring-framework-5.0.2/spring-framework-5.0.2.RELEASE-docs/spring-framework-reference/core.html#spring-core

<?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
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

4.配置相关的AOP参数

二、基于XML的AOP标签详解

  • <aop:config> ===> 配置AOP
  • <aop:aspect id="" ref=""> ===> 配置切面;id属性:是给切面提供一个唯一标识;ref属性:是指定通知类bean的Id
  • <aop:before> ===> 前置通知
  • <aop:after-returning> ===> 后置通知
  • <aop:after-throwing> ===> 异常通知
  • <aop:after> ===> 最终通知
  • <aop:around> ===> 环绕通知
  • <aop:pointcut id="[自定义切入点ID]" expression="execution()"/> ===> 自定义切入点
  • <aop:[通知类型] method="" pointcut-ref="[引用自定义的切入点ID]"></aop:[通知类型]> ===> 引用自定义切入点

示例:

<bean id="logger" class="com.huhai.utils.Logger"></bean>
<!--使用aop:config标签表明开始AOP的配置-->
<aop:config>
    <!--使用aop:aspect标签表明配置切面-->
    <!--id属性:是给切面提供一个唯一标识;ref属性:是指定通知类bean的Id-->
    <aop:aspect id="logAdvice" ref="logger">
        <aop:pointcut id="[自定义切入点ID]" expression="execution(public void com.huhai.impl.AccountServiceImpl.saveAccount())"/>
        <!--配置前置通知 引用自定义切入点-->
        <!--切入点:业务层中被增强的方法-->
        <!--通知:具体增强的功能;该功能方法所属类位于切面中ref指定的bean类-->
        <aop:before method="[通知指向的方法]" pointcut-ref="[自定义切入点ID]"></aop:before>
        <!--配置最终通知 不引用,直接指定-->
        <aop:before method="returnLog" pointcut="execution(public void com.huhai.impl.AccountServiceImpl.saveAccount())"
    </aop:aspect>
</aop:config>

三、切入点表达式的写法:

1.切入点表达式写法规则:

execution(表达式)

2.表达式:

访问修饰符   返回值   包名.类名.方法名(参数列表)
  • 访问修饰符可以省略
  • 返回值可以使用通配符*表示任意返回值
  • 包名可以使用通配符*表示任意包。但是有几级包就需要写几个*
  • 包名可以使用..表示当前包及其子包
  • 类名和方法名都可以使用*来实现通配
  • 方法参数列表规则:

①可以直接写数据类型:

基本类型直接写名称
int

引用类型写包名.类名的方式
java.lang.String

②可以使用通配符表示任意类型,但是必须有参数

③可以使用..表示有无参数均可,有参数可以是任意类型

  • 全通配写法:
* *..*.*(..)
  • 实际开发中切入点表达式的通常写法:

切到业务层实现类下的所有方法

三、示例代码

1.项目架构

2.业务层接口

package com.huhai;

public interface IAccountService {
    void saveAccount();
    void updateAccount(int i);
    int deleteAccount();
}

3.业务层接口实现类

package com.huhai.impl;

import com.huhai.IAccountService;

public class AccountServiceImpl implements IAccountService {

    public void saveAccount() {
        System.out.println("账户成功保存");
    }

    public void updateAccount(int i) {
        System.out.println("账户成功更新");
    }

    public int deleteAccount() {
        System.out.println("账户成功删除");
        return 0;
    }
}

4.表现层

package com.huhai.ui;

import com.huhai.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Realize {
    public static void main(String[] args) {
        //获取容器
        ApplicationContext app = new ClassPathXmlApplicationContext("bean.xml");
        //获取Bean对象
        IAccountService accountService = (IAccountService) app.getBean("accountServiceImpl");
        accountService.saveAccount();
        accountService.updateAccount(1);
        accountService.deleteAccount();
    }
}

5.bean.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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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">

    <bean id="accountServiceImpl" class="com.huhai.impl.AccountServiceImpl"></bean>

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

    <!--使用aop:config标签表明开始AOP的配置-->
    <aop:config>
        <!--使用aop:aspect标签表明配置切面-->
        <!--id属性:是给切面提供一个唯一标识;ref属性:是指定通知类bean的Id-->
        <aop:aspect id="logAdvice" ref="logger">
            <!--aop:before表明是前置通知-->
            <!--aop:after-returning后置通知-->
            <!--aop:after-throwing异常通知-->
            <!--aop:after最终通知-->
            <!--aop:around环绕通知-->

            <!--aop:before表明是前置通知-->

            <!--method指明前置通知的方法-->
            <!--pointcut属性:用于指定切入点表达式,该表达式的含义指的是对业务层中哪些方法增强-->

            <!--切入点表达式的写法:-->
            <!--关键字:execution(表达式)-->
            <!--表达式:-->
            <!--访问修饰符 返回值 包名.类名.方法名(参数列表)-->

            <!--
            切入点表达式的写法:
                关键字:execution(表达式)
                表达式:
                    访问修饰符   返回值   包名.包名.包名...类名.方法名(参数列表)
                标准的表达式写法:
                    public void com.packet.huhai.impl.AccountServiceImpl.saveAccount()
                访问修饰符可以省略
                    void com.huhai.service.impl.AccountServiceImpl.saveAccount()
                返回值可以使用通配符*表示任意返回值
                    * com.huhai.service.impl.AccountServiceImpl.saveAccount()
                包名可以使用通配符*表示任意包。但是有几级包就需要写几个*.
                    * *.*.*.*.AccountServiceImpl.saveAccount())
                包名可以使用..表示当前包及其子包
                    * *..AccountServiceImpl.saveAccount()
                类名和方法名都可以使用*来实现通配
                    * *..*.*()
                参数列表:
                    可以直接写数据类型:
                        基本类型直接写名称           int
                        引用类型写包名.类名的方式   java.lang.String
                    可以使用通配符表示任意类型,但是必须有参数
                    可以使用..表示有无参数均可,有参数可以是任意类型
                全通配写法:
                    * *..*.*(..)

                实际开发中切入点表达式的通常写法:
                    切到业务层实现类下的所有方法
                        * com.huhai.service.impl.*.*(..)
            -->

            <!--配置通知时使用通配符-->
            <aop:before method="allBefore" pointcut="execution(* com.huhai.impl.*.*(..))"></aop:before>

            <!--配置切入点表达式
            id属性用于指定表达式的唯一标识
            expression属性用于指定表达式内容
            此标签写在aop:aspect标签内部只能当前切面使用
            它还可以写在aop:aspect外面,此时就变成了所有切面可用;但需要注意的是被引用的标签必须写在引用标签的前面
            -->
            <aop:pointcut id="stand" expression="execution(public void com.huhai.impl.AccountServiceImpl.saveAccount())"/>

            <!--配置五种通知-->

            <!--配置前置通知-->
            <aop:before method="beforeLog" pointcut-ref="stand"></aop:before>
            <!--配置最终通知-->
            <aop:after method="afterLog" pointcut-ref="stand"></aop:after>
            <!--配置异常通知-->
            <aop:after-throwing method="exceptionLog" pointcut-ref="stand"></aop:after-throwing>
            <!--配置后置通知-->
            <aop:after-returning method="returnLog" pointcut-ref="stand"></aop:after-returning>
            <!--配置环绕通知-->
            <aop:around method="aroundLog" pointcut-ref="stand"></aop:around>
        </aop:aspect>
    </aop:config>

</beans>

6.pom.xml

<?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.huhai</groupId>
    <artifactId>demo20</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--设置打包方式-->
    <packaging>jar</packaging>

    <!--导入spring坐标-->
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.7.RELEASE</version>
        </dependency>

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


</project>
作者:蓝月

-------------------------------------------

个性签名:能我之人何其多,戒骄戒躁,脚踏实地地走好每一步

原文地址:https://www.cnblogs.com/viplanyue/p/13573744.html