aop实践

AOP在Java项目中使用实例

AOP在实际使用时关注什么

  1. 不需要关注你要用的是jdk动态代理还是CGLIB,spring在底层会自动选择,不用考虑目标对象是不是接口
  2. 如果是SSM项目,那在写配置文件的时候要注意启动aop的配置标签要写在spring-mvc.xml 中,不要写到ApplicationContext.xml或者其他的配置文件中,否则就可能切面不生效。ssm项目目录结构请看如下博客。链接: 链接
  3. aop可以和自定义注解一起使用。比如说自定义增强日志功能等。和注解一起使用的情况可以这么理解,在哪加注解,哪就是切点。
  4. 通知的执行顺序:**加粗样式**
  5. 切点表达式的用法用到时查一下就可以了,在这里不详细介绍,如有需求自行百度。

操作步骤

aop在ssm中的使用

  1. 导入依赖,在项目中加入依赖
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>
  1. 修改配置文件(修改spring-mvc.xml)
    <!--启动aop-->
<aop:aspectj-autoproxy />
    <!--指定扫描范围-->
<context:component-scan base-package="com.xxx.扫描范围"></context:component-scan>
  1. 创建切面类,编写切面相关代码
@Aspect
@Component
public class LoginRenewalAspect {
    //这个地方指定切点表达式,如果直接指点切点,用execution();如果使用自定义注解那么用@annotation()
    @Pointcut("execution(* com.xxx.xxx.modules..*Controller.*(..))")
    //@Pointcut("@annotation(com.xxx.xxx.aspect.注解名)")
    private void pointcut() {
    }
    //SpringAOP中,通过advice定义横切逻辑,Spring支持的5中类型的Advice 通知类型	连接点	
      //实现接口
		//前置通知	方法前	org.springframework.aop.MethodBeforeAdvice
		//后置通知	方法后	org.springframework.aop.AfterReturningAdvice
		//环绕通知	方法前后	org.springframework.aop.MethodInterceptor
		//异常抛出通知	方法抛出异常	org.springframework.aop.ThrowsAdvice
		//引介通知	类中增加新的方法属性	org.springframework.aop.IntroductionInterceptor
//此处用后置通知举例。
  @After("pointcut()")
    public void After() {
            //增强的业务逻辑。。。。。
    }
}
  1. 启动项目,启动项目后调用目标对象的方法会在此基础执行增强方法。

aop在spring boot中的使用(aop + 注解 的方式实现)

  1. 引入依赖
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
  1. 创建自定义注解
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LoginRenewal {
    String value() default "";
}
  1. 创建切面类,编写切面相关代码
@Aspect
@Component
public class LoginRenewalAspect {
    //这个地方指定切点表达式,如果直接指点切点,用execution();如果使用自定义注解那么用@annotation()
    @Pointcut("@annotation(com.xxx.xxx.aspect.LoginRenewal )")
    private void pointcut() {
    }
//此处用前置通知举例。
  @Before("pointcut()")
    public void Before() {
            //增强的业务逻辑。。。。。
    }
}
  1. 在目标对象的方法上面加上自定义的注解
@RestController
@RequestMapping("/aop")
public class AopController {

    @GetMapping("/testHello")
    @LoginRenewal
    public String testHello(String params){
        return "Hello World";
    }
}
  1. 重启项目,访问该请求,会执行增强后的方法。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值