SpringBoot自定義註解攔截器,並根據環境變數選擇初始化
TimeStamp.java 自定義註解
import java.lang.annotation.*;
/**
* @author Meng.Liu
* @create 2017-10-30 11:30
**/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface TimeStamp {
/**
* 介面的日誌標識
* @return
*/
String identity();
}
TimeStampAspect.java 註解攔截器
import cn.com.flaginfo.platform.umsapp.aop.parse.AnnotationParseUtils;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj .lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
/**
* @author Meng.Liu
* @create 2017-10-30 11:35
**/
@Aspect
@Component
@Profile("pref" )
public class TimeStampAspect {
private static final Logger logger = LoggerFactory.getLogger(TimeStampAspect.class);
@Pointcut("@annotation(cn.com.flaginfo.platform.umsapp.aop.TimeStamp)")
public void annotationTimeStamp(){ }
@Around("localService() ")
public Object logTimeStamp(ProceedingJoinPoint pjp) throws Throwable {
String className = pjp.getTarget().getClass().getName();
MethodSignature methodSignature = (MethodSignature)pjp.getSignature();
Method method = methodSignature.getMethod();
String identity = null;
try {
identity = AnnotationParseUtils.timeParse(method);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
identity = StringUtils.isEmpty(identity) ? className + "." + method.getName() : identity;
long start = System.currentTimeMillis();
try {
return pjp.proceed();
} catch (Throwable throwable) {
throw throwable;
}finally {
long take = System.currentTimeMillis() - start;
logger.info("@[email protected] : [{}] took [{}ms].", identity, take);
}
}
}
相關推薦
SpringBoot自定義註解攔截器,並根據環境變數選擇初始化
TimeStamp.java 自定義註解 import java.lang.annotation.*; /** * @author Meng.Liu * @create 2017-10-30
自定義註解攔截器,防止表單重複提交
1.自定義註解 package com.paotui.util; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import ja
java的 自定義註解攔截器
自定義的攔截器,是看了別人的文章,做的精簡,所以算是轉載 在專案中,右鍵,建立@ interface 這就是註解,在註解上加上 兩個註解 ,對這個註解進行限定 @Target(value=ElementType.METHOD)//定義了只能用於方法上 @Retention (Reten
Springboot中自定義springmvc攔截器;HandlerInterceptor中preHandle,postHandle,afterCompletion解析
要實現自定義HandlerInterceptor 。通過 WebMvcConfigurer 的實現類屬性注入,完成springboot自定義攔截 自定義一個攔截器; package com.baidu.
SpringBoot中實現攔截器, 並實現對404和500等錯誤的攔截。
今天給大家介紹一下SpringBoot中攔截器的用法,相比Struts2中的攔截器,SpringBoot的攔截器就顯得更加方便簡單了。只需要寫幾個實現類就可以輕輕鬆鬆實現攔截器的功能了,而且不需要配置任何多餘的資訊,對程式設計師來說簡直是一種福利啊。廢話不多說,下面開始介紹攔
SpringBoot使用自定義Mybatis攔截器
註解 添加 attr 自動 自定義攔截器 enc 攔截 number ring 開發過程中經常回需要對要執行的sql加以自定義處理,比如分頁,計數等。通過 MyBatis 提供的強大機制,使用插件是非常簡單的,只需實現 Interceptor 接口,並指定想要攔截的方法
使用自定義註解和反射 ,自動生成查詢語句
runt entity forname == rop ava stat pri string 1.自定義表名註解 package com.lf.annotation; import java.lang.annotation.ElementType; import jav
springboot自定義註解校驗時出現的異常
異常資訊: javax.validation.ConstraintDefinitionException: HV000074 原因: 在約束註釋中它是關於groups()和payload()缺失的,只需新增這兩行就可以了: Class<?>[] groups() de
spring boot 自定義登入攔截器
最近在努力學習spring boot中,這裡記錄一下攔截器的相關知識,在spring boot中,寫一個自定義的攔截器類,用於攔截不通過登入之後而進行的非法訪問,攔截的操作,也是日常專案中,都要用到的,不多說了,看正文。 &n
Springboot自定義註解封裝快取操作
通常,我們為了避免頻繁的查詢訪問資料庫或者第三方介面,會把查詢結果快取到redis或者memcached之類的nosql資料庫中,避免資料庫或者網路開銷過大導致程式效率太低或者雪崩效應,但是程式碼中頻繁的操作快取,會讓程式碼過於冗長,可以通過自定義註解的方式封裝快取的操作,使程式碼更簡潔,話
laravel-admin 自定義匯出excel功能,並匯出圖片
https://www.jianshu.com/p/91975f66427d 最近用laravel-admin在做一個小專案,其中用到了excel匯出功能。 但是laravel-admin自帶的匯出功能不帶圖片,並且匯出的資料有很多冗餘的欄位,並非我所需要的功能。 所以參考官方文件
Json過濾器(基於spring自定義註解的方式,對欄位進行過濾)
在實際的開發過程中,會經常遇到如下情景: 後臺需要給前端返回JSON資料,但是查詢出來返回的資料裡面有很多屬性是不需要的 本文通過自定義註解的方式進行實現,對response進行攔截,通過註解引數,設定欄位資訊(即,過濾哪些欄位,保留哪些欄位),並將bean自動封裝
SpringBoot自定義註解
我們經常使用自定義註解和AOP實現操作日誌、許可權、統計執行時間等功能,本文記錄使用SpringBoot實現自定義註解 本文在一個ssm專案的基礎上進行的 SpringBoot搭建SSM專案:https://blog.csdn.net/qidasheng2012/article/de
如何自定義一個攔截器
1、自定義攔截器類,實現HandlerInterceptor介面,並重寫抽象方法進行攔截器的攔截邏輯 // return true表示放行, return false表示攔截 public class Interceptor1 implements HandlerInterceptor{
SpringBoot自定義註解Annotation的使用
一. 首先匯入相關包, 在build.gradle中新增 dependencies { //支援AOP compile('org.springframework.boot:sprin
java/springboot自定義註解實現AOP
java註解 即是註釋了,百度解釋:也叫元資料。一種程式碼級別的說明。 個人理解:就是內容可以被程式碼理解的註釋,一般是一個類。 元資料 也叫元註解,是放在被定義的一個註解類的前面 ,是對註解一種限制。 談下這兩個: @Retention 和 @Target
Mybatis自定義SQL攔截器
本部落格介紹的是繼承Mybatis提供的Interface介面,自定義攔截器,然後將專案中的sql攔截一下,列印到控制檯。 先自定義一個攔截器 package com.muses.taoshop.common.core.database.config; import org.apache.commons
struts2自定義登陸攔截器
自定義登陸攔截器,實現功能為:對於Session域中未存在使用者資訊時(即未登入時),使用者訪問其他action時,強行跳轉到login頁面進行登陸。 攔截器程式碼如下: public class BOSLoginInterceptor extends MethodFilterInter
自定義應用攔截器
建立一個類 繼承interceptor post public class MyInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOExc
day03-自定義struts2攔截器
1. 攔截器的概述 * 攔截器就是AOP(Aspect-Oriented Programming)的一種實現。(AOP是指用於在某個方法或欄位被訪問之前,進行攔截然後在之前或之後加入某些操作。) * 過濾器:過濾從客服端傳送到伺服器端請求的 * 攔截器:攔截對目標Actio