AOP 即 Aspect Oriented Program 面向切面编程
首先,在面向切面编程的思维外面,把性能分为外围业务性能,和周边性能。
所谓的外围业务,比方登陆,减少数据,删除数据都叫外围业务
所谓的周边性能,比方性能统计,日志,事务管理等等
周边性能在Spring的面向切面编程AOP思维里,即被定义为切面
在面向切面编程AOP的思维外面,外围业务性能和切面性能别离独立进行开发
而后把切面性能和外围业务性能 "编织" 在一起,这就叫AOP
步骤1:先运行,看到成果,再学习
步骤2:模拟和排错
步骤3:jar包
步骤4:思路图
步骤5:筹备业务类 ProductService
步骤6:TestSpring
步骤7:筹备日志切面 LoggerAspect
步骤8:applicationContext.xml
步骤9:TestSpring
步骤10:练习-性能统计切面
步骤11:答案-性能统计切面
步骤 1 : 先运行,看到成果,再学习
老规矩,先下载下载区(点击进入)的可运行我的项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的成果。
步骤 2 : 模拟和排错
在确保可运行我的项目可能正确无误地运行之后,再严格照着教程的步骤,对代码模拟一遍。
模拟过程不免代码有出入,导致无奈失去冀望的运行后果,此时此刻通过比拟正确答案 ( 可运行我的项目 ) 和本人的代码,来定位问题所在。
采纳这种形式,学习有成果,排错有效率,能够较为显著地晋升学习速度,跨过学习路上的各个槛。
举荐应用diffmerge软件,进行文件夹比拟。把你本人做的我的项目文件夹,和我的可运行我的项目文件夹进行比拟。
这个软件很牛逼的,能够晓得文件夹里哪两个文件不对,并且很显著地标记进去
这里提供了绿色装置和应用教程:diffmerge 下载和应用教程
步骤 3 : jar包
为了反对AOP,须要用到一些额定的JAR包。 这些额定的JAR包放在下载区(点击进入)的可运行我的项目,解压进去的lib目录下。
至于多了哪些,记不太分明了。。。。 都导入吧。。。。
步骤 4 : 思路图
1. 性能分两大类,辅助性能和外围业务性能
2. 辅助性能和外围业务性能彼此独立进行开发
3. 比方登陆性能,即使是没有性能统计和日志输入,也能够失常运行
4. 如果有须要,就把"日志输入" 性能和 "登陆" 性能 编织在一起,这样登陆的时候,就能够看到日志输入了
5. 辅助性能,又叫做切面,这种可能选择性的,低耦合的把切面和外围业务性能联合在一起的编程思维,就叫做切面编程
步骤 5 : 筹备业务类 ProductService
package com.how2java.service;public class ProductService { public void doSomeService(){ System.out.println("doSomeService"); }}
步骤 6 : TestSpring
在引入切面之前,调用该业务类
package com.how2java.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.how2java.service.ProductService;public class TestSpring { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" }); ProductService s = (ProductService) context.getBean("s"); s.doSomeService(); }}
步骤 7 : 筹备日志切面 LoggerAspect
该日志切面的性能是 在调用外围性能之前和之后别离打印日志,切面就是原理图中讲的那些辅助性能。
Object object = joinPoint.proceed();
就是未来与某个外围性能编织之后,用于执行外围性能的代码
package com.how2java.aspect;import org.aspectj.lang.ProceedingJoinPoint;public class LoggerAspect { public Object log(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("start log:" + joinPoint.getSignature().getName()); Object object = joinPoint.proceed(); System.out.println("end log:" + joinPoint.getSignature().getName()); return object; }}
步骤 8 : applicationContext.xml
<bean name="s" class="com.how2java.service.ProductService"></bean>
申明业务对象
<bean id="loggerAspect" class="com.how2java.aspect.LoggerAspect"/>
申明日志切面
联合思路图
<aop:pointcut id="loggerCutpoint" expression= "execution(* com.how2java.service.ProductService.*(..)) "/>
指定左边的外围业务性能
<aop:aspect id="logAspect" ref="loggerAspect"> <aop:around pointcut-ref="loggerCutpoint" method="log"/></aop:aspect>
指定右边的辅助性能
而后通过aop:config把业务对象与辅助性能编织在一起。
execution(* com.how2java.service.ProductService.*(..))
这示意对满足如下条件的办法调用,进行切面操作:
* 返回任意类型
com.how2java.service.ProductService.* 包名以 com.how2java.service.ProductService 结尾的类的任意办法
(..) 参数是任意数量和类型
<?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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean name="c" class="com.how2java.pojo.Category"> <property name="name" value="yyy" /> </bean> <bean name="p" class="com.how2java.pojo.Product"> <property name="name" value="product1" /> <property name="category" ref="c" /> </bean> <bean name="s" class="com.how2java.service.ProductService"> </bean> <bean id="loggerAspect" class="com.how2java.aspect.LoggerAspect"/> <aop:config> <aop:pointcut id="loggerCutpoint" expression= "execution(* com.how2java.service.ProductService.*(..)) "/> <aop:aspect id="logAspect" ref="loggerAspect"> <aop:around pointcut-ref="loggerCutpoint" method="log"/> </aop:aspect> </aop:config></beans>
步骤 9 : TestSpring
TestSpring 代码没有产生任何变动,通过配置的形式,把切面和外围业务类编制在了一起。
运行测试,能够发现在编织之后,业务办法运行之前和之后别离会打印日志
package com.how2java.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.how2java.service.ProductService;public class TestSpring { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationContext.xml" }); ProductService s = (ProductService) context.getBean("s"); s.doSomeService(); }}
更多内容,点击理解: https://how2j.cn/k/spring/spring-aop/89.html