jinfeng_wang

G-G-S,D-D-U!

BlogJava 首页 新随笔 联系 聚合 管理
  400 Posts :: 0 Stories :: 296 Comments :: 0 Trackbacks

Spring AOP中的pointcut

PointCuts用于定义所需要“拦截”的class及其method。分为静态、动态两种pointcut。其中静态pointcuts仅和class name, method name相关,可以在配置文件中通过正则表达式进行部署,因此它们都可以在运行前进行确认。而动态pointcuts则需要考虑到方法的参数,在运行时动态的确认pointcuts。一般来说,都是根据methodclass的名字来进行。其涉及到的接口如下定义:

public interface Pointcut {

    ClassFilter getClassFilter();

    MethodMatcher getMethodMatcher();

}

public interface ClassFilter {

    boolean matches(Class clazz);

}

public interface MethodMatcher {

    boolean matches(Method m, Class targetClass);

    boolean isRuntime();

    boolean matches(Method m, Class targetClass, Object[] args);

}

两种静态pointcut的实现:

NameMatchMethodPointcut:只能对方法名进行判别。

RegexpMethodPointcut:可以对类名、方法名使用正则表达式判别。

<beans>

       <bean id="maidServiceTarget"

              class="com.springinaction.chapter03.cleaning.MaidService"/>

      

       <bean id="queryInterceptor" class="com.springinaction.chapter03.cleaning.QueryInterceptor"/>

 

       <bean id="queryPointcutAdvisor"

              class="org.springframework.aop.support.RegExpPointcutAdvisor">

              <property name="pattern">

                     <value>.*get.+By.+</value>

              </property>

              <property name="advice">

                     <ref bean="queryInterceptor"/>

              </property>

       </bean>

      

       <bean id="maidService"

              class="org.springframework.aop.framework.ProxyFactoryBean">

              <property name="proxyInterfaces">

                     <value>com.springinaction.chapter03.cleaning.MaidService</value>

              </property>

              <property name="interceptorNames">

                     <list>

                            <value>queryPointcutAdvisor</value>

                     </list>

              </property>

              <property name="target">

                     <value ref="maidServiceTarget">

              </property>

       </bean>

</beans>

一种动态pointcut的实现:

ControlFlowPointcut:根据当前运行栈的情况,决定当前的advice是否需要被触发。因为它完全基于运行时栈的情况做决策,所以运行速度肯定会变慢。

<beans>

       <bean id="myServiceTarget" class="MyServiceImpl"/>

      

       <bean id="servletInterceptor" class="MyServletInterceptor"/>

      

       <bean id="servletPointcut" class="org.springframework.aop.support.ControlFlowPointcut">

              <constructor-arg>

                     <value>javax.servlet.http.HttpServlet</value>

              </constructor-arg>

       </bean>

 

       <bean id="servletAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">

              <property name="advice">

                     <ref bean="servletInterceptor"/>

              </property>

              <property name="pointcut">

                     <ref bean="servletPointcut"/>

              </property>

       </bean>

       <bean id="service" class="org.springframework.aop.framework.ProxyFactoryBean">

              <property name="proxyInterfaces">

                     <value>MyService</value>

              </property>

              <property name="interceptorNames">

                     <list>

                            <value>servletAdvisor</value>

                     </list>

              </property>

              <property name="target">

                     <value ref="myServiceTarget">

              </property>

       </bean>

</beans>

posted on 2005-03-02 17:33 jinfeng_wang 阅读(2416) 评论(0)  编辑  收藏 所属分类: spring

只有注册用户登录后才能发表评论。


网站导航: