Spring注解驱动-注解实现AOP切面编程

注解实现AOP

需求:在指定包下的所有类中的有方法都加上前置和后置通知。
1.AOP类,使用的注解@Aspect表示当前的类为AOP类

@Aspect
public class LogAop {

        @Before("execution(* *..service.*.*(..))")//匹配报下的所有方法
        public void registerUserLogin(){
            System.out.println("开始记录用户登录的情况");
        }


        @After("execution(* *..service.*.*(..))")
        public void doAfter(){
            System.out.println("用户退出记录登录时间");
        }
}

2.在配置类中注册AOP类使用@Bean标签进行注册

@Bean
public LogAop logAop(){
    return new LogAop();
}

3.在配置上加上注解@EnableAspectJAutoProxy开启AOP注解驱动

@Configuration
@Import(value = {Color.class, Red.class, MyImportBean.class, UserLoginService.class})
@ComponentScan(value = {"com.itguigu.aop"})
@EnableAspectJAutoProxy//开启AOP注解驱动
public class MyConfiguration2 {
    /**
     * 需求根据操作系统进行创建对象
     *
     */
@Test
public void test5() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration2.class);
    UserLoginService bean = (UserLoginService) context.getBean("com.itguigu.service.UserLoginService");
    bean.doSome();
    //printName(context);
}

5.结果,在指定包下的所有类中的所有方法都加上了前置和后置通知

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值