Spring面向切面编程(AOP)


1 spring容器中bean特性

Spring容器的javabean对象默认是单例的。

通过在xml文件中,配置可以使用某些对象为多列。

Spring容器中的javabean对象默认是立即加载(立即实例化:spring加载完成,立即创建对象)

scope:属性

        singleton:默认值为单例,默认也是立即加载,在加载完成spring容器的时候,bean对象已经创建完成   

        prototype:多例的,默认懒加载,spring容器加载完成的时候,不会创建bean的对象,只有从容器获得bean对象的时候,才进行bean对象的实例化

        request: 将创建的javabean对象,封装到request范围

        session:将创建的javabean对象,封装到session范围

Spring容器bean的对象生命周期:

Bean对象的创建一直到销毁为bean的生命周期。

         生命周期的开始:

         如果为单例,由加载完spring容器开始

         如果为多例,由从容器获得bean对象开始

         实例化

         初始化

         服务

         销毁(单例:关闭容器的时候,多例由jvm自动回收)

2 spring的AOP面向切面编程

2.1 模拟银行转账业务

需求:实现银行的转账功能,在转账的时候需要完成

1 身份认证(登陆)

2 权限的验证

3 转账实现

4 历史交易记录,

分析:1,2,4三个功能对于银行的业务,属于公共的功能(共性的功能)

在功能实现的时候,需要将1,2,4抽取出来,单独实现,

做到了将共性的功能和核心的业务功能进行了分离

通过动态代理实现:共性的功能和核心业务功能的合并,产生核心业务对象的

 在代码实现的时候,进行了功能实现的分离:

 代码开发的进行分离,程序在运行的时候进行合并。

2.2 springAOP的思想

在系统开发中,将系统的共性的公共的功能独立实现,在程序运行的过程中,将共性功能和核心的业务功能,进行整合。

好处:

1 完成共性功能和核心业务功能的解耦合

2 提供共性功能的复用性。

2.3springAOP的概念 

Aspect切面:封装共性功能的(增强功能的)类

Advice通过:切面类中封装的增强功能的方法。

PointCut:切入点,是一个集合的概念,该集合的表达使用一个正则表达式表达

      所有核心业务对象的所有方法的前后(事务处理AOP典型的应用)

JoinPoint:连接点,程序中需要加入advice的地方,而且正在执行的ponitCut

织入(Weaving):将aspect和核心业务对象,进行整合的过程。

3 springAOP的实现

3.1通过特定接口实现

Aop通知的类型:

      Before:前置通知

      After:后置通知

      Around:环绕通知

      Throwing:异常通知

需求:实现在业务对象中的方法执行的时候,记录日志功能

3.1.1前置通知

 1 package org.guangsoft.utils;  2 import java.lang.reflect.Method;  3 import java.util.Arrays;  4 import java.util.Date;  5 import org.springframework.aop.MethodBeforeAdvice;  6 /****  7  * 前置增强:  8  * MethodBeforeAdvice 接口表示重写的方法为前置advice  9  * ***/
10 public class BeforeLog implements MethodBeforeAdvice 11 { 12  @Override 13     public void before(Method method, 14  Object[] args, Object obj) 15     throws Throwable 16  { 17  System.out.println(method); 18  System.out.println(Arrays.toString(args)); 19  System.out.println(obj); 20         System.out.println("BeforeLog-------------" + new Date()); 21  } 22 }

AOP配置:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!-- 到入xml文件的约束 -->
 3 <beans xmlns="http://www.springframework.org/schema/beans"
 4  xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
 5  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 6  xsi:schemaLocation="http://www.springframework.org/schema/beans  7  http://www.springframework.org/schema/beans/spring-beans-4.1.xsd  8  http://www.springframework.org/schema/aop  9  http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
10     <!-- 实例化BeforeLog对象 -->
11     <bean id="bf" class="org.guangsoft.utils.BeforeLog"></bean>
12     <!-- 实例化service对象 -->
13     <bean id="us" class="org.guangsoft.service.impl.UsersServiceImpl" />
14     <!-- 进行aop的配置,产生代理对象 -->
15     <aop:config>
16         <!-- 声明切入点 -->
17         <aop:pointcut expression="execution(* org.guansoft.service.impl.*.*(..))"
18  id="pc" />
19         <!-- 织入 将通知和切入点进行合并(切面+核心业务对象) -->
20         <aop:advisor advice-ref="bf" pointcut-ref="pc" />
21     </aop:config>
22 </beans>
23  

