Spring 自定义注解,带增强,环绕的注解
2018-01-20 19:03
183 查看
源文件地址:https://www.cnblogs.com/magicalSam/p/7161942.html
下面会讲解Spring中自定义注解的简单流程,其中会涉及到spring框架中的AOP(面向切面编程)相关概念。
不清楚java注解的,可以先了解java自定义注解:Java自定义注解
通过@Aspect注解使该类成为切面类
通过@Pointcut 指定切入点 ,这里指定的切入点为MyLog注解类型,也就是被@MyLog注解修饰的方法,进入该切入点。
@Before 前置通知:在某连接点之前执行的通知,但这个通知不能阻止连接点之前的执行流程(除非它抛出一个异常)。
@Around 环绕通知:可以实现方法执行前后操作,需要在方法内执行point.proceed(); 并返回结果。
@AfterReturning 后置通知:在某连接点正常完成后执行的通知:例如,一个方法没有抛出任何异常,正常返回。
@AfterThrowing 异常通知:在方法抛出异常退出时执行的通知。
@After 后置通知:在某连接点正常完成后执行的通知:例如,一个方法没有抛出任何异常,正常返回。
以上讲解了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(); c6df //拦截的方法 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属性注入(Ioc的DI)总结】:注解方式属性注入,属性名任意.=for理解:Aop注入代理对象时,注入被增强类对象时,属性名为proxy(自定义)。
- 自己试验在spring的环绕通知里获取目标对象的类名和目标方法的参数类名,用于根据自定义注解判断访问权限,有没有更好的办法,高手指点一下
- spring 自定义注解annotation+aspect 环绕通知配置对dubbo的consumer监控报警
- spring切面:注解:环绕增强
- Spring自定义注解
- J2EE项目使用自定义注解实现基于SpringMVC + Mybatis + Mysql的读写分离
- Spring AOP 自定义注解的实现
- Spring自定义注解+Aop记录用户操作日志
- Spring 中使用@Aspect 控制自定义注解
- SpringBoot自定义参数注解
- Springboot + Freemarker项目中使用自定义注解
- dubbo--自定义注解spring扫描
- 用AOP拦截自定义注解并获取注解属性与上下文参数(基于Springboot框架)
- 使用Spring自定义注解生产Http接口描述信息
- Spring 基于 Aspect 注解的增强实现
- 深入Spring:自定义注解加载和使用
- 自定义注解的spring注入问题
- 在spring中使用自定义注解注册监听器
- 在spring中使用自定义注解注册监听器的方法
- 使用Spring配置shiro时,自定义Realm中属性无法使用注解注入解决办法