Spring整理12 -- 面对切面(AOP)3 -- 使用CGLIB实现AOP

上面我们使用注解配置,注解配置使用很方便也很快速,但它不够灵活,不好维护。下面我们将使用配置文件来建立AOP。

我们还是基于上面的例子,使用配置文件,我们只需修改上面的SecurityHandler.java和applicationContext.xml,代码如下:

SecurityHandler.java

public class SecurityHandler { 

    private void checkSecurity() {

       System.out.println("------checkSecurity()------");

    }

   

}

applicationContext.xml

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

   

    <bean id="userManager" class="spring.UserManagerImpl"/>

   

    <aop:config>

       <aop:aspect id="security" ref="securityHandler">

           <aop:pointcut id="allAddMethod"

expression="execution(*spring.UserManagerImpl.add*(..))"

/>

           <aop:before method="checkSecurity"

pointcut-ref="allAddMethod"/>

       </aop:aspect>

    </aop:config>

从上面代码,我们会发现一个问题,如何在切面中如何传递参数呢?

我们切面的参数都封装在JoinPoint类中,得到参数使用joinPoint.getArgs()返回一个数组,得到方法名使用joinPoint.getSignature().getName()。

测试一下,修改SecurityHandler.java

public class SecurityHandler {

   

    private void checkSecurity(JoinPoint joinPoint) {

       Object[] args = joinPoint.getArgs();

       for (int i=0; i<args.length; i++) {

           System.out.println(args[i]);

       }

       System.out.println(joinPoint.getSignature().getName());

       System.out.println("----checkSecurity()----");

    }

   

}


spring对AOP的支持


1、如果目标对象实现了接口,在默认情况下会采用JDK的动态代理实现AOP
2、如果目标对象实现了接口,也可以强制使用CGLIB生成代理实现AOP
3、如果目标对象没有实现接口,那么必须引入CGLIB,spring会在JDK的动态代理和CGLIB代理之间切换


如何强制使用CGLIB生成代码?
* 加入CGLIB库,SPRING_HOME/lib/cglib/*.jar
* 加入如下配置,强制使用CGLIB代理
<aop:aspectj-autoproxy proxy-target-class="true"/>

JDK动态代理和CGLIB代理的区别?
* JDK动态代理对实现了接口的类进行代理
* CGLIB代理可以对类代理,主要对指定的类生成一个子类,因为是继承
 我们的目标最好不要使用final声明
  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值