java spring自定义注解_java/springboot自定义注解实现AOP

java注解

即是注释了,百度解释:也叫元数据。一种代码级别的说明。 个人理解:就是内容可以被代码理解的注释,一般是一个类。

元数据

也叫元注解,是放在被定义的一个注解类的前面 ,是对注解一种限制。

谈下这两个: @Retention 和 @Target

@Retention :用来说明该注解类的生命周期。它有以下三个参数:

RetentionPolicy.SOURCE  : 注解只保留在源文件中

RetentionPolicy.CLASS  : 注解保留在class文件中,在加载到JVM虚拟机时丢弃

RetentionPolicy.RUNTIME  : 注解保留在程序运行期间,此时可以通过反射获得定义在某个类上的所有注解。

@Target :  用来说明该注解可以被声明在那些元素之前。

ElementType.TYPE:说明该注解只能被声明在一个类前。

ElementType.FIELD:说明该注解只能被声明在一个类的字段前。

ElementType.METHOD:说明该注解只能被声明在一个类的方法前。

ElementType.PARAMETER:说明该注解只能被声明在一个方法参数前。

ElementType.CONSTRUCTOR:说明该注解只能声明在一个类的构造方法前。

ElementType.LOCAL_VARIABLE:说明该注解只能声明在一个局部变量前。

ElementType.ANNOTATION_TYPE:说明该注解只能声明在一个注解类型前。

ElementType.PACKAGE:说明该注解只能声明在一个包名前。

实现自定义注解AOP:

LogAnnotation.java

packagecom.pupeiyuan.aop;importjava.lang.annotation.Documented;importjava.lang.annotation.ElementType;importjava.lang.annotation.Retention;importjava.lang.annotation.RetentionPolicy;importjava.lang.annotation.Target;importjavax.persistence.Inheritance;

@Documented//说明该注解将被包含在javadoc中

@Retention(RetentionPolicy.RUNTIME)//注解会在class字节码文件中存在,在运行时可以通过反射获取到

@Target(ElementType.METHOD)

@Inheritance//说明子类可以继承父类中的该注解

public @interfaceLogAnnotation {

String value()default "-----AOP拦截执行完毕!----";

}

WebLogAspect.java

packagecom.pupeiyuan.aop;importjava.lang.reflect.Method;importjavax.servlet.http.HttpServletRequest;importorg.apache.log4j.Logger;importorg.aspectj.lang.JoinPoint;importorg.aspectj.lang.annotation.Aspect;importorg.aspectj.lang.annotation.Before;importorg.aspectj.lang.annotation.Pointcut;importorg.aspectj.lang.reflect.MethodSignature;importorg.springframework.stereotype.Component;importorg.springframework.web.context.request.RequestContextHolder;importorg.springframework.web.context.request.ServletRequestAttributes;

@Aspect

@Componentpublic classWebLogAspect {protected static final Logger logger = Logger.getLogger(WebLogAspect.class);//定义一个切入点

@Pointcut("@annotation(com.pupeiyuan.aop.LogAnnotation)")public voidannotationPointCut(){

}

@Before("annotationPointCut()")public voiddoBefore(JoinPoint joinPoint) {

MethodSignature methodSignature=(MethodSignature)joinPoint.getSignature();

Method method=methodSignature.getMethod();

LogAnnotation annotation= method.getAnnotation(LogAnnotation.class);//记录请求到达时间//接收到请求,记录请求内容

ServletRequestAttributes attributes =(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

HttpServletRequest request=attributes.getRequest();//记录下请求内容

logger.info("URI : " +request.getRequestURI());

logger.info("URL : " +request.getRequestURL());

logger.info("HTTP_METHOD : " +request.getMethod());

logger.info("IP : " +request.getRemoteAddr());

logger.info(annotation.value());

}

}

controller

packagecom.pupeiyuan.controller;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.context.annotation.Scope;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.web.bind.annotation.RestController;importorg.springframework.web.servlet.ModelAndView;importcom.github.pagehelper.PageHelper;importcom.pupeiyuan.aop.LogAnnotation;importcom.pupeiyuan.bean.NhReportStatusHistory;importcom.pupeiyuan.common.controller.BaseController;importcom.pupeiyuan.core.DataSourceKey;importcom.pupeiyuan.core.DynamicDataSourceContextHolder;importcom.pupeiyuan.services.NhReportService;/***@authorpypua

* @date 2018年8月30日 上午9:21:20

**/@Controller

@RequestMapping("burket")

@Scope("prototype")public class BurketController extendsBaseController {//services层注入

@Autowired NhReportService nhReportService;

@LogAnnotation

@RequestMapping(value= "/burketList", method ={RequestMethod.GET,RequestMethod.POST})publicModelAndView burketList(HttpServletRequest request,

HttpServletResponse response

)throwsException {

System.out.println("hello,springboot");//参数容器

Map params = new HashMap();

PageHelper.startPage(1, 2);

DynamicDataSourceContextHolder.set(DataSourceKey.DB_SLAVE1);

List findList =nhReportService.findList(params);

ModelAndView modelAndView= newModelAndView();

modelAndView.setViewName("burketList");

modelAndView.addObject("list", findList);returnmodelAndView;

}

}

效果

spring拦截器----方法执行之前---------

2018-12-07 16:48:53.769 INFO 92292 --- [nio-8082-exec-4] com.pupeiyuan.aop.WebLogAspect : URI : /burket/burketList

2018-12-07 16:48:53.778 INFO 92292 --- [nio-8082-exec-4] com.pupeiyuan.aop.WebLogAspect : URL : http://localhost:8082/burket/burketList

2018-12-07 16:48:53.778 INFO 92292 --- [nio-8082-exec-4] com.pupeiyuan.aop.WebLogAspect : HTTP_METHOD : GET

2018-12-07 16:48:53.778 INFO 92292 --- [nio-8082-exec-4] com.pupeiyuan.aop.WebLogAspect : IP : 0:0:0:0:0:0:0:1

2018-12-07 16:48:53.778 INFO 92292 --- [nio-8082-exec-4] com.pupeiyuan.aop.WebLogAspect : -----AOP拦截执行完毕!----

hello,springboot

2018-12-07 16:48:53.788 INFO 92292 --- [nio-8082-exec-4] c.p.core.DynamicRoutingDataSource : 当前数据源:{}DB_SLAVE1

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值