AOP&注解式拦截&spring aop

6 篇文章 0 订阅
2 篇文章 0 订阅

思考以下方式在记录日志方式中的使用.(将日志当成一个切面)
日志的类型信息,详细信息等,定义在注解中.
使用注解的时候,填写上.
在aop中,记录日志的时候,将这些信息写到日志中

  • 编写拦截规则的注解
package com.hgh.springboot_1.test1_3_2;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
 * 注解本身是没有功能的,就和xml一样.
 * 注解和xml都是一种元数据,元数据即解释数据的数据,这就是所谓配置.
 * @author Administrator
 *
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Action {

    String name();
}
  • 使用注解的被拦截类
package com.hgh.springboot_1.test1_3_2;

import org.springframework.stereotype.Service;

/**
 * 使用注解的被拦截类
 * @author 13076
 *
 */
@Service
public class DemoAnnotationService {

    @Action(name="注解式拦截的add操作")
    public void add(){
        System.out.println("执行了DemoAnnotationService的add方法");
    }
}
  • 使用方法规则的被拦截类
package com.hgh.springboot_1.test1_3_2;

import org.springframework.stereotype.Service;

/**
 * 使用方法规则的被拦截类
 * @author 13076
 *
 */
@Service
public class DemoMethodServices {

    public void add(){

    }
}
  • 切面
package com.hgh.springboot_1.test1_3_2;

import java.lang.reflect.Method;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Aspect //声明一个切面
@Component
public class LogAspect {

    //声明一个切点
    @Pointcut("@annotation(com.hgh.springboot_1.test1_3_2.Action)")
    public void annotationPoinCut(){
    }
    /**
     * 基于注解的拦截
     * @param joinPoint
     */
    //声明一个建言
    @After("annotationPoinCut()")
    public void after(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        Action action = method.getAnnotation(Action.class);
        //通过反射可以获得注解上的属性,然后做日志记录相关的操作,下面的相同.
        System.out.println("注解式拦截: " + action.name());

    }
    /**
     * 基于方法的拦截
     * @param joinPoint
     */
    @Before("execution(* com.hgh.springboot_1.test1_3_2.DemoMethodServices.*(..))")
    public void before(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println("方法规则拦截: " + method.getName());
    }
}
  • 配置类
package com.hgh.springboot_1.test1_3_2;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configurable
@ComponentScan("com.hgh.springboot_1.test1_3_2")
//开启spring对aspectj的支持
@EnableAspectJAutoProxy
public class AopConfig {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class);
        DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class);
        DemoMethodServices demoMethodServices = context.getBean(DemoMethodServices.class);
        demoAnnotationService.add();
        demoMethodServices.add();
        context.close();
    }
}

输出

执行了DemoAnnotationService的add方法
注解式拦截: 注解式拦截的add操作
方法规则拦截: add

Pom.xml

这里写代码片
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值