SpringAOP的简单实现
AOP,即面向切面编程,springAOP采用的是动态代理的技术
其主要作用可以做一些与业务逻辑无关,但却必须的操作,例如日志记录,权限管理,检查数据,等等。首先,来做一个小实现来方便理解
首先,建立一个maven工程,导入对于的包,我直接全部贴出来
pom.xml
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.lx</groupId>
- <artifactId>springmvcdemo</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>springmvcdemo</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <!-- spring的依赖 -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>4.3.10.RELEASE</version>
- </dependency>
- <!-- springMVC -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>4.3.10.RELEASE</version>
- </dependency>
- <!-- Spring AOP模块 -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aspects</artifactId>
- <version>4.3.10.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- <build>
- </build>
- </project>
加入MathCalculator 类,这个类就是我们的业务逻辑类,我们要对它进行切面编程
MathCalculator .java
- public class MathCalculator {
- public int div(int i,int j) {
- System.out.println("MathCalculator.div()被调用");
- return i/j;
- }
- }
然后,建立一个切面类
- /***
- * 切面类
- * @author hicku
- *
- *@Aspect:这个注解告诉spring,这个是一个切面类,而不是其他普通的类
- */
- @Aspect
- public class LogAspects {
- //抽取公共的切入点表达式
- //1,本类引用
- //2,其他的切面引用
//告诉他,这个是public的,返回参数类型是int,同时目标位置为MathCalculator下面的所有方法- @Pointcut("execution(public int com.lx.springmvcdemo.aop.MathCalculator.*(..))")
- public void printCut() {}
- //@before,在目标方法之前切入,切入点表达式(指在哪个方法切入)
- //@Before(value = "public int com.lx.springmvcdemo.aop.MathCalculator.*(..)")
- //也可以用
- @Before("printCut()")
- public void logStart(JoinPoint joinPoint) {
- Object[] obj=joinPoint.getArgs();
- System.out.println("除法运行"+joinPoint.getSignature().getName()+"-------参数列表:"+Arrays.asList(obj));
- }
- @After("execution(public int com.lx.springmvcdemo.aop.MathCalculator.*(..))")
- public void logEnd(JoinPoint joinPoint) {
- System.out.println("除法结束-------"+joinPoint.getSignature().getName());
- }
- //JoinPoint这个参数一定要放在其他值的前面,否则会报错
- @AfterReturning(value="printCut()",returning="result")
- public void logReturn(JoinPoint joinPoint,Object result) {
- System.out.println("除法返回正常-------运行结果("+result+")");
- }
- @AfterThrowing(value="printCut()",throwing="exception")
- public void logException(Exception exception) {
- System.out.println("除法异常-------异常信息:{"+exception.toString()+"}");
- }
- }
如里面注释,需要@Aspect,让spring知道,这个是一个切面类
还有,注意,代码里面有两个切入代码表达式的方法。
然后,还要一个配置类,因为要用springAOP,你一定要用在spring里面注册了的bean,不然是用不了的
配置类如下:
- @EnableAspectJAutoProxy
- @Configuration
- public class MainConfigofAOP {
- //业务逻辑类加入到容器中
- @Bean
- public MathCalculator mathCalculator() {
- return new MathCalculator();
- }
- //将切面类加入到容器中
- @Bean
- public LogAspects logAspects() {
- return new LogAspects();
- }
- }
最后,写一个测试类:
- public class IOCTest_AOP {
- AnnotationConfigApplicationContext applicationContext=new AnnotationConfigApplicationContext(MainConfigofAOP.class);
- @Test
- public void test01() {
- //使用AOP一定要使用spring容器里面注册的bean来用,不能自己new一个MathCalculator,这样做是没有用的
- //MathCalculator me=new MathCalculator();
- //me.div(1, 1);
- MathCalculator mathCalculator= (MathCalculator) applicationContext.getBean("mathCalculator");
- mathCalculator.div(1, 0);
- }
- }
输出结果如下:
- 除法运行div-------参数列表:[1, 0]
- MathCalculator.div()被调用
- 除法结束-------div
- 除法异常-------异常信息:{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的简单实现的更多相关文章
- spring-aop的简单实例注解版
项目结构如图,基本的spring的配置就不在赘述 1.首先编写自定义的切面类 package org.wu.test; import org.aspectj.lang.annotation.After ...
- AspectJ教学
这几天看JAVA基础看的有点头疼,决定时不时的换换口味,准备開始调研一些如今流行的技术,于是,開始埋头思考自己知识的盲区(当时,自己的知识盲区茫茫多...),想了几天后,决定要開始研究一下几种技术及实 ...
- SpringBoot CGLIB AOP解决Spring事务,对象调用自己方法事务失效.
对于像我这种喜欢滥用AOP的程序员,遇到坑也是习惯了,不仅仅是事务,其实只要脱离了Spring容器管理的所有对象,对于SpringAOP的注解都会失效,因为他们不是Spring容器的代理类,Sprin ...
- Spring详细教程
Spring学习总结---- 一.导入Spring必须的jar包 二.简单示例入门注入一个User 1.编写User实体类 package test.Spring.helloworld; import ...
- @Transactional spring 事务(转载)
原文链接: http://www.cnblogs.com/sweetchildomine/p/6978037.html?utm_source=itdadao&utm_medium=referr ...
- 理解SpingAOP
目录 什么是AOP? AOP术语 通知(Advice) 连接点(Join point) 切点(Pointcut) 连接点和切点的区别 切面(Aspect) 引入(Introduction) 织入(We ...
- SpringAOP+注解实现简单的日志管理
今天在再次深入学习SpringAOP之后想着基于注解的AOP实现日志功能,在面试过程中我们也经常会被问到:假如项目已经上线,如何增加一套日志功能?我们会说使用AOP,AOP也符合开闭原则:对代码的修改 ...
- spring-aop + memcached 的简单实现
一般情况下,java程序取一条数据是直接从数据库中去取,当数据库达到一定的连接数时,就会处于排队等待状态,某些在一定时间内不会发生变化的数据,完全没必要每次都从数据库中去取,使用spring-aop ...
- 基于Annotation与SpringAOP的缓存简单解决方案
前言: 由于项目的原因,需要对项目中大量访问多修改少的数据进行缓存并管理,为达到开发过程中通过Annotation简单的配置既可以完成对缓存的设置与更新的需求,故而设计的该简易的解决方案. 涉及技术: ...
随机推荐
- 待研究———node中使用session时的id不断更改问题
使用的expree,中间件为cookie-parser,express-session,当对res.session.id进行赋值操作后,再调取其值会发现,此时它的值并不是最初给定的值,而是经过加密的字 ...
- ssi,服务器端包含,<include file="">
一.什么是SSI ssi,全称Server Side Include,中文名,服务器端包含. SSI (Server Side Includes)是HTML页面中的指令,在页面被提供时由服务器进行运算 ...
- Codeforces785D - Anton and School - 2
传送门 题意 给出一个只包含'(',')'的字符序列,询问有多少个\(RSBS\) 分析 首先需要知道一个公式 \[\sum_{i=0}^{min(x,y)}C_x^i*C_y^i=C_{x+y}^x ...
- bzoj 3979: [WF2012]infiltration【瞎搞+随机化】
参考:https://www.cnblogs.com/ccz181078/p/5622200.html 非常服气.jpg 就是random_shuffle几次然后顺着找,ans取min... #inc ...
- 又一篇Centos7下的asp.net core部署教程
历程2个多月的学习,我终于从PHP转.Net开发了. 从壹开始前后端分离[ .NETCore2.1 +Vue 2 +AOP+DI]框架 感谢老张的博客,我对asp.net core入门主要就是靠他的博 ...
- Step 4: Install and Configure Databases
https://www.cloudera.com/documentation/enterprise/latest/topics/cm_ig_installing_configuring_dbs.htm ...
- 给Ambari集群里安装基于Hive的大数据实时分析查询引擎工具Impala步骤(图文详解)
不多说,直接上干货! Impala和Hive的关系(详解) 扩展博客 给Clouderamanager集群里安装基于Hive的大数据实时分析查询引擎工具Impala步骤(图文详解) 参考 horton ...
- B/S和C/S示意图
B/S C/S
- ORA-00845错误原因及解决办法
故障现象: 用startup命令启动数据库时出现ora-00845错误提示: SQL> startup ORA-00845: MEMORY_TARGET not supported on thi ...
- poj2661Factstone Benchmark
链接 利用log函数来求解 n!<=2^k k会达到400+W 暴力就不要想了,不过可以利用log函数来做 log2(n!) = log2(1)+log2(2)+..log2(n)<=k ...