Spring 基于 Aspect 注解的增强实现
来源:互联网 发布:2k16捏脸数据 姚明 编辑:程序博客网 时间:2024/05/17 04:28
定义基本实体类
package com.advice;/** * @author Duoduo * @version 1.0 * @date 2017/4/25 23:41 */public class Performer { public void doPerform() { System.out.println("Performer do perform ....................... "); }}
定义基于注解的增强类
package com.advice;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.*;/** * @author Duoduo * @version 1.0 * @date 2017/4/25 23:42 */@Aspect//定义切面public class Audience { //定义切点 @Pointcut("execution(* com.advice.Performer.doPerform(..))") public void doPerform(){} @Before("doPerform()") public void takeSeas() { System.out.println("The audience is taking their seats."); } @Before("doPerform()") public void turnOffPhone() { System.out.println("The audience is turn off their cellphone."); } @AfterReturning("doPerform()") public void applaund() { System.out.println("CLAP CLAP CLAP CLAP ..."); } @AfterThrowing("doPerform()") public void demandRefund() { System.out.println("Boo! we want our money back!"); } @Around("doPerform()") public void watchPerfomance(ProceedingJoinPoint joinPoint) { try { Long start = System.currentTimeMillis(); joinPoint.proceed(); long end = System.currentTimeMillis(); System.out.println("The performance took "+(end-start)+" milliseconds"); } catch (Throwable throwable) { throwable.printStackTrace(); } }}
Spring 自动代理配置
<!-- aop 增强自动代理 --><aop:aspectj-autoproxy/><bean id="audience" class="com.advice.Audience"/><bean id="performer" class="com.advice.Performer"/>
Junit测试
@Test public void testDoPerform() throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext("classpath:smart-context.xml"); //代理为指向Interface的代理 Performer performer = (Performer) context.getBean("performer"); System.out.println("+++++++++++++++++++++++++++++++++"); performer.doPerform(); }
测试结果
+++++++++++++++++++++++++++++++++2017-04-26 20:51:16,980 DEBUG [main] (AbstractBeanFactory.java:251) - Returning cached instance of singleton bean 'audience'The audience is taking their seats.The audience is turn off their cellphone.Performer do perform ....................... The performance took 91 millisecondsCLAP CLAP CLAP CLAP ...
0 0
- Spring 基于 Aspect 注解的增强实现
- 基于注解@Aspect的AOP实现
- spring 基于Aspect和注解的切面编程(aop)
- 基于@Aspect的AOP注解配置
- spring Aspect 实现自定义注解的日志记录,有时候注解类不起作用的原因分析
- spring aspect 注解
- Aspect注解实现简单的切面
- Spring Mvc那点事---(27)Spring Mvc基于aspect的AOP实现
- 跟我学aspectj之十二 ----- 基于注解的Aspect
- Spring中Aspect注解使用
- 基于注解的spring AOP简单实现
- 基于spring注解切面的简单实现
- 基于Spring注解实现的工厂模式
- springmvc Aspect 实现自定义注解的日志记录
- 基于@Aspect的AOP
- Spring -- AOP入门基础&基于Aspect的AOP通知用法
- Spring框架 基于@Aspect的AOP配置(六)
- Spring AOP 的@Aspect
- C++中rand()函数的用法
- 201412-2 Z字形扫描
- Java-使用java mail技术发送电子邮件
- Web应用防护系统OpenWAF开源CC防护模块
- 编程练习4.25
- Spring 基于 Aspect 注解的增强实现
- centos 上为火狐浏览器安装flash 插件
- 动态规划——字符串编辑问题
- 数据结构与算法之图的邻接表与邻接矩阵<十二>
- 基于 Django1.10 文档的深入学习(11)—— django.shortcuts 之 render()
- 容器或将代替私有云
- 数据结构与算法之图的深度遍历<十四>
- Java第一行代码
- NCPC 2014 Problem E Opening Ceremony Problem ID: ceremony