2019-03-08/14:22:58

演示:登陆核心业务类与日志周边功能实现AOP面向切面思想

jar包:https://share.weiyun.com/5GOFouP

学习资料:http://how2j.cn/k/spring/spring-aop/89.html?p=67889

AOP术语:https://www.cnblogs.com/bencoper/p/10507208.html

1.AOP

  Spring(1)中有讲过什么是AOP,这里就放一个原理图看一下

  1. 功能分两大类,辅助功能和核心业务功能
  2. 辅助功能和核心业务功能彼此独立进行开发
  3. 比如登陆功能,即便是没有性能统计和日志输出,也可以正常运行
  4. 如果有需要,就把"日志输出" 功能和 "登陆" 功能 编织在一起,这样登陆的时候,就可以看到日志输出了
  5. 辅助功能,又叫做切面,这种能够选择性的,低耦合的把切面和核心业务功能结合在一起的编程思想,就叫做切面编程

2.准备业务类ProductService

  1. package service;
  2.  
  3. public class ProductService {
  4.  
  5. public void doSomeService(){
  6.  
  7. System.out.println("doSomeService");
  8.  
  9. }
  10.  
  11. }

ProductService.java

3.引入切面之前调用业务类

  1. package test;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
  6. import com.how2java.service.ProductService;
  7.  
  8. public class TestSpring {
  9.  
  10. public static void main(String[] args) {
  11. ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
  12. ProductService s = (ProductService) context.getBean("s");
  13. s.doSomeService();
  14. }
  15. }

TestSpring.java

运行之前需要在配置文件中加一个扫描

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  12. http://www.springframework.org/schema/tx
  13. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  14. http://www.springframework.org/schema/context
  15. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  16.  
  17. <bean name="s" class="service.ProductService"> //声明业务对象
  18. </bean>

运行结果

4.准备日志切面 LoggerAspect

该日志切面的功能是 在调用核心功能之前和之后分别打印日志,切面就是原理图中讲的那些辅助功能。

下面是切面要执行的函数 log,log函数有一个形参 joinPoint 这个可以理解为断点

第8行代码可以理解为就是将来与某个核心功能编织之后,用于执行核心功能的代码

getSignature 获取切点的签名

proceed() 执行切点本来的业务

  1. package aspect;
  2. import org.aspectj.lang.ProceedingJoinPoint;
  3.  
  4. public class LoggerAspect {
  5.  
  6. public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
  7. System.out.println("start log:" + joinPoint.getSignature().getName()); //开始
  8. Object object = joinPoint.proceed(); //监视
  9. System.out.println("end log:" + joinPoint.getSignature().getName()); //执行切面,结束
  10. return object;
  11. }
  12. }

5.AOP配置文件详解

17-20行:详情见注释

23-25行:   指定右边的核心业务功能    看原理图的实现

这一句是声明切入点,切入点的 id 叫 loggerCutPoint ,用来标记这个切入点,这个expression表示:满足expression中的方法调用之后,就会去进行切面操作,类似于触发了切面

第一个 * 代表返回任意类型       service.ProductService.*     表示包名以 service.ProductService 开头的类的任意方法(第二个*表示任意方法,通配符的意思)(..) 表示方法的参数是任意数量和类型

简单说就是,只要service这个包中的ProductService类的任意一个函数被调用,不管返回值是什么,都会触发开关,我就会去执行切面,也就是辅助功能,但是辅助功能是什么呢,就是下面两句:

27-29行:   指定左边的辅助功能     看原理图的实现

这两句是定义了一个切面,上面说只要触发开关,就会去执行切面,就是指的这里的切面,所谓切面,就是一个类中的方法...

id代表这个切面的名字,ref就是指的方法所在的类,method代表的就是方法的名字,

pointcut-ref="loggerCutpoint" 这个就是表示这个切面是和上面的切点关联起来的(一个切点是可以关联多个切面的,一个切面只能关联一个方法),只要上面的切点被触发,就会到这里来执行一些辅助功能

after表示在切入点触发之后来执行我这个中断,当然也有before,一共有五个before,after,After-returning ,After-throwing,Around。具体的AOP术语单独用一篇博客细讲一下

在 method 参数后面,还可以加上参数列表。

  1. <aop:config/>:把业务对象与辅助功能编织在一起。
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  12. http://www.springframework.org/schema/tx
  13. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  14. http://www.springframework.org/schema/context
  15. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  16.  
  17. <bean name="s" class="service.ProductService">
  18. </bean> //声明业务类
  19.  
  20. <bean id="loggerAspect" class="aspect.LoggerAspect"/> //声明日志切面
  21.  
  22. <aop:config>
  23. <aop:pointcut id="loggerCutpoint"
  24. expression=
  25. "execution(* service.ProductService.*(..)) "/>
  26.  
  27. <aop:aspect id="logAspect" ref="loggerAspect">
  28. <aop:around pointcut-ref="loggerCutpoint" method="log"/>
  29. </aop:aspect>
  30. </aop:config>
  31.  
  32. </beans>

6.测试代码

  1. package test;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
  6. import service.ProductService;
  7.  
  8. public class TestSpring {
  9.  
  10. public static void main(String[] args) {
  11. ApplicationContext context = new ClassPathXmlApplicationContext(
  12. new String[] { "applicationContext.xml" });
  13. ProductService s = (ProductService) context.getBean("s");
  14. s.doSomeService();
  15. }
  16. }

TestSpring.java

 
TestSpring 代码没有发生任何变化,通过配置的方式,把切面和核心业务类编制在了一起。
运行测试,可以发现在编织之后,业务方法运行之前和之后分别会打印日志

