1、面向切面編程(AOP)的概念:把項目中需要在多處用到的功能,比如日志、安全和事物等集中到一個類中處理,而不用在每個需要用到該功能的地方顯式調用。 2、術語解釋: 橫切關注點:分布應用于多處的功能 切面:橫切關注點可以被模塊化為一個類,這個類被稱為一個切面 通知(advice):切面要完成的工作。SPRing的通知有5種類型:before、after、after-returning、after-throwing和around這五種類型。 連接點(joinpoint):連接點表示在何種操作發生時應用切面。比如方法調用時、修改字段時和拋出異常時等等 切點(pointcut):一般用某個包中的類的明確的方法來指定在何處應用切面,應用切面的這個點便稱為切點,一般用切點來指定連接點。
第一步:導入jar包(Spring與aspectJ) 第二步:Spring的主配置文件: i.在表頭中添加aop、context的命名空間
<?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: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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">第三歩:在配置文件中開啟aop注解
<aop:aspectj-autoproxy proxy-target-class="true"/>第四步:讓類生產bean對象,通過掃描包,所有注解的類都生產bean
<context:component-scan base-package="com.iotek.aspect"/>第五步: 定義橫切面類
package com.iotek.aspect;import java.util.Arrays;import java.util.List;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.springframework.stereotype.Component;@Component("logAspect")public class LogAspect { /* * 1、@Before:在某個方法執行前調用 * 2、@After:在某個方法執行后調用 * 3、@AfterReturning:在某個方法執行后調用,且獲得其返回值 * 4、@AfterThrowing:在某個方法執行后調用,如果有異常在執行此方法 * 5、@Around:環繞執行,包含了@Before與@After等(不常用) * * 注意:執行參數的設置 * 例如:@Before("execution(public void com.iotek.daoImpl.ComputeDaoImpl.add(int, int))") * execution(---):執行命令 * 簡化:@Before("execution(* com.iotek.daoImpl.ComputeDaoImpl.*(..))") * * * 常用的對象: * JoinPoint:獲得信息: * 1、獲得當前執行的方法名: * 2、獲得方法中參數傳遞的內容 * */ /* * 由于每個方法執行的內容都相同:execution(內容都相同) * 則將內容提起出來 PointCut */ /*@Pointcut(value="execution(* com.iotek.daoImpl.ComputeDaoImpl.*(..))") public void qrd(){};*/ //--設置方法執行點: //@Before("execution(public void com.iotek.daoImpl.ComputeDaoImpl.add(int, int))") //@Before("execution(* com.iotek.daoImpl.ComputeDaoImpl.*(..))") //@Before("qrd()") /* * */ public void xmlBefore(JoinPoint point){ String name=point.getSignature().getName(); List list=Arrays.asList(point.getArgs()); System.out.println("--->xmlBefore名字是:"+name+"參數是:"+list); } public void xmlAfter(){ System.out.println("--->xmlAfter名字是"); } public void afterException(JoinPoint point,Exception ex){ String methodName = point.getSignature().getName(); System.out.println("--->afterException"+methodName+":異常信息:"+ex); } public void afterReturnMethod(JoinPoint point,Object res){ String methodName=point.getSignature().getName(); System.out.println("--->afterResultMethod方法名稱是:"+methodName+"-返回值:"+res); } public void aroundMethod(JoinPoint point){ String methodName=point.getSignature().getName(); List list=Arrays.asList(point.getArgs()); System.out.println("--->aroundMethod:"+methodName+"-參數:"+list); }}第六步、定義實體類
package com.iotek.dao;public class PersonDao { public void add(int i, int j) { int sum = i /j; System.out.println("add==="+sum); } public int sum(int i, int j) { int sum = i + j; System.out.println("sum==="+sum); return sum; }}第七步、注冊AOP對象
<!-- 注冊:AOP對象 --> <bean id="logAspect" class="com.iotek.aspect.LogAspect"></bean>第八步、 定義切入點 表達式:PointCut
<!-- 定義切入點 表達式:PointCut --> <aop:pointcut expression="execution(* com.iotek.dao.PersonDao.*(..))" id="log"/> <aop:aspect ref="logAspect"> <aop:before method="xmlBefore" pointcut-ref="log"/> <aop:after method="xmlAfter" pointcut-ref="log"/> <aop:after method="aroundMethod" pointcut-ref="log"/> <aop:after-returning method="afterReturnMethod" returning="res" pointcut-ref="log"/> <aop:after-throwing method="afterException" throwing="ex" pointcut-ref="log"/> </aop:aspect> </aop:config>第九步、測試類
package com.iotek.demo;import org.springframework.context.applicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.iotek.dao.PersonDao;public class Demo { public static void main(String args[]){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); PersonDao person=context.getBean("person", PersonDao.class); person.add(10, 5); person.sum(2, 1); }}applicationContext.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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <aop:aspectj-autoproxy proxy-target-class="true"/> <context:component-scan base-package="com.iotek.aspect"/> <bean id="person" class="com.iotek.dao.PersonDao"></bean> <aop:config> <!-- 定義切入點 表達式:PointCut --> <aop:pointcut expression="execution(* com.iotek.dao.PersonDao.*(..))" id="log"/> <aop:aspect ref="logAspect"> <aop:before method="xmlBefore" pointcut-ref="log"/> <aop:after method="xmlAfter" pointcut-ref="log"/> <aop:after method="aroundMethod" pointcut-ref="log"/> <aop:after-returning method="afterReturnMethod" returning="res" pointcut-ref="log"/> <aop:after-throwing method="afterException" throwing="ex" pointcut-ref="log"/> </aop:aspect> </aop:config> </beans>通過掃描包完成Bean的注入,需要在類中聲明@Component(“logAspect”) 切面類也可以通過注解方式定義:
package com.iotek.aspect2;import java.util.Arrays;import java.util.List;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;@Aspectpublic class LogAspect2 { @Before("execution(* com.iotek.dao.PersonDao.*(..))") public void xmlBefore(JoinPoint point){ String name=point.getSignature().getName(); List list=Arrays.asList(point.getArgs()); System.out.println("--->xmlBefore名字是22222222:"+name+"參數是:"+list); } @After("execution(* com.iotek.dao.PersonDao.*(..))") public void xmlAfter(){ System.out.println("--->xmlAfter名字是2222222"); } @AfterThrowing(value="execution(* com.iotek.dao.PersonDao.*(..))",throwing="ex") public void afterException(JoinPoint point,Exception ex){ String methodName = point.getSignature().getName(); System.out.println("--->afterException222222"+methodName+":異常信息:"+ex); } @AfterReturning(value="execution(* com.iotek.dao.PersonDao.*(..))",returning="res") public void afterReturnMethod(JoinPoint point,Object res){ String methodName=point.getSignature().getName(); System.out.println("--->afterResultMethod方法名稱是22222222222:"+methodName+"-返回值:"+res); }}新聞熱點
疑難解答