Spring-AOP 应用之注解

参考文档:Spring官网 

写在前面:spring主要提供了两种方式的AOP实现:1,使用注解方式‘2,使用xml配置方式。本文介绍注解方式的使用。

分析:Spring中使用的切面编程,是对AspectJ的一封装,只取用了其中一部分的功能,目前,Spring只支持对方法的切面编程,不支持对其他的切面编程,而AspectJ提供了很全面的AOP支持;Spring也可以使用全面的Aspectj的切面编程,具体配置,可以参考官网,如果有需要,读者可以留言,后续对其进行介绍和解析。

一,导入Jar包:

Spring相关:bean,core,context,aop ,expression,版本:4.3.18;AspectJ相关:aspectjrt,aspectjweaver,版本:1.9.1

二,第一接口和实现类:

public interface RoleService {
    public void printRole(String msg);
    public void printRole(String name,String sex);
}

@Component
public class RoleServiceImpl implements RoleService {
    //切点
    public void printRole(String msg) {
        System.out.println(msg);
    }
    public void printRole(String name, String sex) {
        System.out.println("自己的方法"+name+""+sex);
    }
}

三,定义切面类:

package com.blog.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

//切面
@Component
@Aspect 
public class MyAspect {
    //申明一个切点
    @Pointcut("execution(* com.blog.aop.RoleServiceImpl.printRole())")
    //本身没起任何作用
    public void print() {
        System.out.println("print");
    }
    /**
     * 带参数的pointCut
     * @param name
     * @param sex
     */
    @Pointcut("execution(* com.blog.aop.RoleServiceImpl.printRole(..))" +"&&args(name,sex)")
    public void cutPoint(String name ,String sex) {
        
    }
    /**
     * 带参数的advice
     * @param name
     * @param sex
     */
    @Before("cutPoint(name,sex)")
    public void cutBefore(String name,String sex) {
        System.out.println("#########cutPoint Before##########"+name);
    }
    //通知
    @Before("print()")  //连接点
    public void before() {
        System.out.println("before..........进入方法之前执行");
    }
    
    @After("print()")
    public void after() {
        System.out.println("after..........不管是否异常都会执行");
    }
    
    @AfterReturning("print()")
    public void afterReturning() {
        System.out.println("afterReturning..........异常之后就不能执行了");
    }
    
    @AfterThrowing("print()")
    public void afterThrowing() {
        System.out.println("afterThrowing..........异常之后执行");
    }
    
    @Around("print()")
    public void around(ProceedingJoinPoint jp) {
        System.out.println("around..........before");
        try {
            jp.proceed();
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("around..........after");
    }
}
 

四,定义配置类:

@Configuration
@EnableAspectJAutoProxy              //启动AspectJ的自动代理【必须】
@ComponentScan("com.blog.aop")  //配置自动扫描
public class AOPConfig {
//    @Bean
//    public MyAspect getMyAspect() {
//        return new MyAspect();
//    }
}

五,主方法,测试类:

public class AOPTestMain {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(AOPConfig.class);
        RoleService service = (RoleService)context.getBean(RoleService.class);
//        service.printRole("张三");
        service.printRole("张三","man");
    }
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值