Spring-AOP 基于注解的实现
一、AOP:
是对OOP编程方式的一种补充。翻译过来为“面向切面编程”。
可以理解为一个拦截器框架,但是这个拦截器会非常武断,如果它拦截一个类,那么它就会拦截这个类中的所有方法。如对一个目标列的代理,增强了目标类的所有方法。
两个解决办法:
1.不优雅的做法:
在添加增强时,根据方法名去判断,是否添加增强,但是这样就得一直去维护这个增强类。
2.面向切面:
将增强类和拦截条件组合在一起,然后将这个切面配置到 ProxyFactory 中,从而生成代理。
二、AOP 和 切面的关系
1.类比于 OOP 和 对象,AOP 和 切面就是这样的一种关系。
2.也可以将 切面 看成是 AOP 的一个工具。
三、几个概念
切面(Advisor):是AOP中的一个术语,表示从业务逻辑中分离出来的横切逻辑,比如性能监控,日志记录,权限控制等。
这些功能都可以从核心的业务逻辑中抽离出去。可以解决代码耦合问题,职责更加单一。封装了增强和切点。
增强(Advice):增强代码的功能的类,横切到代码中。
目标:目标方法(JDK代理)或目标类(CGLIB代理)
代理:JDK代理,CGLIB代理。或是通过 ProxyFactory 类生产。
切点:通过一个条件来匹配要拦截的类,这个条件称为切点。如拦截所有带 Controller 注解的类。增强的条件。
连接点:作为增强方法的入参,可以获取到目标方法的信息。
四、实践:自定义一个注解,用Spring-AOP实现给添加这个注解的方法做业务处理
1.POM文件中引入Spring的jar包,其他jar不需要引用。
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
2.自定义一个注解
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; /**
* 监控请求的进度
* @author wangfeixiong
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ReviewProgressAnnotation {
String value();
}
3.配置Spring 开启AOP注解
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <description>Spring MVC Configuration</description> <!-- 加载配置属性文件 -->
<context:property-placeholder ignore-unresolvable="true" location="classpath:*.properties"/> <!-- 使用Annotation自动注册Bean -->
<context:component-scan base-package="com.future.dcwj"/>
<!-- 默认的注解映射的支持,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping -->
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager">
<mvc:message-converters register-defaults="true">
<!-- 将StringHttpMessageConverter的默认编码设为UTF-8 -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven> <!--开启spring中的定时任务-->
<task:annotation-driven/> <!--开始AOP-->
<aop:aspectj-autoproxy/> <!-- 对静态资源文件的访问, 将无法mapping到Controller的path交给default servlet handler处理 -->
<mvc:default-servlet-handler/> <!-- 静态资源映射 -->
<mvc:resources mapping="/static/**" location="/static/" cache-period="31536000"/> <!-- 定义无Controller的path<->view直接映射 -->
<mvc:view-controller path="/" view-name="redirect:${web.view.index}"/>
</beans>
4.书写注解类,自定义业务
import com.future.dcwj.common.annotation.ReviewProgressAnnotation;
import org.apache.log4j.Logger;
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.springframework.stereotype.Component; import java.util.HashMap;
import java.util.Map; /**
* describe:
*
* @author wangfeixiong
* @date 2018/02/09
*/
@Component
@Aspect
public class ReviewProgressAOP {
private final static Logger LOGGER = Logger.getLogger(ReviewProgressAOP.class); public static final String SUCESS = "true";
private static final String NO_SUCESS = "false";
public static final String REVIEW_ACTION_PROGRESS= "reviewActionProgress"; @Pointcut("@annotation(com.future.dcwj.common.annotation.ReviewProgressAnnotation)")
public void pointcutName() {
} /**
* @param joinPoint
*/
@Around("pointcutName()")
public Object introcepter(ProceedingJoinPoint joinPoint) {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
ReviewProgressAnnotation progressAnnotation = methodSignature.getMethod().getAnnotation(ReviewProgressAnnotation.class);
String operation = progressAnnotation.value();
StringBuffer key = new StringBuffer();
LOGGER.error(key.toString());
User user = UserUtils.getUser();
if (user != null) {
key.append(user.getId());
}
key = key.append(operation);
LOGGER.info(operation);
try {
Object result = joinPoint.proceed();
map.put(key.toString(), SUCESS);
JedisUtils.mapPut(REVIEW_ACTION_PROGRESS,map);
return result;
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return null;
}
}
Spring-AOP 基于注解的实现的更多相关文章
- spring-第十七篇之spring AOP基于注解的零配置方式
1.基于注解的零配置方式 Aspect允许使用注解定义切面.切入点和增强处理,spring框架可以识别并根据这些注解来生成AOP代理.spring只是用了和AspectJ 5一样的注解,但并没有使用A ...
- Spring Aop基于注解的实现
一.AspectOriented Programing,面向切面编程. AOP主要用于日志记录,性能统计,安全控制(权限控制),事务处理,异常处理等.将日志记录,性能统计,安全控制,事务处理,异常 ...
- Spring AOP基于注解的“零配置”方式实现
为了在Spring中启动@AspectJ支持,需要在类加载路径下新增两个AspectJ库:aspectjweaver.jar和aspectjrt.jar.除此之外,Spring AOP还需要依赖一个a ...
- Spring AOP基于配置文件的面向方法的切面
Spring AOP基于配置文件的面向方法的切面 Spring AOP根据执行的时间点可以分为around.before和after几种方式. around为方法前后均执行 before为方法前执行 ...
- Spring:基于注解的Spring MVC
什么是Spring MVC Spring MVC框架是一个MVC框架,通过实现Model-View-Controller模式来很好地将数据.业务与展现进行分离.从这样一个角度来说,Spring MVC ...
- spring aop 使用注解方式总结
spring aop的注解方式:和xml的配置方式略有区别,详细如下: 1.首先还是建立需要的切面类:切面类里面定义好切点配置,以及所有的需要实现的通知方法. /** * */ package com ...
- 利用Spring AOP自定义注解解决日志和签名校验
转载:http://www.cnblogs.com/shipengzhi/articles/2716004.html 一.需解决的问题 部分API有签名参数(signature),Passport首先 ...
- spring AOP自定义注解方式实现日志管理
今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...
- spring AOP自定义注解 实现日志管理
今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...
- (转)利用Spring AOP自定义注解解决日志和签名校验
一.需解决的问题 部分API有签名参数(signature),Passport首先对签名进行校验,校验通过才会执行实现方法. 第一种实现方式(Origin):在需要签名校验的接口里写校验的代码,例如: ...
随机推荐
- 初学python类编的一个求矩形小程序
简单的程序不简单,里面包含类定义类,传参,初始化,方法调用,创建实例,格式输出.主要在python中随时定义变量随时用,我这道题题想好久就是我初识类,传参,不是所有参数都的加单引号.简单的东西,复杂话 ...
- CAT部署安装文档
多数软件都在/root/project/codebase/3rdpart redhat7用firewalld取代了iptables,遇到问题请添加redhat7关键字搜索,详情请参见Common ad ...
- STL空间配置器、vector、list、deque、map复习
本文写于2017-03-03,从老账号迁移到本账号,原文地址:https://www.cnblogs.com/huangweiyang/p/6440830.html STL的六大组件:容器.算法.迭代 ...
- 关于python那些事儿
学习总结: 1.输入一个数据 a=input. 2.在输出结果中增加字符 # 运行如下语句: print("你的名字叫{}.".format("饺子")) (以 ...
- react native进一步学习(NavigatorIOS 学习)
特别申明:本人代码不作为任何商业的用途,只是个人学习的一些心得,为了使得后来的更多的程序员少走一些弯路.*(如若侵犯你的版权还望见谅)*. 开发工具:WebStorm,xcode 1. rn的创建的时 ...
- C# 8.0 抢先看-- Async Stream
异步流? Async Stream 简单说来是一种非同步的迭代器模式,说更白一点就是可以await 的foreach.在过去的C# 中如果要回传一个可迭代的IEnumerable<T> , ...
- SpringMVC详细学习笔记
Spring MVC 1 spring MVC简介: Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构 ...
- Vue语法学习第二课——指令
指令,是指在Vue中,带有-v前缀的特殊特性 指令特性的值预期是单个JavaScript表达式(v-for例外) <p v-if="seen">看得到</p> ...
- Python第六章(北理国家精品课 嵩天等)
一 1.集合类型定义及其操作: 集合用{}表示,元素用逗号分隔,无序,唯一 集合操作符: |:并 -:减 &:交 ^ :补 <= <:判断子集关系 >= >:判断包含关 ...
- mysql 5.7安装图解 mysql 5.7图文安装完整教程
今天给搭建分享一个教程,mysql 5.7的安装操作,这里呢我叫大家怎么用二进制去安装mysql,其实在大多数的生产环境中使用二进制预编译的安装方式是最多了,下面大家跟着我的步骤去尝试着安装下吧. 先 ...