`
newchq
  • 浏览: 53392 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

Spring整理12 -- 面对切面(AOP)2 -- 配置文件的方式实现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()----");

    }
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics