基于xml的AOP开发

开发明确

  • 编写核心业务类代码(目标类的目标方法)
  • 编写切面类,切面类中有增强方法
  • 在配置文件中,配置织入关系

1、快速入门

原先代码可查看https://blog.csdn.net/weixin_35798336/article/details/112466111。期间有用底层实现的动态代理,实际上也就是把代理对象的工厂用配置形式实现。

  1. 导入AOP相关坐标

    <?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.chen</groupId>
        <artifactId>Spring</artifactId>
        <packaging>pom</packaging>
        <version>1.0-SNAPSHOT</version>
        <modules>
            <module>05spring-account</module>
            <module>06spring-aop</module>
        </modules>
    
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.13.1</version>
            </dependency>
    
            <!--包含aop,beans,core坐标-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.0.2.RELEASE</version>
            </dependency>
    
            <!--spring发现aspectj比自己本身的好,所以将其引入-->
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.8.4</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>5.0.2.RELEASE</version>
            </dependency>
    
            <dependency>
                <groupId>commons-dbutils</groupId>
                <artifactId>commons-dbutils</artifactId>
                <version>1.4</version>
            </dependency>
    
            <dependency>
                <groupId>com.mchange</groupId>
                <artifactId>c3p0</artifactId>
                <version>0.9.5.2</version>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.6</version>
            </dependency>
        </dependencies>
    
        <properties>
            <maven.compiler.source>8</maven.compiler.source>
            <maven.compiler.target>8</maven.compiler.target>
        </properties>
    
    </project>
    
  2. 创建目标接口(可自行创建)和目标类

  3. 创建切面类(内部增强方法)

  4. 将目标接口和切面类的创建交给spring

  5. 在配置文件中配置织入关系

    <?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="accountService" class="com.chen.service.impl.AccountServiceImpl">
            <!-- 注入dao -->
            <property name="accountDao" ref="accountDao"></property>
        </bean>
    
        <!-- 配置事务管理器-->
        <!--配置切面对象-->
        <bean id="txManager" class="com.chen.utils.TransactionManager">
            <!-- 注入ConnectionUtils -->
            <property name="connectionUtils" ref="connectionUtils"></property>
        </bean>
    
        <!--配置织入(哪些方法需要哪些增强==通知+切点)-->
        <aop:config>
            <aop:aspect id="txManagerAdvice" ref="txManager">
                <aop:pointcut id="pt1" expression="execution(* com.chen.service.impl.*.*(..))"/>
                <aop:before method="beginTransaction" pointcut-ref="pt1"></aop:before>
                <aop:after method="commit" pointcut-ref="pt1"></aop:after>
                <aop:after-throwing method="rollback" pointcut-ref="pt1"></aop:after-throwing>
                <aop:after-returning method="release" pointcut-ref="pt1"></aop:after-returning>
            </aop:aspect>
        </aop:config>
    
        <!--配置Dao对象-->
        <bean id="accountDao" class="com.chen.dao.impl.AccountDaoImpl">
            <!-- 注入QueryRunner -->
            <property name="runner" ref="runner"></property>
            <!-- 注入ConnectionUtils -->
            <property name="connectionUtils" ref="connectionUtils"></property>
        </bean>
    
        <!--配置QueryRunner-->
        <bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype"></bean>
    
        <!-- 配置数据源 -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <!--连接数据库的必备信息-->
            <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
            <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/nba"></property>
            <property name="user" value="root"></property>
            <property name="password" value="root"></property>
        </bean>
    
        <!-- 配置Connection的工具类 ConnectionUtils -->
        <bean id="connectionUtils" class="com.chen.utils.ConnectionUtils">
            <!-- 注入数据源-->
            <property name="dataSource" ref="dataSource"></property>
        </bean>
    
    
    </beans>
    

2、细节详解

  1. 声明配置:

    <aop:config></aop:config>
    
  2. aspect配置切面

    <aop:config>
        <aop:aspect id="txManagerAdvice" ref="txManager"></aop:aspect>
    </aop:config>
    

    id:给切面提供一个唯一标识。 ref:引用配置好的通知类 bean 的 id。

  3. aop:pointcut配置切入点表达式

    <aop:config>
        <aop:before method="beginTransaction" pointcut-ref="pt1"></aop:before>
                <aop:after-returning method="commit" pointcut-ref="pt1"></aop:after-returning>
                <aop:after method="release" pointcut-ref="pt1"></aop:after>
                <aop:after-throwing method="rollback" pointcut-ref="pt1"></aop:after-throwing>
    </aop:config>
    

    pointcut:切入点表达式,指对类的哪些方法进行增强

    method:指定通知中方法的名称。 pointct:定义切入点表达式 pointcut-ref:指定切入点表达式的引用

  4. aop:xxx配置通知类型

    1. aop:before:用于配置前置通知。指定增强的方法在切入点方法之前执行
    2. aop:after-throwing:用于配置异常通知
    3. aop:after-returning:用于配置后置通知
    4. aop:after:用于配置最终通知
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值