SPRING03_AOP的概述、动态代理、cglib代理、相关概念、基于xml配置、基于注解配置(三)

⑤. 基于XML的AOP开发


  • ①. 导入依赖


<properties>
        <spring.version>5.0.5.RELEASE</spring.version>
    </properties>
    <!--导入spring的context坐标,context依赖core、beans、expression-->
    <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
        <!-- aspectj的织入 -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.13</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.1.0.RELEASE</version>
        </dependency>
    </dependencies>


②. 创建目标接口和目标类(内部有切点)


    //创建目标接口和目标类(内部有切点)
    public interface TargetInterface {
        public void method();
    }
    
    public class Target implements TargetInterface {
        @Override
        public void method() {
            System.out.println("Target running....");
        }
    }
    //创建切面类(内部有增强方法)
    public class MyAspect {
        //前置增强方法
        public void before(){
            System.out.println("前置代码增强.....");
        }
    }


③. 基于xml的形式进行配置


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--目标对象-->
    <bean id="target" class="com.xiaozhi.aop.Target"/>

    <!--切面对象-->
    <bean id="myAspect" class="com.xiaozhi.aop.MyAspect"></bean>

    <!--配置织入:告诉Spring框架,哪些方法(切点)需要进行哪些增强(前置 | 后置)-->
    <aop:config>
          <!--声明切面[告诉spring哪个类是切面类]-->
          <aop:aspect ref="myAspect">
          <!--切面:切入点+通知-->
              <!--通知
              method:切面类中增强的方法
              pointcut:切入点表达式
              -->
              <!--前置通知-->
              <aop:before method="before" pointcut="execution(public void com.xiaozhi.aop.Target.save())"></aop:before>
          </aop:aspect>
    </aop:config>
</beans>


④. 进行测试


    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:applicationContext.xml")
    public class AopTest {
        @Autowired
        private TargetInterface target;
        @Test
        public void test1(){
            target.method();
        }
    }
上一篇:SPRING03_AOP的概述、动态代理、cglib代理、相关概念、基于xml配置、基于注解配置(四)


下一篇:SPRING03_AOP的概述、动态代理、cglib代理、相关概念、基于xml配置、基于注解配置(一)