AOP,即面向切面编程,springAOP采用的是动态代理的技术

其主要作用可以做一些与业务逻辑无关,但却必须的操作,例如日志记录,权限管理,检查数据,等等。首先,来做一个小实现来方便理解

首先,建立一个maven工程,导入对于的包,我直接全部贴出来

pom.xml

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4.  
  5. <groupId>com.lx</groupId>
  6. <artifactId>springmvcdemo</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9.  
  10. <name>springmvcdemo</name>
  11. <url>http://maven.apache.org</url>
  12.  
  13. <properties>
  14. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  15. </properties>
  16.  
  17. <dependencies>
  18. <!-- spring的依赖 -->
  19. <dependency>
  20. <groupId>org.springframework</groupId>
  21. <artifactId>spring-context</artifactId>
  22. <version>4.3.10.RELEASE</version>
  23. </dependency>
  24.  
  25. <!-- springMVC -->
  26. <dependency>
  27. <groupId>org.springframework</groupId>
  28. <artifactId>spring-webmvc</artifactId>
  29. <version>4.3.10.RELEASE</version>
  30. </dependency>
  31.  
  32. <!-- Spring AOP模块 -->
  33. <dependency>
  34. <groupId>org.springframework</groupId>
  35. <artifactId>spring-aspects</artifactId>
  36. <version>4.3.10.RELEASE</version>
  37. </dependency>
  38.  
  39. <dependency>
  40. <groupId>junit</groupId>
  41. <artifactId>junit</artifactId>
  42. <version>3.8.1</version>
  43. <scope>test</scope>
  44. </dependency>
  45.  
  46. </dependencies>
  47.  
  48. <build>
  49.  
  50. </build>
  51.  
  52. </project>

加入MathCalculator 类,这个类就是我们的业务逻辑类,我们要对它进行切面编程

MathCalculator .java

  1. public class MathCalculator {
  2.  
  3. public int div(int i,int j) {
  4. System.out.println("MathCalculator.div()被调用");
  5. return i/j;
  6. }
  7. }

然后,建立一个切面类

  1. /***
  2. * 切面类
  3. * @author hicku
  4. *
  5. *@Aspect:这个注解告诉spring,这个是一个切面类,而不是其他普通的类
  6. */
  7.  
  8. @Aspect
  9. public class LogAspects {
  10.  
  11. //抽取公共的切入点表达式
  12. //1,本类引用
  13. //2,其他的切面引用
    //告诉他,这个是public的,返回参数类型是int,同时目标位置为MathCalculator下面的所有方法
  14. @Pointcut("execution(public int com.lx.springmvcdemo.aop.MathCalculator.*(..))")
  15. public void printCut() {}
  16.  
  17. //@before,在目标方法之前切入,切入点表达式(指在哪个方法切入)
  18. //@Before(value = "public int com.lx.springmvcdemo.aop.MathCalculator.*(..)")
  19. //也可以用
  20. @Before("printCut()")
  21. public void logStart(JoinPoint joinPoint) {
  22. Object[] obj=joinPoint.getArgs();
  23. System.out.println("除法运行"+joinPoint.getSignature().getName()+"-------参数列表:"+Arrays.asList(obj));
  24. }
  25.  
  26. @After("execution(public int com.lx.springmvcdemo.aop.MathCalculator.*(..))")
  27. public void logEnd(JoinPoint joinPoint) {
  28. System.out.println("除法结束-------"+joinPoint.getSignature().getName());
  29. }
  30.  
  31. //JoinPoint这个参数一定要放在其他值的前面,否则会报错
  32. @AfterReturning(value="printCut()",returning="result")
  33. public void logReturn(JoinPoint joinPoint,Object result) {
  34. System.out.println("除法返回正常-------运行结果("+result+")");
  35. }
  36.  
  37. @AfterThrowing(value="printCut()",throwing="exception")
  38. public void logException(Exception exception) {
  39. System.out.println("除法异常-------异常信息:{"+exception.toString()+"}");
  40. }
  41. }

如里面注释,需要@Aspect,让spring知道,这个是一个切面类

还有,注意,代码里面有两个切入代码表达式的方法。

然后,还要一个配置类,因为要用springAOP,你一定要用在spring里面注册了的bean,不然是用不了的

