Spring 自定义注解,配置简单日志注解
java在jdk1.5中引入了注解,spring框架也正好把java注解发挥得淋漓尽致。
下面会讲解Spring中自定义注解的简单流程,其中会涉及到spring框架中的AOP(面向切面编程)相关概念。
不清楚java注解的,可以先了解java自定义注解:Java自定义注解
一、创建自定义注解
requestUrl 为我们自定义的一个参数
package com.sam.annotation;
import java.lang.annotation.*;
/**
* @author sam
* @since 2017/7/13
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface MyLog {
String requestUrl();
}
二、解析注解
此处使用了spring的AOP(面向切面编程)特性
通过@Aspect注解使该类成为切面类
通过@Pointcut 指定切入点 ,这里指定的切入点为MyLog注解类型,也就是被@MyLog注解修饰的方法,进入该切入点。
- @Before 前置通知:在某连接点之前执行的通知,但这个通知不能阻止连接点之前的执行流程(除非它抛出一个异常)。
- @Around 环绕通知:可以实现方法执行前后操作,需要在方法内执行point.proceed(); 并返回结果。
- @AfterReturning 后置通知:在某连接点正常完成后执行的通知:例如,一个方法没有抛出任何异常,正常返回。
- @AfterThrowing 异常通知:在方法抛出异常退出时执行的通知。
- @After 后置通知:在某连接点正常完成后执行的通知:例如,一个方法没有抛出任何异常,正常返回。
package com.sam.annotation;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
/**
* @author sam
* @since 2017/7/13
*/
@Aspect //AOP 切面
@Component
public class MyLogAspect {
//切入点
@Pointcut(value = "@annotation(com.sam.annotation.MyLog)")
private void pointcut() {
}
/**
* 在方法执行前后
*
* @param point
* @param myLog
* @return
*/
@Around(value = "pointcut() && @annotation(myLog)")
public Object around(ProceedingJoinPoint point, MyLog myLog) {
System.out.println("++++执行了around方法++++");
String requestUrl = myLog.requestUrl();
//拦截的类名
Class clazz = point.getTarget().getClass();
//拦截的方法
Method method = ((MethodSignature) point.getSignature()).getMethod();
System.out.println("执行了 类:" + clazz + " 方法:" + method + " 自定义请求地址:" + requestUrl);
try {
return point.proceed(); //执行程序
} catch (Throwable throwable) {
throwable.printStackTrace();
return throwable.getMessage();
}
}
/**
* 方法执行后
*
* @param joinPoint
* @param myLog
* @param result
* @return
*/
@AfterReturning(value = "pointcut() && @annotation(myLog)", returning = "result")
public Object afterReturning(JoinPoint joinPoint, MyLog myLog, Object result) {
// HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
// HttpSession session = request.getSession();
System.out.println("++++执行了afterReturning方法++++");
System.out.println("执行结果:" + result);
return result;
}
/**
* 方法执行后 并抛出异常
*
* @param joinPoint
* @param myLog
* @param ex
*/
@AfterThrowing(value = "pointcut() && @annotation(myLog)", throwing = "ex")
public void afterThrowing(JoinPoint joinPoint, MyLog myLog, Exception ex) {
System.out.println("++++执行了afterThrowing方法++++");
System.out.println("请求:" + myLog.requestUrl() + " 出现异常");
}
}
三、使用自定义注解
在controller中直接使用注解@MyLog
package com.sam.controller;
import com.sam.annotation.MyLog;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* @author sam
* @since 2017/7/13
*/
@RestController
@RequestMapping(value = "/index")
public class IndexController {
@MyLog(requestUrl = "/index请求")
@RequestMapping(method = RequestMethod.GET)
public String index() {
return "index";
}
}
启动项目,访问 http://localhost:8080/index
结果
++++执行了around方法++++
执行了 类:class com.yfs.controller.IndexController 方法:public java.lang.String com.yfs.controller.IndexController.index() 自定义请求地址:/index请求
++++执行了afterReturning方法++++
执行结果:index
以上讲解了Spring实现自定义注解的简单流程,需要做更多的自定义操作,则需要在切面类里面进行编程了。
Spring 自定义注解,配置简单日志注解的更多相关文章
- Spring 自定义注解,结合AOP,配置简单日志注解 (转)
java在jdk1.5中引入了注解,spring框架也正好把java注解发挥得淋漓尽致. 下面会讲解Spring中自定义注解的简单流程,其中会涉及到spring框架中的AOP(面向切面编程)相关概念. ...
- 关于什么是SpringMVC,和SpringMVC基于xml配置、注解配置、纯注解配置
首先我们先要了解一下,什么是SpringMVC? SpringMVC是Spring框架内置的MVC的实现.SpringMVC就是一个Spring内置的MVC子框架,也就是说SpringMVC的相关包都 ...
- spring自定义自动配置注解
我们知道springboot自动配置@EnableAutoConfiguration是通过@Import(AutoConfigurationImportSelector.class)来把自动配置组件加 ...
- Spring的基础配置,以及注解
常用依赖 <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webm ...
- Spring 自定义标签配置
前景:经常使用一些依赖于Spring的组件时,发现可以通过自定义配置Spring的标签来实现插件的注入,例如数据库源的配置,Mybatis的配置等.那么这些Spring标签是如何自定义配置的?学习Sp ...
- 详解SSH注解配置,bean注解、事物注解等
使用过SSH注解的屌丝们都知道,要想使用注解需要在applicationContext.xml配置文件里面开启注解配置,开启方式如下:1.头部声明需加入xmlns:context="http ...
- Spring AOP配置简单记录(注解及xml配置方式)
在了解spring aop中的关键字(如:连接点(JoinPoint).切入点(PointCut).切面(Aspact).织入(Weaving).通知(Advice).目标(Target)等)后进行了 ...
- spring定时任务的配置式与注解式
在定时任务配置文件中添加内容: <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=&q ...
- 什么是基于Java的Spring注解配置? 给一些注解的例子?
基于Java的配置,允许你在少量的 Java注解 的帮助下,进行你的大部分Spring配置而非通过XML文件. 以@Configuration 注解为例,它用来标记类可以当做一个bean的定义,被Sp ...
随机推荐
- Jenkins修改管理员密码
前言:Jenkins修改管理员密码,我看了网上所有的教程,竟然全都是拿着一串已经加密好的111111的密文去替代config.xml文件里面的密码,然后大家的密码都是111111!我觉得这种做法实在太 ...
- ASP.NET Web API 自定义 HttpParameterBinding
背景 问题的起因是这样的.群里面一个哥们儿发现在使用 ASP.NET WebAPI 时,不能在同一个方法签名中使用多次 FromBodyAttribute 这个 Attribute .正好我也在用 W ...
- C#程序遍历数组A中所有元素
] { "a1","a2","a3","a4","a5"}; //第一种方法 ; i < A. ...
- Win7安装Docker
系统环境 1. windows 7 旗舰版64位 i5-2450M CPU 8G内存 2.支持“ Hardware Virtualization Technology”,并且,“virtualiza ...
- eclipse 好用的插件总结
改变eclipse的背景色,各种关键词颜色的插件. color theme. 在mac下最好是通过手动复制粘贴jar文件到, eclipse的plugins下面. 通过Install new soft ...
- 关于iphone点击readonly的input虚拟键盘不消失的情况
今天遇到了一个比较棘手的问题,事情是这样的: 咱有一个添加地址的页面,大概长这样: 收货地址后那个"请选择收货地址"是一个readonly的input, 咱一进页面,直接点击这个& ...
- Ajax请求,跨域小坑
今天在上班的时候,被坐在旁边项目经理叫过去问了一个Ajax请求跨域的问题,一开始没理解清楚也还有对这个没有理解的透,后面被打击的要死. 当时的需求是需要测试一个已发布的api接口,需要在本地写测试程序 ...
- 浅谈MVC Form认证
简单的谈一下MVC的Form认证. 在做MVC项目时,用户登录认证需要选用Form认证时,我们该怎么做呢?下面我们来简单给大家说一下. 首先说一下步骤 1.用户登录时,如果校验用户名密码通过后,需要调 ...
- Linux 进程,线程 -- (未完)
系统调用 Linux 将系内核的功能接口制作成系统调用, Linux 有 200 多个系统调用, 系统调用是操作系统的最小功能单元. 一个操作系统,以及基于操作系统的应用,都不能实现超越系统调用的功能 ...
- 逃跑(escape)
逃跑(escape) 时间限制: 3 Sec 内存限制: 128 MB 题目描述 输入 第一行是5个正整数,n,m,k,S,T,分别代表无向图点数,边数,蝙蝠的数量,二小姐所在起点的编号,目标点的编 ...