開源框架spring詳解-----AOP的深刻理解
AOP的理解
1、AOP的概述
AOP是一種不同于OOP(面向對象編程)的編程模式,它不是OOP的替代,而是對OOP的一種有益補充。
2、spring AOP的原理
3、spring AOP的實現
在spring2.5中,常用的AOP實現方式有兩種。第一種是基于xml配置文件方式的實現,第二種是基于注解方式的實現。
接下來,以具體的是理智講解這兩種方式的使用。
Java代碼
- package com.zxf.service;
- /**
- * 業務邏輯接口
- * @author z_xiaofei168
- */
- public interface AccountService {
- public void save(String loginname, String password);
- }
- 它的實現類
- package com.zxf.service;
- import com.zxf.dao.AccountDao;
- /**
- * AccountService的實現類
- * @author z_xiaofei168
- */
- public class AccountServiceImpl implements AccountService {
- private AccountDao accountDao;
- public AccountServiceImpl() {}
- /** 帶參數的構造方法 */
- public AccountServiceImpl(AccountDao accountDao){
- this.accountDao = accountDao;
- }
- public void save(String loginname, String password) {
- accountDao.save(loginname, password);
- throw new RuntimeException("故意拋出一個異常。。。。");
- }
- /** set方法 */
- public void setAccountDao(AccountDao accountDao) {
- this.accountDao = accountDao;
- }
- }
對于業務系統來說,AccountServiceImpl類就是目標實現類,它的業務方法,如save()方法的前后或代碼會出現異常的地方都是AOP的連接點。
下面是日志服務類的代碼:
Java代碼
- package com.zxf.aspect;
- import org.aspectj.lang.JoinPoint;
- import org.aspectj.lang.ProceedingJoinPoint;
- /**
- * 日志切面類
- * @author z_xiaofei168
- */
- public class LogAspect {
- //任何通知方法都可以將第一個參數定義為 org.aspectj.lang.JoinPoint類型
- public void before(JoinPoint call) {
- //獲取目標對象對應的類名
- String className = call.getTarget().getClass().getName();
- //獲取目標對象上正在執行的方法名
- String methodName = call.getSignature().getName();
- System.out.println("前置通知:" + className + "類的" + methodName + "方法開始了");
- }
- public void afterReturn() {
- System.out.println("后置通知:方法正常結束了");
- }
- public void after(){
- System.out.println("最終通知:不管方法有沒有正常執行完成,一定會返回的");
- }
- public void afterThrowing() {
- System.out.println("異常拋出后通知:方法執行時出異常了");
- }
- //用來做環繞通知的方法可以第一個參數定義為org.aspectj.lang.ProceedingJoinPoint類型
- public Object doAround(ProceedingJoinPoint call) throws Throwable {
- Object result = null;
- this.before(call);//相當于前置通知
- try {
- result = call.proceed();
- this.afterReturn(); //相當于后置通知
- } catch (Throwable e) {
- this.afterThrowing(); //相當于異常拋出后通知
- throw e;
- }finally{
- this.after(); //相當于最終通知
- }
- return result;
- }
- }
這個類屬于業務服務類,如果用AOP的術語來說,它就是一個切面類,它定義了許多通知。Before()、afterReturn()、after()和afterThrowing()這些方法都是通知。
#p#
<1>.基于xml配置文件的AOP實現
這種方式在實現AOP時,有4個步驟。
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-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd>
- <bean id="accountDaoImpl" class="com.zxf.dao.AccountDaoImpl"/>
- <bean id="accountService" class="com.zxf.service.AccountServiceImpl">
- <property name=" accountDaoImpl " ref=" accountDaoImpl "/>
- < span>bean>
- <bean id="logAspectBean" class="com.zxf.aspect.LogAspect"/>
- <aop:config>
- <aop:aspect id="logAspect" ref="logAspectBean">
- <aop:pointcut id="allMethod"
- expression="execution(* com.zxf.service.*.*(..))"/>
- <aop:before method="before" pointcut-ref="allMethod" />
- <aop:after-returning method="afterReturn" pointcut-ref="allMethod"/>
- <aop:after method="after" pointcut-ref="allMethod"/>
- <aop:after-throwing method="afterThrowing" pointcut-ref="allMethod"/>
- < span>aop:aspect>
- < span>aop:config>
- < span>beans>
上述配置針對切入點應用了前置、后置、最終,以及拋出異常后通知。這樣在測試執行AccountServiceImpl類的save()方法時,控制臺會有如下結果輸出。 前置通知:com.zxf.service.AccountServiceImpl類的save方法開始了。 針對MySQL的AccountDao實現中的save()方法。 后置通知:方法正常結束了。 最終通知:不管方法有沒有正常執行完成,一定會返回的。 <2>基于注解的AOP的實現 首先創建一個用來作為切面的類LogAnnotationAspect,同時把這個類配置在spring的配置文件中。 在spring2.0以后引入了JDK5.0的注解Annotation的支持,提供了對AspectJ基于注解的切面的支持,從而 更進一步地簡化AOP的配置。具體的步驟有兩步。 Spring的配置文件是如下的配置: Xml代碼 這是那個切面的類LogAnnotationAspect Java代碼
備注:輸出結果和前面的一樣。 【編輯推薦】