@around增强(也称通知)

概念:@around是一个介于@after和@before之间的注解,所以有人说around十分强大(为什么这么说?)因为以下三点

1.有人说他可以完全阻止目标方法执行,实际上是不写proceedindjoinpoint参数的proceed()就可以达成了,其实proceed()方法是在around注解中执行目标方法的关键词.

2.另外有人说around可以自己选择目标方法什么时候执行,实际上是通过proceed()方法,然后把增强方法放于proceed()前后就可以决定在proceed()之前执行还是之后执行.

3.另外还有人说他可以共享某种资源,其实际也是因为该proceed()里面塞入参数,该参数被所有目标的带参方法所用(同时也覆盖了带参方法本身的参数)

看上面的around简介还是不知道我在说什么?没关系,请看代码!


组件类

package test;

import org.springframework.stereotype.Component;


@Component
public class component {
    public component() {
        System.out.println("组件正在初始化");
    }
    public void test() {
        System.out.println("执行某事务...");

    }
}


切面类
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class aspect {
    @Around("execution(* test.*.*(..))")
    private void test(ProceedingJoinPoint pj) throws Throwable {
        System.out.println("在proceed()之前:输出的增强该组件");
        pj.proceed();
        System.out.println("在proceed()之后:输出的增强该组件");
    }
}


配置
<?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:context="http://www.springframework.org/schema/context"
        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/context
                             http://www.springframework.org/schema/context/spring-context-2.5.xsd
                             http://www.springframework.org/schema/aop
                             http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                            ">
        <context:component-scan base-package="test">
            <context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/>
        </context:component-scan>
        <aop:aspectj-autoproxy/>
</beans>


运行类

import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class ac {
        public static void main(String[] args) {
            ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
            component com=ac.getBean(component.class);
            com.test();
        }
}

运行结果

前面的这段代码足以说明1,2点,代码必须自己细细品尝才得出真知的,所以我不多说什么了.


第三种:执行方法前后共享某个状态的数据.即向proceed()里面放入一串字符串


这时候我的组件类改成带参的方法,并且多增加一个方法

@Component
public class component {
    public component() {
        System.out.println("组件正在初始化");
    }
    public void test(String name1) {
        System.out.println("参数1被改变:"+name1);
        System.out.println("执行某事务1...");
        
    }
    public void test1(String name2) {
        System.out.println("参数2被改变:"+name2);
        System.out.println("执行某事务2...");
        
    }
}

运行类


public class ac {
        public static void main(String[] args) {
            ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
            component com=ac.getBean(component.class);
            com.test("1");
            com.test1("2");

            System.out.println();
        }
}


红色部分是本来的形参

但是你看看结果



本来要输出1和2的现在都被一个数组给被替代了(这就是他们口中称的目标方法执行前后共享某种状态的数据)


今天学习around到此为止


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值