SpringAOP(5)的更多相关文章

  1. Spring-AOP实践 - 统计访问时间

    公司的项目有的页面超级慢,20s以上,不知道用户会不会疯掉,于是老大说这个页面要性能优化.于是,首先就要搞清楚究竟是哪一步耗时太多. 我采用spring aop来统计各个阶段的用时,其中计时器工具为S ...

  2. Spring-Aop入门

    (一)Aop术语定义 1.通知(advice) 通知定义了切面要做什么工作,即调用的方法,同时定义了什么时候做这些工作,即以下五种类型 (1)前置通知(Before) :在目标方法调用之前执行切面方法 ...

  3. 转-springAOP基于XML配置文件方式

    springAOP基于XML配置文件方式 时间 2014-03-28 20:11:12  CSDN博客 原文  http://blog.csdn.net/yantingmei/article/deta ...

  4. SpringAOP详解(转载大神的)

    AOP(Aspect-Oriented Programming)这个东西,名字与 OOP 仅差一个字母,其实它是对 OOP 编程方式的一种补充,并非是取而代之.翻译过来就是"面向方面编程&q ...

  5. spring-aop学习

     SpringAOP学习 author:luojie 1.  AOP中的基本概念 AOP的通用术语,并非spring java所特有.很遗憾AOP的术语不是特别的直观.但如果让Spring java来 ...

  6. SpringAOP之静态代理

    一.SpringAOP: ⒈AOP:Aspect Oriented Programming 面向切面编程, 实现的是核心业务和非核心业务之间的的分离,让核心类只做核心业务,代理类只做非核心业务.  ⒉ ...

  7. springaop实现登陆验证

    1.首先配置好springmvc和springaop 2.先写好登陆方法,通过注解写代理方法 通过代理获得登陆方法的参数方法名,然后再aop代理方法内进行登陆验证 贴出代码 package com.h ...

  8. spring-aop示例

    具体案例放在github上,主要是jar包在上面 https://github.com/guoyansi/spring-aop-example knights.xml <?xml version ...

  9. 使用SpringAop 验证方法参数是否合法

    (原文地址:http://blog.csdn.net/is_zhoufeng/article/details/7683194) 1.依赖包    aspectjweaver.jar 其中Maven的配 ...

  10. 关于SpringAOP的XML方式的配置

    AOP(XML)[理解][应用][重点] 1.AOP基础实例 A.导入jar包 核心包(4个)         日志(2个)             AOP(4个) Spring进行AOP开发(1个) ...

随机推荐

  1. MySQL 复制 - 性能与扩展性的基石 3:常见问题及解决方案

    主备复制过程中有很大可能会出现各种问题,接下来我们就讨论一些比较普遍的问题,以及当遇到这些问题时,如何解决或者预防问题发生. 1 数据损坏或丢失 问题描述:服务器崩溃.断电.磁盘损坏.内存或网络错误等 ...

  2. 【效率神奇】Github丧心病狂的9个狠招

    Github,一个被业内朋友成为「全球最大的同性交友社区」的平台. 小时候遇到不会的字可以查新华字典.后来写作文我们可以通过作文书.或者文摘去找合适的素材.同样,写代码可以去Github上找适合自己的 ...

  3. 高淇java300集异常机制作业

    1.以下关于异常的代码的执行结果是(C ).(选择一项) 1 2 3 4 5 6 7 8 9 10 11 12 public class Test {     public static void m ...

  4. 在做关于NIO TCP编程小案例时遇到无法监听write的问题,没想到只是我的if语句的位置放错了位置,哎,看了半天没看出来

    在做关于NIO TCP编程小案例时遇到无法监听write的问题,没想到只是我的if语句的位置放错了位置,哎,看了半天没看出来 贴下课堂笔记: 在Java中使用NIO进行网络TCP套接字编程主要以下几个 ...

  5. h5实现实时时钟

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"/> <meta nam ...

  6. 一次apk打开时报内存溢出错误,故写下内存溢出的各种原因和解决方法

    原转载:https://blog.csdn.net/cp_panda_5/article/details/79613870 正文内容: 对于JVM的内存写过的文章已经有点多了,而且有点烂了,不过说那么 ...

  7. 数据库原理 - 序列3 - 事务是如何实现的? - Redo Log解析

    6.5 事务实现原理之1:Redo Log 介绍事务怎么用后,下面探讨事务的实现原理.事务有ACID四个核心属性:A:原子性.事务要么不执行,要么完全执行.如果执行到一半,宕机重启,已执行的一半要回滚 ...

  8. eclipse代码提示设置过常用字符还是不起作用的解决方法

    问题:重装eclipse之后发现没有了代码提示,一般情况下在设置中添加自动提示的字符之后就可以了,设置如下 如上图,初始的时候是只有一个点号,并没有字符,输入26个字母的大小写后点击Apply and ...

  9. SpringBoot 项目在静态工具类中注入 RedisTemplate

    静态属性不能直接注入,可以通过其set方法进行注入.(注意生成的set方法需要去掉static). 在工具类里直接注入RedisTemplate,两种方法: (1)使用@Autowired priva ...

  10. #Java学习之路——基础阶段二(第八篇)

    我的学习阶段是跟着CZBK黑马的双源课程,学习目标以及博客是为了审查自己的学习情况,毕竟看一遍,敲一遍,和自己归纳总结一遍有着很大的区别,在此期间我会参杂Java疯狂讲义(第四版)里面的内容. 前言: ...