3.1.2后置通知

对业务对象的方法进行后增强。

 1 package org.guangsoft.utils;  2 import java.lang.reflect.Method;  3 import java.util.Date;  4 import org.springframework.aop.AfterReturningAdvice;  5 /***  6  * 后置通知  7  * ***/
 8 public class AfterLog implements AfterReturningAdvice  9 { 10  @Override 11     public void afterReturning(Object obj1,// obj1 接收目标方法的返回值
12  Method method, 13  Object[] args, 14             Object obj2) throws Throwable 15  { 16         // System.out.println(obj1+"----------------------"+obj2);
17         System.out.println("AfterLog-------------------" + new Date()); 18  } 19

AOP配置:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!-- 到入xml文件的约束 -->
 3 <beans xmlns="http://www.springframework.org/schema/beans"
 4  xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
 5  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 6  xsi:schemaLocation="http://www.springframework.org/schema/beans  7  http://www.springframework.org/schema/beans/spring-beans-4.1.xsd  8  http://www.springframework.org/schema/aop  9  http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
10     <!-- 实例化BeforeLog对象 -->
11     <bean id="bf" class="org.guangsoft.utils.BeforeLog"></bean>
12     <bean id="af" class="org.guangsoft.utils.AfterLog"></bean>
13     <!-- 实例化service对象 -->
14     <bean id="us" class="org.guangsoft.service.impl.UsersServiceImpl" />
15     <!-- 进行aop的配置,产生代理对象 -->
16     <aop:config>
17         <!-- 声明切入点 -->
18         <aop:pointcut expression="execution(* org.guangsoft.service.impl.*.*(..))"
19  id="pc" />
20         <!-- 织入 将通知和切入点进行合并(切面+核心业务对象) -->
21         <aop:advisor advice-ref="bf" pointcut-ref="pc" />
22         <aop:advisor advice-ref="af" pointcut-ref="pc" />
23     </aop:config>
24 </beans>
25  

3.1.3环绕通知

 1 package org.guangsoft.utils;  2 import java.lang.reflect.Method;  3 import java.util.Arrays;  4 import java.util.Date;  5 import org.aopalliance.intercept.MethodInterceptor;  6 import org.aopalliance.intercept.MethodInvocation;  7 /***  8  * 环绕通知  9  * ***/
10 public class AoundLog implements MethodInterceptor 11 { 12     /**
13  * MethodInvocation中封装了目标对象,调用的方法,方法需要的参数 14  * ***/
15  @Override 16     public Object invoke(MethodInvocation mi) throws Throwable 17  { 18         Method method = mi.getMethod(); 19         Object[] args = mi.getArguments(); 20         Object obj = mi.getThis(); 21  System.out.println(method); 22  System.out.println(Arrays.toString(args)); 23  System.out.println(obj); 24         System.out.println("around------before--------" + new Date()); 25         Object rv = method.invoke(obj, args);// 调用目标对象的方法,放行
26         System.out.println("around------after--------" + new Date()); 27         return rv; 28  } 29 }

AOP配置:同上

3.1.4 异常通知

 1 package org.guangsoft.utils;  2 import java.util.Date;  3 import org.springframework.aop.ThrowsAdvice;  4 /****  5  * 异常通知  6  * **/
 7 public class ExceptionLog implements ThrowsAdvice  8 {  9     /*** 10  * 该类中的方法参考AfterReturningAdvice写 11  * 该参数是用来接收异常信息的 12  * ***/
13     public void afterThrowing(Throwable ex) throws Throwable 14  { 15         // System.out.println(obj1+"----------------------"+obj2);
16         System.out.println("ExceptionLog-----------" + ex.getMessage() 17                 + "--------" + new Date()); 18  } 19 }

Pointcut:核心业务对象

Advice:通知

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM