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. 如何在Java中调用Python代码

    有时候,我们会碰到这样的问题:与A同学合作写代码,A同学只会写Python,而不会Java, 而你只会写Java并不擅长Python,并且发现难以用Java来重写对方的代码,这时,就不得不想方设法“调 ...

  2. C#中的委托(一)

    一.委托 把方法作为参数传给其他方法: 二.声明委托 在C#中使用一个类时,分两个阶段.首先,需要去定义一个类,然后实例化类的一个对象(只需要静态方法除外). 使用委托也需要经过这2个步骤,首先必须定 ...

  3. js设置,获取,删除Cookie

    //JS操作cookies方法! //写cookies function setCookie(name,value) {     var Days = 30;     var exp = new Da ...

  4. 一天搞定CSS:定位position--17

    1.定位取值概览 2.相对定位relative <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...

  5. 网络编程应用:基于TCP协议【实现一个聊天程序】

    要求: 基于TCP协议实现一个聊天程序,客户端发送一条数据,服务器端发送一条数据 客户端代码: package Homework1; import java.io.IOException; impor ...

  6. JAVAEE学习——struts2_02:结果跳转方式、访问servletAPI方式、获得参数以及封装和练习:添加客户

    一.结果跳转方式 <action name="Demo1Action" class="cn.itheima.a_result.Demo1Action" m ...

  7. PyQt5环境搭建及cx_freeze打包exe

    Python的图形库也有好几个,Qt文档和使用面还是要广一些. 打包成可执行文件的也有好几个,PyInstaller用的比较多,但是PyInstaller目前还不支持Python3.6(开发版支持3. ...

  8. Elasticsearch VS Solr

    最近公司用到了ES搜索引擎,调研发现大公司常用的搜索引擎还有Solr. 鉴于 Lucene 强大的特性和稳定性,有很多种基于 Lucene 封装的企业级搜索平台.其中最流行有两个:Apache Sol ...

  9. PHP中单引号和双引号的区别

    双引号里面的字段会经过编译器解释,然后再当作HTML代码输出:单引号里面的不进行解释,直接输出: PHP引号使用原则 1.字符串的值用单引号 2.PHP中尽量用单引号,HTML代码全部用双引号 3.在 ...

  10. Zab: A simple totally ordered broadcast protocol(译)

    摘要 这是一个关于ZooKeeper正在使用的全序广播协议(Zab)的简短概述.它在概念上很容易理解,也很容易实现,并且提供很高的性能.在这篇文章里,我们会呈现ZooKeeper在Zab上的需求,也会 ...