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 自定义注解,配置简单日志注解的更多相关文章

  1. Spring 自定义注解,结合AOP,配置简单日志注解 (转)

    java在jdk1.5中引入了注解,spring框架也正好把java注解发挥得淋漓尽致. 下面会讲解Spring中自定义注解的简单流程,其中会涉及到spring框架中的AOP(面向切面编程)相关概念. ...

  2. 关于什么是SpringMVC,和SpringMVC基于xml配置、注解配置、纯注解配置

    首先我们先要了解一下,什么是SpringMVC? SpringMVC是Spring框架内置的MVC的实现.SpringMVC就是一个Spring内置的MVC子框架,也就是说SpringMVC的相关包都 ...

  3. spring自定义自动配置注解

    我们知道springboot自动配置@EnableAutoConfiguration是通过@Import(AutoConfigurationImportSelector.class)来把自动配置组件加 ...

  4. Spring的基础配置,以及注解

    常用依赖 <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webm ...

  5. Spring 自定义标签配置

    前景:经常使用一些依赖于Spring的组件时,发现可以通过自定义配置Spring的标签来实现插件的注入,例如数据库源的配置,Mybatis的配置等.那么这些Spring标签是如何自定义配置的?学习Sp ...

  6. 详解SSH注解配置,bean注解、事物注解等

    使用过SSH注解的屌丝们都知道,要想使用注解需要在applicationContext.xml配置文件里面开启注解配置,开启方式如下:1.头部声明需加入xmlns:context="http ...

  7. Spring AOP配置简单记录(注解及xml配置方式)

    在了解spring aop中的关键字(如:连接点(JoinPoint).切入点(PointCut).切面(Aspact).织入(Weaving).通知(Advice).目标(Target)等)后进行了 ...

  8. spring定时任务的配置式与注解式

    在定时任务配置文件中添加内容: <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=&q ...

  9. 什么是基于Java的Spring注解配置? 给一些注解的例子?

    基于Java的配置,允许你在少量的 Java注解 的帮助下,进行你的大部分Spring配置而非通过XML文件. 以@Configuration 注解为例,它用来标记类可以当做一个bean的定义,被Sp ...

随机推荐

  1. OpenCv关于灰度积分图的SSE代码学习和改进。

    最近一直沉迷于SSE方面的优化,实在找不到想学习的参考资料了,就拿个笔记本放在腿上翻翻OpenCv的源代码,无意中看到了OpenCv中关于积分图的代码,仔细研习了一番,觉得OpenCv对SSE的灵活运 ...

  2. 【web前端开发】浏览器兼容性处理

    1.居中问题div里的内容,IE默认为居中,而FF默认为左对齐,可以尝试增加代码margin: 0 auto;2.高度问题两上下排列或嵌套的div,上面的div设置高度(height),如果div里的 ...

  3. iOSNsPredicate Appium 定位元素

    Appium使用WebDriverAgent之后,新增了一种定位方法iOSNsPredicate,总结了一下使用方法: MobileElement photo = driver.findElement ...

  4. Java线程间通信

    1.由来 当需要实现有顺序的执行多个线程的时候,就需要进行线程通信来保证 2.实现线程通信的方法 wait()方法: wait()方法:挂起当前线程,并释放共享资源的锁 notify()方法: not ...

  5. 深入探索C++对象模型(四)

    Function语意学(The Semantics of Function) static member functions不可能做到的两点:(1)直接存取nonstatic数据,(2)被声明为con ...

  6. orcle :Could not initialize "D:\app\Administrator\product\11.2.0\dbhome_1\bin\oci.dll" Make sure you have the 32 bits Oracle Client installed.

    服务器重启后,数据库登录信息为空 错误信息: ---------------------------(Not logged on) - PL/SQL Developer---------------- ...

  7. STL的空间配置器std_alloc 笔记

    STL的空间配置器std_alloc 笔记 C++的内存分配基本操作是 ::operator new(),内存释放是 ::operator delete(),这里个全局函数相当于C的malloc和fr ...

  8. 导入网页数据到 Google Sheet

    数据没有用,我们需要的是数据所反映出来的东西.增长率,排名,占比等.而这些结果是通过分析数据得到的. 从网上搜集到数据后,导入到表格程序中便可以进行方便地分析处理了.下面介绍将网页中的表格数据导入到 ...

  9. python 标准库 -- subprocess

    subprocess 主要功能室执行外部的命令和程序 一个进程可 fork 一个子进程, 并让这个子进程 exec 另外一个程序. 在 python 中, 可以通过标准库中的 subprocess 包 ...

  10. Python 基础 (单、双引号区别) 不断补充

    最近开始学习Python ,一些小细节的东西不是很理解,所以就记录一下,方便自己以后查看. 我的Python环境: Mac pro 10.12.3,Python3.5 ,Pycharm 多句题外话:公 ...