配置类如下:

  1. @EnableAspectJAutoProxy
  2. @Configuration
  3. public class MainConfigofAOP {
  4.  
  5. //业务逻辑类加入到容器中
  6. @Bean
  7. public MathCalculator mathCalculator() {
  8. return new MathCalculator();
  9. }
  10.  
  11. //将切面类加入到容器中
  12. @Bean
  13. public LogAspects logAspects() {
  14. return new LogAspects();
  15. }
  16.  
  17. }

最后,写一个测试类:

  1. public class IOCTest_AOP {
  2.  
  3. AnnotationConfigApplicationContext applicationContext=new AnnotationConfigApplicationContext(MainConfigofAOP.class);
  4.  
  5. @Test
  6. public void test01() {
  7. //使用AOP一定要使用spring容器里面注册的bean来用,不能自己new一个MathCalculator,这样做是没有用的
  8. //MathCalculator me=new MathCalculator();
  9. //me.div(1, 1);
  10. MathCalculator mathCalculator= (MathCalculator) applicationContext.getBean("mathCalculator");
  11. mathCalculator.div(1, 0);
  12. }
  13. }

输出结果如下:

  1. 除法运行div-------参数列表:[1, 0]
  2. MathCalculator.div()被调用
  3. 除法结束-------div
  4. 除法异常-------异常信息:{java.lang.ArithmeticException: / by zero}

最后,总结一下:

* AOP:【动态代理】
* 指,在程序运行期间,动态的将某一段代码切入制定的位置进行运行
*
* 1,导入AOP模块:spring AOP:(spring-aspects)pom.xml的包
* 2,定义一个业务逻辑类MathCaluculator():在业务逻辑的的时候将日志打印(方法之前,运行结束,出现异常)
* 3,定义一个日志切面类(LogAspects):切面类里面的方法需要动态感知MathCaluculator运行到哪里了
* 通知方法:
* 前置通知(@Before):logStart:在目标方法div运行之前运行
* 后置通知(@After):logEnd:在目标方法div运行结束之后运行
* 返回通知(@AfterReturning):logReturn:在目标正常返回之后运行
* 异常通知(@AfterThrowing):logException:在目标出现异常之后运行
* 环绕通知(@Around):动态代理,手动推进目标方法运行(joinPoint。procced())
* 4,给切面类的目标方法标注何时何地运行(在LogAspects用通知注解)
* 5,将切面类和业务逻辑类(目标所在类)都加入到容器中
* 6,然后必须告诉spring,哪个是切面类(给切面类上加一个注解:@Aspect)
* 【7】,给配置文件中加入@EnableAspectJAutoProxy【开启基于注解的AOP模式】
* 在spring中有很多的@EnableXXXX,都是开启某一个功能的
* 其中JoinPrint这个参数一定要放到其他值的前面

* 三步:
* 1),将业务逻辑组件和切面类都加入到容器中,告诉spring哪个是切面类(@Aspect)
* 2),在切面类里面每一个通知方法上标注通知注解,告诉spring什么时候运行(切入点表达式:
* @Pointcut("execution(public int com.lx.springmvcdemo.aop.MathCalculator.*(..))"))或者是@Before什么的
* 3),上面第七步,开启注解AOP模式:@EnableAspectJAutoProxy

同IOC一样,这个AOP的原理也在看,

主要就是实现了这两个接口:implements SmartInstantiationAwareBeanPostProcessor, BeanFactoryAware
最后这就是后置处理器(后置处理器就是在bean初始化前后做的事情)和自动装配BeanFactory

