Spring学习(20)--- Schema-based AOP(基于配置的AOP实现) -- 配置切入点pointcut

 

pointcut(切断点)表达式:

  • execution(public * *(..))  
  • execution(* set*(..))  
  • execution(* com.xyz.service.AccountService.*(..))  
  • execution(* com.xyz.service..(..))  
  • execution(* com.xyz.service...(..))  
  • within(com.xyz.service.*) (only in Spring AOP)
  • within(com.xyz.service..*) (only in Spring AOP)
  • this(com.xyz.service.AccountService) (only in Spring AOP)
  • ....

     execution用于匹配方法执行的连接点

举几个例子:

  • execution(public * *(..))       切入点为执行所有public方法时 
  • execution(* set*(..))        切入点为执行所有set开始的方法时
  • execution(* com.xyz.service.AccountService.*(..))        切入点为执行AccountService类中所有方法时
  • execution(* com.xyz.service..(..))            切入点为执行com.xyz.service包下的所有方法时
  • execution(* com.xyz.service...(..))            切入点为执行com.xyz.service包及其子包下的所有方法时
  • within(com.xyz.service.*) (only in Spring AOP)
  • within(com.xyz.service..*) (only in Spring AOP)            within 用于匹配制定类型内的执行方法
  • this(com.xyz.service.AccountService) (only in Spring AOP)      this 用于匹配当前AOP代理对象类型的执行方法

 其他

  • target(com.xyz.service.AccountService)  (only in Spring AOP)    target 用于匹配当前目标对象类型的执行方法
  • args(java.io.Serializable)   (only in Spring AOP)        args 用于匹配当前执行的方法传入的参数为指定类型的执行方法

基于注解的匹配

  • @target(org.springframework.transaction.annotation.Transactional)  (only in Spring AOP)
  • @within(org.springframework.transaction.annotation.Transactional)  (only in Spring AOP)
  • @annotation(org.springframework.transaction.annotation.Transactional)  (only in Spring AOP)
  • @args(com.xyz.security.Classified)   (only in Spring AOP)

实现:

<aop:config>
          <aop:pointcut id="businessService" expression="execution(* com.aop.schema..(..))">
          
          </aop:pointcut>
</aop:config>

例子:

新建两个类

package com.aop.schema;
/**
 * 
 * 切面类
 *
 */
public class MyAspect {

}

 

package com.aop.schema;

/**
 * 
 * 业务类
 *
 */
public class ApsectBiz {

}

 

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"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.1.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
         
     <bean id="myAspect" class="com.aop.schema.MyAspect"></bean>
      
     <bean id="apsectBiz" class="com.aop.schema.ApsectBiz"></bean>
      
     <aop:config>
          <aop:aspect id="myAspectAOP" ref="myAspect">
            <aop:pointcut id="myPointcut" expression="execution(* com.aop.schema.ApsectBiz.*(..))" />
          </aop:aspect>
     </aop:config>
 
</beans>

 

posted @ 2015-07-09 17:05  Json_wangqiang  阅读(362)  评论(0编辑  收藏  举报