学习阶段小结:spring AOP
1.AOP定义及作用:
aop是spring的一个重要组件,面向切面编程,为基于spring的应用程序提供了事务管理的服务.其将通用业务和传统业务分离,通过结合Aop配置实现功能叠加效果(即编写一个通用方法就可以作用于需要该方法的所有的方法中);
2.AOP用到的标记(aop面向方面编程是在oop面向对象编程的方法基础上进行改进的)
(1)Aspect(方面/切面)
写的通用方法(逻辑),可以切进其他方法中(目标组件方法)
(2)Pointcut(切入点)
写的表达式,用于指定被切的目标组件和方法,其包括方法限定表达式,类型限定表达式和名称限定表达式
①方法限定表达式:
execution(修饰符?
返回类型 方法名(参数列表) 抛出异常?)
//所有以load开头的方法会被切入功能
execution(* load*(..))
//cn.xdl.web.controller包下所有类所有方法被切入
execution(* cn.xdl.web.controller.*.*(..))
//cn.xdl.web.controller包下及子包中所有类所有方法被切入
execution(* cn.xdl.web.controller..*.*(..))
execution(* load*(..))
//cn.xdl.web.controller包下所有类所有方法被切入
execution(* cn.xdl.web.controller.*.*(..))
//cn.xdl.web.controller包下及子包中所有类所有方法被切入
execution(* cn.xdl.web.controller..*.*(..))
②类型限定表达式:
within(包名.类名)
//RestUserController组件所有方法被切入
within(cn.xdl.web.controller.RestUserController)
//cn.xdl.web.controller包下所有类所有方法被切入
within(cn.xdl.web.controller.*)
//cn.xdl.web.controller包下及子包中所有类所有方法被切入
within(cn.xdl.web.controller..*)
③名称限定表达式:
bean(id名称,即默认情况下是需要指定通用方法的类的类名,类名首字母小写)
//id=restUserController的对象所有方法 bean(restUserController) //id名以Controller结尾的所有对象所有方法 bean(*Controller)(3)Advice(通知,指定切入的时机,即方法调用前,方法调用后,异常发生后等等)try{ 环绕通知前置部分--》@Around 前置通知--》@Before //执行目标组件方法 后置通知--》@AfterReturning 环绕通知后置部分 }catch(){ 异常通知--》@AfterThrowing }finally{ 最终通知--》@After }
3.AOP使用的步骤(1)编写需要追加的通用方法(编写切面--Aspect)(2)确认切入点表达式(该表达式规定了切入的对象是谁--pointcut)(3)选择通知的类型(Advice)
4.具体案例详解:目标:给自己写的RESTful服务(增删改查服务)记录其每次调用时调用的组件,方法和执行的时间(1)通用方法:记录其每次调用时调用的组件,方法和执行的时间(2)确认追加的对象规定了都有谁可以用这个通用功能(3)确定追加通用方法的时间本例中原方法前和方法后都要用到这个通用方法,因此采用@Around通知4-1:代码如下--package com.xdl.aop; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; import org.springframework.util.StopWatch; @Component @Aspect public class WatchBean { @Around("within(com.xdl.web.controller.RestFulController)") public Object time(ProceedingJoinPoint pjp) throws Throwable{ StopWatch watch = new StopWatch(); watch.start();//开始计时 //以上之前 Object obj = pjp.proceed();//执行目标组件方法 //以下之后 watch.stop();//停止计时 long time = watch.getTotalTimeMillis(); //获取执行的组件名称 String className=pjp.getTarget().getClass().getName(); //获取执行组件的方法名 String methodName=pjp.getSignature().getName(); System.out.println(className+"的"+methodName+"方法:"+"执行的时间:"+time+"毫秒"); System.out.println(methodName); return obj; } }4-2:启用Aop配置--<?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:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> <!-- 启用Aop配置 --> <aop:aspectj-autoproxy ></aop:aspectj-autoproxy> <!-- 启用扫描,扫描aop组件 --> <context:component-scan base-package="com.xdl.aop" ></context:component-scan> </beans>注意:
(1):WatchBean是属于aop的组件,所以最好放在com.xdl.aop这样类似定义的包中,且要在类名上加@Component标记(不唯一)
(2):在web.xml中要将spring-aop.xml放进去,代码位置如下:<servlet> <servlet-name>springMvc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!--在这里加xml--> <param-value>classpath:spring-mvc.xml,classpath:spring-dao.xml,classpath:sping-aop.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>(3):切面标记@Aspect要加在通用方法类上

被折叠的 条评论
为什么被折叠?



