回见Java框架spring Boot学习笔记(十三):aop实例操作,这里介绍注解aop操作

首先编写一个切入点HelloWorld.java

 package com.example.spring;

 public class HelloWorld {
public void printHello(){
System.out.println("Hello Aop.");
}
}

编写切面TimeHandler.java

 package com.example.spring;

 import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before; //使用@Aspect注解轻松定义切面
@Aspect
public class TimeHandler { // 在方法上面使用注解完成前置增强配置
@Before(value = "execution(* com.example.spring.HelloWorld.*(..))")
public void beforTime()
{
System.out.println("前置增强:CurrentTime = " + System.currentTimeMillis());
} // 在方法上面使用注解完成后置增强配置
@After(value = "execution(* com.example.spring.HelloWorld.*(..))")
public void afterTime()
{
System.out.println("后置增强:CurrentTime = " + System.currentTimeMillis());
} // 在方法上面使用注解完成环绕增强配置
@Around(value = "execution(* com.example.spring.HelloWorld.*(..))")
public void aroundTime(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
//方法之前
System.out.println("环绕增强:CurrentTime = " + System.currentTimeMillis()); //执行被增强的方法
proceedingJoinPoint.proceed(); //方法之后
System.out.println("环绕增强:CurrentTime = " + System.currentTimeMillis());
}
}

编写配置文件aop.xml

 <?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <!-- bean definition & AOP specific configuration -->
<!-- 1 配置对象-->
<bean id="helloWorld" class="com.example.spring.HelloWorld"/>
<bean id="timeHandler" class="com.example.spring.TimeHandler"/> <!-- 2 开启aop操作-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>

编写运行文件Application.java

 package com.example.spring;

 import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application {
public static void main(String[] args) {
//bean配置文件所在位置 D:\\IdeaProjects\\spring\\src\\Beans.xml
//使用AbstractApplicationContext容器
AbstractApplicationContext context = new ClassPathXmlApplicationContext("file:D:\\IdeaProjects\\spring\\src\\aop.xml");
//得到配置创建的对象
HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld");
helloWorld.printHello();
}
}

运行输出

环绕增强:CurrentTime = 1510208830742
前置增强:CurrentTime = 1510208830742
Hello Aop.
环绕增强:CurrentTime = 1510208830750
后置增强:CurrentTime = 1510208830750

最新文章

  1. F#之旅8 - 图片处理应用之动画二维码
  2. 关于Xcode7中添加不了libresolv.dylib等类似库的问题
  3. Android java传递int类型数组给C
  4. luogu[2093]零件分组
  5. Flatten 2D Vector
  6. java并发编程实践笔记
  7. grails-shiro权限认证
  8. 《Spring技术内幕》学习笔记17——Spring HTTP调用器实现远程调用
  9. 解决animate动画连续播放bug
  10. 用java流方式判断文件类型
  11. 面试前的准备---C#知识点回顾----04
  12. Java操作PDF之itext入门
  13. PHP常用数组(Array)函数整理
  14. iOS下OpenCV开发用OC还是Swift
  15. POJ 2253 Frogger(Dijkstra变形——最短路径最大权值)
  16. 一脸懵逼学习Java操作Excel之POI(Apache POI)
  17. web项目访问地址前添加小图片
  18. blob 对象 实现分片上传 及 显示进度条
  19. 【转载】java对象和byte数组互转,直接拿去用
  20. 【比赛】NOIP2017 时间复杂度

热门文章

  1. Android使用okhttp 响应Post请求 使用线程
  2. 《重构-改善既有代码的设计》学习笔记----Extract Method(提炼函数)
  3. spring boot项目中处理Schedule定时任务
  4. ES - Index Templates 全局index模板
  5. Node文件模块
  6. Kong配置参考
  7. IIS Express 域认证问题(https://stackoverflow.com/questions/4762538/iis-express-windows-authentication)
  8. Scrapy CrawlSpider源码分析
  9. JavaScript数组方法--concat、push
  10. [python,2018-06-29] 37%法则及其拓展解决恋爱问题