基于xml的AOP开发

开发明确

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

1、快速入门

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

  1. 导入AOP相关坐标

    
    <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.0modelVersion>
    
        <groupId>com.chengroupId>
        <artifactId>SpringartifactId>
        <packaging>pompackaging>
        <version>1.0-SNAPSHOTversion>
        <modules>
            <module>05spring-accountmodule>
            <module>06spring-aopmodule>
        modules>
    
        <dependencies>
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>4.13.1version>
            dependency>
    
            
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-contextartifactId>
                <version>5.0.2.RELEASEversion>
            dependency>
    
            
            <dependency>
                <groupId>org.aspectjgroupId>
                <artifactId>aspectjweaverartifactId>
                <version>1.8.4version>
            dependency>
    
            <dependency>
                <groupId>org.springframeworkgroupId>
                <artifactId>spring-testartifactId>
                <version>5.0.2.RELEASEversion>
            dependency>
    
            <dependency>
                <groupId>commons-dbutilsgroupId>
                <artifactId>commons-dbutilsartifactId>
                <version>1.4version>
            dependency>
    
            <dependency>
                <groupId>com.mchangegroupId>
                <artifactId>c3p0artifactId>
                <version>0.9.5.2version>
            dependency>
    
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>5.1.6version>
            dependency>
        dependencies>
    
        <properties>
            <maven.compiler.source>8maven.compiler.source>
            <maven.compiler.target>8maven.compiler.target>
        properties>
    
    project>
    
  2. 创建目标接口(可自行创建)和目标类

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

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

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

    
    <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">
    
    
         
        
        <bean id="accountService" class="com.chen.service.impl.AccountServiceImpl">
            
            <property name="accountDao" ref="accountDao">property>
        bean>
    
        
        
        <bean id="txManager" class="com.chen.utils.TransactionManager">
            
            <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>
    
        
        <bean id="accountDao" class="com.chen.dao.impl.AccountDaoImpl">
            
            <property name="runner" ref="runner">property>
            
            <property name="connectionUtils" ref="connectionUtils">property>
        bean>
    
        
        <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>
    
        
        <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:用于配置最终通知

你可能感兴趣的:(spring)