SpringAOP的简单实现的更多相关文章

  1. spring-aop的简单实例注解版

    项目结构如图,基本的spring的配置就不在赘述 1.首先编写自定义的切面类 package org.wu.test; import org.aspectj.lang.annotation.After ...

  2. AspectJ教学

    这几天看JAVA基础看的有点头疼,决定时不时的换换口味,准备開始调研一些如今流行的技术,于是,開始埋头思考自己知识的盲区(当时,自己的知识盲区茫茫多...),想了几天后,决定要開始研究一下几种技术及实 ...

  3. SpringBoot CGLIB AOP解决Spring事务,对象调用自己方法事务失效.

    对于像我这种喜欢滥用AOP的程序员,遇到坑也是习惯了,不仅仅是事务,其实只要脱离了Spring容器管理的所有对象,对于SpringAOP的注解都会失效,因为他们不是Spring容器的代理类,Sprin ...

  4. Spring详细教程

    Spring学习总结---- 一.导入Spring必须的jar包 二.简单示例入门注入一个User 1.编写User实体类 package test.Spring.helloworld; import ...

  5. @Transactional spring 事务(转载)

    原文链接: http://www.cnblogs.com/sweetchildomine/p/6978037.html?utm_source=itdadao&utm_medium=referr ...

  6. 理解SpingAOP

    目录 什么是AOP? AOP术语 通知(Advice) 连接点(Join point) 切点(Pointcut) 连接点和切点的区别 切面(Aspect) 引入(Introduction) 织入(We ...

  7. SpringAOP+注解实现简单的日志管理

    今天在再次深入学习SpringAOP之后想着基于注解的AOP实现日志功能,在面试过程中我们也经常会被问到:假如项目已经上线,如何增加一套日志功能?我们会说使用AOP,AOP也符合开闭原则:对代码的修改 ...

  8. spring-aop + memcached 的简单实现

    一般情况下,java程序取一条数据是直接从数据库中去取,当数据库达到一定的连接数时,就会处于排队等待状态,某些在一定时间内不会发生变化的数据,完全没必要每次都从数据库中去取,使用spring-aop ...

  9. 基于Annotation与SpringAOP的缓存简单解决方案

    前言: 由于项目的原因,需要对项目中大量访问多修改少的数据进行缓存并管理,为达到开发过程中通过Annotation简单的配置既可以完成对缓存的设置与更新的需求,故而设计的该简易的解决方案. 涉及技术: ...

随机推荐

  1. 待研究———node中使用session时的id不断更改问题

    使用的expree,中间件为cookie-parser,express-session,当对res.session.id进行赋值操作后,再调取其值会发现,此时它的值并不是最初给定的值,而是经过加密的字 ...

  2. ssi,服务器端包含,<include file="">

    一.什么是SSI ssi,全称Server Side Include,中文名,服务器端包含. SSI (Server Side Includes)是HTML页面中的指令,在页面被提供时由服务器进行运算 ...

  3. Codeforces785D - Anton and School - 2

    传送门 题意 给出一个只包含'(',')'的字符序列,询问有多少个\(RSBS\) 分析 首先需要知道一个公式 \[\sum_{i=0}^{min(x,y)}C_x^i*C_y^i=C_{x+y}^x ...

  4. bzoj 3979: [WF2012]infiltration【瞎搞+随机化】

    参考:https://www.cnblogs.com/ccz181078/p/5622200.html 非常服气.jpg 就是random_shuffle几次然后顺着找,ans取min... #inc ...

  5. 又一篇Centos7下的asp.net core部署教程

    历程2个多月的学习,我终于从PHP转.Net开发了. 从壹开始前后端分离[ .NETCore2.1 +Vue 2 +AOP+DI]框架 感谢老张的博客,我对asp.net core入门主要就是靠他的博 ...

  6. Step 4: Install and Configure Databases

    https://www.cloudera.com/documentation/enterprise/latest/topics/cm_ig_installing_configuring_dbs.htm ...

  7. 给Ambari集群里安装基于Hive的大数据实时分析查询引擎工具Impala步骤(图文详解)

    不多说,直接上干货! Impala和Hive的关系(详解) 扩展博客 给Clouderamanager集群里安装基于Hive的大数据实时分析查询引擎工具Impala步骤(图文详解) 参考 horton ...

  8. B/S和C/S示意图

    B/S C/S

  9. ORA-00845错误原因及解决办法

    故障现象: 用startup命令启动数据库时出现ora-00845错误提示: SQL> startup ORA-00845: MEMORY_TARGET not supported on thi ...

  10. poj2661Factstone Benchmark

    链接 利用log函数来求解 n!<=2^k k会达到400+W 暴力就不要想了,不过可以利用log函数来做 log2(n!) = log2(1)+log2(2)+..log2(n)<=k ...