Spring旅程(四) AOP--Spring AOP实例

转载至:http://blog.csdn.net/lovesummerforever/article/details/22668947

采用配置文件的方式。

1、          导入相应的Spring jar包。

2、          在SpringIOC中的步骤123中已经给出

3、        将横切性关注的问题模块化,建立安全处理类。在SecurityHandler类中写我们的独立方法,也就是定义Advice(具体实现),代码如下。

[java]  view plain  copy
  1. public class SecurityHandler {  
  2.           
  3.     private void checkSecurity() {  
  4.         System.out.println("-------checkSecurity-------");  
  5.     }         
  6. }  

 

4、          在配置文件中进行相关配置。applicationContext.xml代码如下所示。

[java]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.          xmlns:aop="http://www.springframework.org/schema/aop"  
  6.          xmlns:tx="http://www.springframework.org/schema/tx"  
  7.          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  8.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd  
  9.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">  
  10.              
  11.    
  12.     <bean id="userManager" class="com.bjpowernode.spring.UserManagerImpl"/>  
  13.       
  14.     <bean id="securityHandler" class="com.bjpowernode.spring.SecurityHandler"/>  
  15.       
  16.     <aop:config>  
  17.         <aop:aspect id="secutityAspect" ref="securityHandler">  
  18.           
  19.         <!-- 以add开头的方法   <aop:pointcut id="addAddMethod" expression="execution(* add*(..))"/> -->  
  20.         <!-- com.bjpowernode.spring.*包下的所有方法.   
  21.                  <aop:pointcut id="addAddMethod" expression="execution(* com.bjpowernode.spring.*.*(..))"/>  
  22.                  -->  
  23.                    
  24.                   <aop:pointcut id="addAddMethod" expression="execution(* com.bjpowernode.spring.*.add*(..)) || execution(* com.bjpowernode.spring.*.del*(..))"/>  
  25.             <aop:before method="checkSecurity" pointcut-ref="addAddMethod"/>  
  26.         </aop:aspect>  
  27.     </aop:config>  
  28.       
  29. </beans>  

需要说明几点

指定SecutityHanderaspect:    <aop:aspectid="secutityAspect" ref="securityHandler">

Pointcut(切入点)以及事务范围,在这里用于所有的adddel方法上:<aop:pointcutid="addAddMethod" expression="execution(*com.bjpowernode.spring.*.add*(..)) || execution(*com.bjpowernode.spring.*.del*(..))"/>

设置通知类型并指向哪个切入点

<aop:beforemethod="checkSecurity" pointcut-ref="addAddMethod"/>

 

将目标类UsemrManagerImplAspectSecurityHandler配置到SpringIOC:<beanid="userManager"class="com.bjpowernode.spring.UserManagerImpl"/>

<bean id="securityHandler"class="com.bjpowernode.spring.SecurityHandler"/>

 

 

5、          客户端调用代码如下所示。

[java]  view plain  copy
  1. import org.springframework.beans.factory.BeanFactory;  
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  3.   
  4. public class Client {  
  5.   
  6.     public static void main(String[] args) {  
  7.         BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");  
  8.         UserManager userManager = (UserManager)factory.getBean("userManager");  
  9.   
  10.         userManager.delUser(1);  
  11.                   
  12.     }  
  13.   
  14. }  

 

上述基本上完成了一个SpringAOP的实例。


如果我们在advice中,也就是在我们的安全性方法中想要获取客户端的数据该如何获取呢?


我们可以在这个方法中添加参数,也就是我们JoinPoint,通过传递这个对象,我们可以去得到参数值,SecurityHandler类的代码如下所示。

[java]  view plain  copy
  1. public class SecurityHandler {  
  2.       
  3.       
  4.     private void checkSecurity(JoinPoint joinPoint) {  
  5.         //取得参数.  
  6.         for(int i=0;i<joinPoint.getArgs().length;i++)  
  7.         {  
  8.             System.out.println(joinPoint.getArgs()[i]);  
  9.         }  
  10.         System.out.println(joinPoint.getSignature().getName());  
  11.         System.out.println("-------checkSecurity-------");  
  12.     }         
  13. }  


 

当然我们也可以采用注解的方式来实现。

1、采用注解的方式首先要加入一些jar包,*Spring_home/spring-framework-2.0\lib\aspectj\aspectjrt.jar aspectjweaver.jar

2、SecurityHandler类的代码如下所示。和上面的通过配置文件方式进行对比,很快就会懂得不同的标签不同的含义。

[java]  view plain  copy
  1. import org.aspectj.lang.annotation.After;  
  2. import org.aspectj.lang.annotation.Aspect;  
  3. import org.aspectj.lang.annotation.Before;  
  4. import org.aspectj.lang.annotation.Pointcut;  
  5.   
  6. @Aspect  
  7. public class SecurityHandler {  
  8.       
  9.       
  10.     /** 
  11.      * 定义Pointcut,Pointcut的名称为addAddMethod(),此方法没有返回值和参数 
  12.      * 该方法就是一个标识,不进行调用 
  13.      */  
  14.       
  15.     @Pointcut("execution(* add*(..))")  
  16.     private void addAddMethod(){};  
  17.       
  18.       
  19.     /** 
  20.      * 定义Advice,表示我们的Advice应用到哪些Pointcut订阅的Joinpoint上 
  21.      */  
  22.       
  23.     @Before("addAddMethod()")  
  24.     //@After("addAddMethod()")  
  25.     private void checkSecurity() {  
  26.         System.out.println("-------checkSecurity-------");  
  27.     }         
  28. }  

3、applicationContext.xml文件中配置aspect类和目标类UserManagerImpl,同时配置实用Annotation方式,代码如下所示。

[html]  view plain  copy
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.          xmlns:aop="http://www.springframework.org/schema/aop"  
  4.          xmlns:tx="http://www.springframework.org/schema/tx"  
  5.          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  6.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd  
  7.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">  
  8.              
  9.     <!-- 启用AspectJ对Annotation的支持 -->         
  10.     <aop:aspectj-autoproxy/>             
  11.       
  12.     <bean id="userManager" class="com.bjpowernode.spring.UserManagerImpl"/>  
  13.       
  14.     <bean id="securityHandler" class="com.bjpowernode.spring.SecurityHandler"/>  
  15. </beans>  


4、客户端调用和配置XML方式一样。


是不是关于SpirngAOP,渐渐的懂些了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值