(四) 基于XML声明式AOP,实现“攻击“前后的扩展

(四) 基于XML声明式AOP,实现"攻击"前后的扩展

实现思路
  1. 需要spring-aop-4.3.6.RELEASE.jaraspectjweaver-1.8.10.jar 这两个支持的jar包
  2. 创建增强类(切面)MyAspect,无需实现任何接口
  3. applicationContext.xml中引入aop的命名空间
  4. 配置applicationContext.xml中aspect信息
  5. 测试效果
1.需要spring-aop-4.3.6.RELEASE.jar 这个aop支持的jar包,里面包含了aspect信息

在这里插入图片描述

2. 创建增强类(切面)MyAspect,无需实现任何接口
package com.dacangshu.ext;

/**
 * @author dacangshu
 * @date 2021/5/11 15:15
 */
public class MyAspect {
    public void before(){
        System.out.println("之前做点什么....");
    }

    public void after(){
        System.out.println("之后做点什么....");
    }
}

3.applicationContext.xml中引入aop的命名空间
  1. xml头部增加如下内容:
 xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
  1. 完成版本
<?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="user" class="com.dacangshu.pojo.User">
        <property name="name" value="老谢"/>
        <property name="age" value="18"/>
    </bean>

    <!--  增强  -->
    <bean id="myAspect" class="com.dacangshu.ext.MyAspect"/>
</beans>
4. 配置applicationContext.xml中aspect信息
  1. 增加如下内容
<aop:config>
    <aop:aspect id="aspect" ref="myAspect">
        <!--     切入点       -->
        <aop:pointcut id="userPointcut" expression="execution(* com.dacangshu.pojo.*.*(..))"/>

        <!--      前置通知      -->
        <aop:before method="before" pointcut-ref="userPointcut"/>

        <!--      后置通知      -->
        <aop:after method="after" pointcut-ref="userPointcut"/>
    </aop:aspect>
</aop:config>
  1. 完整版
<?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="user" class="com.dacangshu.pojo.User">
        <property name="name" value="老谢"/>
        <property name="age" value="18"/>
    </bean>

    <!--  增强  -->
    <bean id="myAspect" class="com.dacangshu.ext.MyAspect"/>

    <aop:config>
        <aop:aspect id="aspect" ref="myAspect">
            <!--     切入点       -->
            <aop:pointcut id="userPointcut" expression="execution(* com.dacangshu.pojo.*.*(..))"/>

            <!--      前置通知      -->
            <aop:before method="before" pointcut-ref="userPointcut"/>

            <!--      后置通知      -->
            <aop:after method="after" pointcut-ref="userPointcut"/>
        </aop:aspect>
    </aop:config>
</beans>
5.测试效果
package com.dacangshu;

import com.dacangshu.pojo.User;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author dacangshu
 * @date 2021/5/11 13:34
 */
public class BootStrap {
    public static void main(String[] args) {
        // 创建上下文context对象,使用ClassPathXmlApplicationContext
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        // 从容器中获取id为iAttack的接口
        IAttack iAttack = context.getBean("user", IAttack.class);

        // 调用接口中的attack
        iAttack.attack();
    }
}

输出结果

之前做点什么....
name = 老谢, 发动一次攻击
之后做点什么....

注意: 仍然要使用接口

6. 其它JoinPoint可以传入
package com.dacangshu.ext;

import org.aspectj.lang.JoinPoint;

/**
 * @author dacangshu
 * @date 2021/5/11 15:15
 */
public class MyAspect {
    public void before(JoinPoint joinpoint) {
        System.out.println("之前做点什么....");
    }

    public void after(JoinPoint joinpoint) {
        System.out.println("之后做点什么....");
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值