AOP操作 - AspectJ注解


一、创建类,在类中定义方法

public class User {
    public void add(){
        System.out.println("add() in User...");
    }
}

二、创建增强类,编写增强逻辑

public class UserProxy {
//    前置通知
    public void before(){
        System.out.println("before......");
    }
}

三、进行通知的配置

3.1 在Spring配置文件中,开启注解扫描。

<?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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--开启扫描注解-->
    <context:component-scan base-package="cn.ac.spring5.aspectj"></context:component-scan>
</beans>

3.2 使用注解创建User、UserProxy对象

@Component
public class User {
    public void add(){
        System.out.println("add() in User...");
    }
}
@Component
public class UserProxy {
//    前置通知
    public void before(){
        System.out.println("before......");
    }
}

3.3 在增强类上面添加注解@Aspect

@Component
@Aspect //生成代理对象
public class UserProxy {
//    前置通知
    public void before(){
        System.out.println("before......");
    }
}

3.4 在spring配置文件中开启生成代理对象。

<?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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--开启扫描注解-->
    <context:component-scan base-package="cn.ac.spring5.aspectj"></context:component-scan>
<!--开启AspectJ生成代理对象-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

四、配置不同类型的通知

在增强类中,在作为通知方法上面添加通知类型注解,使用切入点表达式配置。

@Component
@Aspect
public class UserProxy {
//    前置通知
//    @Before 注解表示作为前置通知
    @Before(value = "execution(* cn.ac.spring5.aspectj.User.add(..))")
    public void before(){
        System.out.println("before......");
    }

//    后置通知(返回通知)
    @AfterReturning(value = "execution(* cn.ac.spring5.aspectj.User.add(..))")
    public void afterReturning(){
        System.out.println("afterReturning......");
    }
}

五、 相同切入点抽取

四 写通知时候,value值会有很多重复的,所以可以抽取如下

@Component
@Aspect //生成代理对象
public class UserProxy {

//  相同切入点抽取
    @Pointcut(value = "execution(* cn.ac.spring5.aspectj.User.add(..))")
    public void pointcut(){

    }
//    前置通知
//    @Before 注解表示作为前置通知
    @Before(value = "pointcut()")
    public void before(){
        System.out.println("before......");
    }

//    后置通知(返回通知)
    @AfterReturning(value = "pointcut()")
    public void afterReturning(){
        System.out.println("afterReturning......");
    }
}

六、 多个增强类对同一个方法进行增强,可以设置增强类的优先级

在增强类上面添加注解 @Order(数字类型值),数字类型值越小,优先级越高。


@Componet
@Aspect
@Order(1)
public class PersonProxy{
	...
}

七、完全注解开发

不需要创建xml配置文件,创建配置类

@Configuration
@ComponentScan(basePackages={"cn.ac.spring5"})
@EnableAspectJAutoProxy(proxyTargetClass=true)
public class ConfigAop{
	...
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值