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; import Java.lang.annotation.documented; 导入Java.lang.annotation.element type; import Java.lang.annotation.retention; import Java.lang.annotation.retention policy; import Java.lang.annotation.target; import javax.persistence.inheritance;
@Documented//说明此注释包含在javadoc中
@ retention (retention policy.runtime ) /注释驻留在class字节代码文件中,可以在运行时通过反射检索
@target(elementtype.method )。
@Inheritance//说明子类可以从父类继承注释
公共@接口logannotation {
String value () default '-----AOP侦听执行完成! ----';
}
WebLogAspect.java
packagecom.pupeiyuan.aop; importjava.lang.reflect.Method; import javax.servlet.http.http servlet请求; importorg.apache.log4j.Logger; importorg.AspectJ.lang.join point; importorg.AspectJ.lang.annotation.aspect; importorg.AspectJ.lang.annotation.before; importorg.AspectJ.lang.annotation.pointcut; importorg.AspectJ.lang.reflect.methodsignature; importorg.spring framework.stereotype.com ponent; importorg.spring framework.web.context.request.requestcontextholder; importorg.spring framework.web.context.request.servletrequestattributes;
@Aspect
@ componentpublicclassweblogaspect { protectedstaticfinalloggerlogger=logger.getlogger (weblog aspect.class ); //定义切口
@pointcut () @ annotation (com.pupeiyuan.AOP.log annotation ) ) ) public voidannotationPointCut ) ) {
}
@before(annotationpointcut () ) publicvoiddobefore ) joinpointjoinpoint
MethodSignature m
ethodSignature=(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