Spring框架AOP
aop技术是面向切面编程思想,作为OOP的延续思想添加到企业开发中,用于弥补OOP开发过程中的缺陷而提出的编程思想。AOP底层也是面向对象;只不过面向的不是普通的Object对象,而是特殊的AOP对象。AOP的关注点是组成系统的非核心通用服务模块(比如登录检查等),相对于普通对象,aop不需要通过继承、方法调用的方式来提供功能,只需要在xml文件中以引用的方式,将非核心服务功能引用给需要改功能的核心业务逻辑对象或方法中。最终实现对象的解耦。spring 中ioc技术实现了核心业务逻辑对象之间的解耦(如LoginAction与DaoImpl)
需要的依赖
百度云盘下载:
链接:https://pan.baidu.com/s/1hVbjTwzhzVVa7bWX2Qyfxw
提取码:efar
AOP可以说是Spring中最难理解的一个知识点了,你可能网上找了很多资料,也买过很多本书,但都不是很理解到底什么是AOP?我曾经也是琢磨了好久才有了一定的了解。那么,到底怎么讲这个知识点呢。来不及解释了,快上车!听完这个例子,我相信你一定会对AOP有一个非常深刻的理解!
让我们新建一个英雄类:
package com.spring.bean; public class Hero { private String heroName;
private String type;
private String description; public String getHeroName() {
return heroName;
}
public void setHeroName(String heroName) {
this.heroName = heroName;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Hero [heroName=" + heroName + ", type=" + type + ", description=" + description + "]";
} }
再来个露娜类,继承自英雄类:
package com.spring.bean; public class Luna extends Hero{ /**
* 秀操作
*/
public void operation(){
System.out.println("看我月下无限连!");
} /**
* 跑路
*/
public void run(){
System.out.println("我操,大空了,赶紧跑!");
} /**
* 发信息
* @param str
*/
public void say(String str){
System.out.println(str);
} }
可以看到,露娜类有三个方法,分别是秀操作,跑路和发信息。
再写一个团战类:
package com.spring.service; import com.spring.bean.Luna; /**
* 团战类
* @author Administrator
*
*/
public class Battle { public void tuan(){
Luna luna = new Luna();
luna.say("上去开团!");
luna.operation(); } }
测试代码如下:
public class TestBattle {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-aop.xml");
Battle battle = (Battle)context.getBean("Battle");
battle.tuan();
}
}
spring-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id = 'Battle' class="com.spring.test.Battle"></bean>
</beans>
运行测试代码:
在团战方法里面,我们新建一个露娜的对象,然后发出信息“上去开团”,接着又秀了一把操作。这是一个比较普通的流程。而事实上,露娜可能需要在团战前就提醒我方队友“等我集合打团”,不要人都没齐,队友就无脑往前冲。OK,我们如何通过代码来实现这个过程呢?很显然,这个过程需要在团战方法执行之前就被执行。这就需要AOP面向切面的技术了。
我们需要写一个类,实现MethodBeforeAdvice接口。
package com.spring.service; import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; import com.spring.bean.Luna; /**
* Notice 定义一个通知打团的信号 - 团战之前
* @author Administrator
*
*/
public class BeforeTuanZhan implements MethodBeforeAdvice{ @Override
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
System.out.print(this.getClass().getName() + " -- ");
Luna luna = new Luna();
luna.setHeroName("露娜");
luna.setType("战士/法师");
luna.setDescription("月光女神");
luna.say("等我集合打团!");
} }
我们希望这个方法在团战之前就被执行,怎么做呢?没错,就是在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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id='Battle' class="com.spring.service.Battle"></bean>
<bean id='BeforeTuanZhan' class="com.spring.service.BeforeTuanZhan"></bean> <aop:config>
<!-- 定义所有可供露娜切入的点(方法) -->
<!-- 原则上只要时机正确,任何团战露娜都可以切进去! -->
<aop:pointcut expression="execution(* com.spring.test.Battle.*(..))"
id="pointcut" /> <aop:advisor advice-ref="BeforeTuanZhan" pointcut-ref="pointcut" />
</aop:config>
</beans>
这里配置了一个切点pointcut
execution(* com.spring.test.Battle.*(..))
这句话的含义就是,返回值为任意,com.spring.test包里面的Battle类,这个类里面所有的方法都需要切入。所谓的切入,就是在方法执行前,执行中,发生异常的时候执行某个其他的方法。执行中用的不多,一般就用另外三种情况。
现在,我们重新执行测试代码:
package com.spring.test; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.spring.service.Battle; public class TestBattle {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-aop.xml");
Battle battle = (Battle)context.getBean("Battle");
battle.tuan();
}
}
报错的话
如果遇到报错:java.lang.ClassNotFoundException,一般是xml配置的bean的class不对,按住ctrl+左键单击,可以点击定位到类才对。
如果遇到报错:java.lang.IllegalArgumentException: warning no match for this type name。。PointcutParser.parsePointcutExpression,可能是切点没有配置对
现在各位想一下,如果团战打赢了怎么办,是不是马上就该去推塔或者打龙啊,这个时候,如果队友团战打赢了就发呆,那就很坑了。所以呢,你这个时候就得提醒队友下一步该做什么,这个提醒的步骤是在团战方法执行结束后才发生的。
我们需要新建一个AfterTuanZhan类,实现AfterReturningAdvice接口。
package com.spring.service; import java.lang.reflect.Method; import org.springframework.aop.AfterReturningAdvice; import com.spring.bean.Luna; /**
* Notice 定义一个团战结束后的类 - 团战之后
* @author Administrator
*/
public class AfterTuanZhan implements AfterReturningAdvice{ @Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
System.out.print(this.getClass().getName() + " -- ");
Luna luna = new Luna();
luna.setHeroName("露娜");
luna.setType("战士/法师");
luna.setDescription("月光女神");
luna.say("进攻敌方防御塔!");
} }
配置到spring-aop.xml中:
<bean id = 'AfterTuanZhan' class="com.spring.service.AfterTuanZhan"></bean>
<!-- 定义一个切面 -->
<aop:config> <!-- 定义所有可供露娜切入的点(方法) -->
<!-- 原则上只要时机正确,任何团战露娜都可以切进去! -->
<aop:pointcut expression="execution(* com.spring.test.Battle.*(..))" id="pointcut"/> <aop:advisor advice-ref="BeforeTuanZhan" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="AfterTuanZhan" pointcut-ref="pointcut"/> </aop:config>
现在,我们重新执行测试代码:
再来说说万一团战失利的情况,比如露娜断大了咋办,没错,这个时候就是团战发生了异常,我们在Battle类中手动设置一个异常:
/**
* 团战类
* @author Administrator
*
*/
public class Battle { public void tuan(){
Luna luna = new Luna();
luna.say("上去开团!");
luna.operation(); int i = 1 / 0 ; } }
然后,编写TuanZhanException类,实现ThrowsAdvice接口:
package com.spring.service; import java.lang.reflect.Method; import org.springframework.aop.ThrowsAdvice; import com.spring.bean.Luna; /**
* 定义一个团战异常类,万一出现情况就进入这个类
* @author Administrator
*
*/
public class TuanZhanException implements ThrowsAdvice { //该方法会在露娜团战出现异常后自动执行
public void afterThrowing(Method method, Object[] args,
Object target, Exception ex){
System.out.print(this.getClass().getName() + " -- ");
Luna luna = new Luna();
luna.run();
}
}
配置到spring-aop.xml:
<bean id = 'TuanZhanException' class="com.spring.service.TuanZhanException"></bean>
<!-- 定义一个切面 -->
<aop:config> <!-- 定义所有可供露娜切入的点(方法) -->
<!-- 原则上只要时机正确,任何团战露娜都可以切进去! -->
<aop:pointcut expression="execution(* com.spring.test.Battle.*(..))" id="pointcut"/> <aop:advisor advice-ref="BeforeTuanZhan" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="AfterTuanZhan" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="TuanZhanException" pointcut-ref="pointcut"/> </aop:config>
现在,我们重新执行测试代码:
总结:
1. aop面向切面,切的是什么,没错,切的是方法!
2. 怎么切,你记好了,就是你先自己规定哪些方法需要切,然后设置切入的方式:方法执行之前做什么,执行之后做什么,如果方法出现异常,又要做什么?另外还有一种方法执行的过程中做什么,只是用的比较少,反正我还没有见过在哪里用了。用的最多的就是发生异常后做什么,比如事务管理。
转自:https://www.cnblogs.com/skyblue-li/p/7877676.html
Spring框架AOP的更多相关文章
- spring框架 AOP核心详解
AOP称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等待,Struts2的拦截器设计就是基于AOP的思想,是个比较经典的例子. 一 AOP的基本概念 (1)Asp ...
- 跟着刚哥学习Spring框架--AOP(五)
AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.OOP引入 ...
- spring框架aop用注解形式注入Aspect切面无效的问题解决
由于到最后我的项目还是有个邪门的错没解决,所以先把文章大概内容告知: 1.spring框架aop注解扫描默认是关闭的,得手动开启. 2.关于Con't call commit when autocom ...
- Spring框架——AOP代理
我们知道AOP代理指的就是设计模式中的代理模式.一种是静态代理,高效,但是代码量偏大:另一种就是动态代理,动态代理又分为SDK下的动态代理,还有CGLIB的动态代理.Spring AOP说是实现了AO ...
- Spring框架-AOP详细学习[转载]
参考博客:https://blog.csdn.net/qq_22583741/article/details/79589910#4-%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85% ...
- Spring框架 AOP面向切面编程(转)
一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址:http://www.cnbl ...
- 10 Spring框架 AOP (三) Spring对AspectJ的整合
上两节我们讲了Spring对AOP的实现,但是在我们的开发中我们不太使用Spring自身的对AOP的实现,而是使用AspectJ,AspectJ是一个面向切面的框架,它扩展了Java语言.Aspect ...
- Spring框架AOP学习总结(下)
目录 1. AOP 的概述 2. Spring 基于AspectJ 进行 AOP 的开发入门(XML 的方式): 3.Spring 基于AspectJ 进行 AOP 的开发入门(注解的方式): 4.S ...
- 08 Spring框架 AOP (一)
首先我们先来介绍一下AOP: AOP(Aspect Orient Programming),面向切面编程,是面向对象编程OOP的一种补充.面向对象编程是从静态角度考虑程序的结构,面向切面编程是从动态的 ...
- 09 Spring框架 AOP (二) 高级用法
上一篇文章我们主要讲了一点关于AOP编程,它的动态考虑程序的运行过程,和Spring中AOP的应用,前置通知,后置通知,环绕通知和异常通知,这些都是Spring中AOP最简单的用法,也是最常用的东西, ...
随机推荐
- ASP.NET全局编码和语言
// /*--------------- // // 使用地方:ASP.NET 项目 // // // // 文件名: // // 文件功能描述:可控制整个项目的一个统一编码格式和语言文字显示 ...
- 用java代码实现
用java代码实现(1)珠穆朗玛峰高度为8848米,有一张足够大的纸,厚度为0.001米. (2)请问,我折叠多少次,可以折成珠穆朗玛峰的高度/** * @author 18269 * @date 2 ...
- 阿里P7浅谈SpringMVC
一.前言 既然是浅谈 SpringMVC,那么我们就先从基础说起,本章节主要讲解以下内容: 1.三层结构介绍 2.MVC 设计模式介绍 3.SpringMVC 介绍 4.入门程序的实现 注:介绍方面的 ...
- rsync & sersync 实时同步
1.根据之前一篇关于rsync的随笔部署好rsync服务后,可以开始inotify的部署 2.sersync的部署 ①.部署服务(安装和配置过程) #Master 部署Sersync服务 mkdir ...
- 面对runc逃逸漏洞,华为云容器为您保驾护航
背景信息 基于runc运行时的容器存在安全漏洞,攻击者可以通过恶意容器镜像等方式获取宿主机root执行权限.漏洞CVE-2019-5736的详细信息,请参见 https://cve.mitre.org ...
- 《构建之法》——Alpha2项目的测试
这个作业属于哪个课程 课程的链接 这个作业要求在哪里 作业要求的链接 团队名称 Runningman 这个作业的目标 测试其他组的项目,互相借鉴 作业正文 作业正文 测试人姓名 陈嘉莹 测试人学号 2 ...
- Annoying Present
http://codeforces.com/group/1EzrFFyOc0/contest/1009/problem/C 题意:原本有一个n个0的数组a[],你对它进行m次操作,每次操作让a[j]+ ...
- 字符串搜索(strStr)--- java版
这里来学习一下从一个源字符串中搜索指定的字符串,有些啰嗦,直接看最终的效果: 实际上JAVA SDK中相当于String.indexOf()方法,上面的用例改用JAVA SDK来实现看一下: 编译运行 ...
- Java 基础 线程的Runnable接口 /线程的同步方法 /同步代码块
笔记: /**通过 Runnable接口来实现多线程 * 1. 创建一个实现runnable 接口的类 * 2. 在类中实现接口的run() 抽象方法 * 3. 创建一个runnable 接口实现类的 ...
- TCP的服务简单介绍
1 引言尽管T C P和U D P都使用相同的网络层(I P),T C P却向应用层提供与U D P完全不同的服务.T C P提供一种面向连接的.可靠的字节流服务.面向连接意味着两个使用 T C P的 ...