【AOP】基于@Aspect的AOP配置
基于spring cloud的aop配置
1,启动类MemberAppliaction增加注解
@Import({SwaggerConfiguraion.class, WebMvcAutoConfiguration.class})
@SpringBootApplication
@FFanApplication
@EnableFFanApiDoc
@EnableAspectJAutoProxy(proxyTargetClass = true, exposeProxy=true) //启动aspect的aop注解
@EnableAutoConfiguration
@EnableTransactionManagement
@EnableDiscoveryClient
@ComponentScan(basePackages = {"cn.wanda.sail.member"})
@MapperScan(basePackages = {"cn.wanda.sail.member.mapper"})
@EnableFeignClients(basePackages = {"cn.wanda.sail.member.client"})
public class MemberApplication {
2,定义切面类
@Aspect
@Component
public class ProcessorAspect { private static final Logger log = LoggerFactory.getLogger(ProcessorAspect.class);
3,定义切入方法
@Around("execution(public * cn.wanda.sail.member.task.support.MemberTask.process(..))") //环绕增强
public Object execute(ProceedingJoinPoint joinPoint) throws Throwable {
if (joinPoint.getArgs() == null || joinPoint.getArgs().length == 0) {
return joinPoint.proceed();
}
TaskContext task = (TaskContext) joinPoint.getArgs()[0];
log.info("LOG00020: {} start.....", task.getTaskId());
long start = System.currentTimeMillis();
Object result = null;
try {
result = joinPoint.proceed(); //执行目标方法
} catch (Exception exception) {
handleException(task.getTaskId(), exception);
}
log.info("LOG00040: {} end cost : {} ms", task.getTaskId(), (System.currentTimeMillis() - start)); return result;
}
4,常用的切入方法
(1),后置返回通知
@AfterReturning("execution(* com.sxit..*.*(..))")
(2),后置异常通知
@AfterThrowing("execution(* com.sxit..*.*(..))")
(3),后置最终通知
@After("execution(* com.sxit..*.*(..))")
(4),环绕通知
("execution(* com.sxit..*.*(..))")
(5),前置通知
@Before("execution(* com.sxit..*.*(..))")
5,定义切入点
@Pointcut("execution(* com.sxit..*.*(..))") //针对具体要切入的方法进行说明,如果这块有说明,则切入方法@Before,@Around 都可不用再声明其具体的切入方法,直接声明pointcut声明的方法即可
public void init(){
} @Before(value="init()")
public void before(){
System.out.println("方法执行前执行.....");
}
AOP详解
AOP核心概念
1、横切关注点
对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点
2、切面(aspect)
类是对物体特征的抽象,切面就是对横切关注点的抽象
3、连接点(joinpoint)
被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器
4、切入点(pointcut)
对连接点进行拦截的定义
5、通知(advice)
所谓通知指的就是指拦截到连接点之后要执行的代码,通知分为前置、后置、异常、最终、环绕通知五类
6、目标对象
代理的目标对象
7、织入(weave)
将切面应用到目标对象并导致代理对象创建的过程
8、引入(introduction)
在不修改代码的前提下,引入可以在运行期为类动态地添加一些方法或字段
【AOP】基于@Aspect的AOP配置的更多相关文章
- spring 注解 之 AOP基于@Aspect的AOP配置
Spring AOP面向切面编程,可以用来配置事务.做日志.权限验证.在用户请求时做一些处理等等.用@Aspect做一个切面,就可以直接实现. 1.首先定义一个切面类,加上@Component @A ...
- spring aop 基于schema的aop
AOP的基本概念: 连接点(Jointpoint):表示需要在程序中插入横切关注点的扩展点,连接点可能是类初始化.方法执行.方法调用.字段调用或处理异常等等,Spring只支持方法执行连接点,在AOP ...
- 【Spring】基于@Aspect的AOP配置
Spring AOP面向切面编程,可以用来配置事务.做日志.权限验证.在用户请求时做一些处理等等.用@Aspect做一个切面,就可以直接实现. · 本例演示一个基于@Aspect的小demo 1. ...
- 基于@Aspect的AOP配置
1. Spring 除了支持Schema 方式配置 AOP,还支持注解方式:使用 @Aspect 来配置 2. Spring 默认不支持 @Aspect 风格的切面声明,通过如下配置开启@Aspect ...
- spring-第十七篇之spring AOP基于注解的零配置方式
1.基于注解的零配置方式 Aspect允许使用注解定义切面.切入点和增强处理,spring框架可以识别并根据这些注解来生成AOP代理.spring只是用了和AspectJ 5一样的注解,但并没有使用A ...
- 第三章 AOP 基于Schema的AOP
基于Schema定义的切面和前现两种方式定义的切面,内容上都差不多,只是表现形式不一样而已. 3.7.1一般增强的使用 a.目标类 public class Target { public void ...
- 基于aspect实现AOP——xml配置的其他操作
将上方配置中的前置通知,可换成环绕通知
- Spring AOP基于注解的“零配置”方式实现
为了在Spring中启动@AspectJ支持,需要在类加载路径下新增两个AspectJ库:aspectjweaver.jar和aspectjrt.jar.除此之外,Spring AOP还需要依赖一个a ...
- 第三章 AOP 基于@AspectJ的AOP
在前面,我们分别使用Pointcut.Advice.Advisor接口来描述切点.增强.切面.而现在我们使用@AdpectJ注解来描述. 在下面的例子中,我们是使用Spring自动扫描和管理Bean. ...
随机推荐
- MFC 控件使用汇总
一.动态创建button CButton *button=new CButton; button->Create(_T(,,,),);//最后一个是ID BEGIN_MESSAGE_MAP(CM ...
- leetcode766
本题经过一下午的思考,终于解出来了.使用的是层次遍历的思想. class Solution { public: bool isToeplitzMatrix(vector<vector<in ...
- C++风格与C风格文件读写效率测试-vs2015,vs2017
void test_write() { ; const char* c_plus_write_file = "H://c_plus_write_file.txt"; const c ...
- springMVC的多文件的异步上传实现
springMVC的MultipartFile与传统的ajax文件上传兼容性不好,采用如下的ajax方法,后台无法获取文件. $.ajax({ url: '/upload', type: 'POST' ...
- Timer的缺陷
- 【原创】4. MYSQL++ 之 SQLTypeAdapter类型、SQLQueryParms类型 与 SQLBuffer
1. mysqlpp::SQLBuffer 该类型其实就是SQLTypeAdapter传入的各种类型(int, string, double, long, String, …) 的包装,包装的结果就是 ...
- 分布式缓存产品Redis和memcached比较区别(图)
- Linux常用基本命令 1
useradd 创建用户. password 修改密码. date 查看时间 man date 帮助文档.f往后翻 b往前翻 q退出.软修改 man hwclock 修改硬件时钟, cal 查看日历 ...
- php 数据导出csv 注意问题。
总共10W数据每次下载到9.5W就停了. 加上这个就好了 ini_set('memory_limit','512M'): //脚本运行无时间限制. set_time_limit(0); 要设置一个se ...
- 682. Baseball Game 棒球游戏 按字母处理
[抄题]: You're now a baseball game point recorder. Given a list of strings, each string can be one of ...