【spring_5_Spring的AOP技术-注解方式】

1. AOP注解方式入门程序

创建maven工程,导入坐标。编写接口,完成IOC的操作。步骤略。
编写切面类
给切面类添加注解 @Aspect,编写增强的方法,使用通知类型注解声明

1.配置xml扫描注解

<?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="com.aopImpl"></context:component-scan>
    
</beans>

2.配置注解

@Component
public class User {
    //连接点/切入点
    public void add(){
        System.out.println("add......");
    }
}

给切面类添加注解 @Aspect,编写增强的方法,使用通知类型注解声明

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

}

3.配置文件中开启自动代理

<?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="com.aopImpl"></context:component-scan>
    <!--开启Aspect生成代理对象-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>

4.通知类型注解
@Before – 前置通知
@AfterReturing – 后置通知
@Around – 环绕通知(目标对象方法默认不执行的,需要手动执行)
@After – 最终通知
@AfterThrowing – 异常抛出通知

@Component
@Aspect  //生成代理对象
public class UserProxy {
    //增强/通知  ---》前置通知
    @Before(value = "execution(* com.*.User.add(..))")
    public void before(){
        System.out.println("before.............");

    }

    // 环绕通知
    @Around(value = "execution(* com.*.User.add(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("before.............");
        //  执行被增强的方法
        proceedingJoinPoint.proceed();
        System.out.println("after.............");
    }

    // 最终通知
    @After(value = "execution(* com.*.User.add(..))")
    public void after() {

        System.out.println("after.............");
    }

    //异常通知
    @AfterThrowing(value = "execution(* com.*.User.add(..))")
    public void afterThrowing() {

        System.out.println("afterThrowing.............");
    }

    //后置通知
    @AfterReturning(value = "execution(* com.*.User.add(..))")
    public void afterReturning() {

        System.out.println("afterReturning.............");
    }
}

5.测试类

@Test
public void aopTest1(){
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    
    user.add();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值