spring AOP [包括对struts动作的拦截]

第一种:使用spring AOP 对action做日志管理,可以只拦截特定的action!
虽然Spring提供很多内置拦截器,但是我将向您展示如何创建自己的拦截器并把它应用于一个Struts动作。为了使用拦截器,您需要做三件事:

1、创建拦截器;
2、注册拦截器;
3、声明在何处拦截代码。

这看起来非常简单的几句话却非常强大。例如,在清单7中,我为Struts动作创建了一个日志记录拦截器。这个拦截器在每个方法调用之前打印一句话:

清单 7.一个简单的日志记录拦截器

package ca.nexcel.books.interceptors;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class LoggingInterceptor implements MethodBeforeAdvice {
   public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("logging before!");
    }
}


这个拦截器非常简单。before()方法在拦截点中每个方法之前运行。在本例中,它打印出一句话,其实它可以做您想做的任何事。下一步就是在Spring配置文件中注册这个拦截器,如清单8所示:

清单 8.在Spring配置文件中注册拦截器

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" 
  "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
  <bean id="bookService" class="ca.nexcel.books.business.BookServiceImpl"/>

  <bean name="/searchSubmit" 
        class="ca.nexcel.books.actions.SearchSubmit">
     <property name="bookService">
        <ref bean="bookService"/>
     </property>
  </bean>

  <!--  Interceptors --> 
  <bean name="logger"    
    class="ca.nexcel.books.interceptors.LoggingInterceptor"/> |(1)

  <!-- AutoProxies -->
  <bean name="loggingAutoProxy" 
        class="org.springframework.aop.framework.autoproxy.
          BeanNameAutoProxyCreator"> |(2)
    <property name="beanNames">
          <value>/searchSubmit</valuesgt; |(3)
    </property>
    <property name="interceptorNames">
        <list>
          <value>logger</value> |(4)
        </list>
    </property>
   </bean>

</beans>


您可能已经注意到了,清单8扩展了清单6中所示的应用程序以包含一个拦截器。具体细节如下:

◆在(1)处,我注册了这个拦截器。
◆在(2)处,我创建了一个bean名称自动代理,它描述如何应用拦截器。还有其他的方法定义拦截点,但是这种方法常见而简便。
◆在(3)处,我将Struts动作注册为将被拦截的bean。如果您想要拦截其他的Struts动作,则只需要在“beanNames”下面创建附加的<value>标记。
◆在(4)处,当拦截发生时,我执行了在(1)处创建的拦截器bean的名称。这里列出的所有拦截器都应用于“beanNames”。
  
就是这样。就像这个例子所展示的,将您的Struts动作置于Spring框架的控制之下,为处理您的Struts应用程序提供了一系列全新的选择。在本例中,使用动作委托可以轻松地利用Spring拦截器提高Struts应用程序中的日志记录能力。
第二种:使用Spring AOP对action做日志管理

该方法会对所有的action类进行拦截,不能指定具体的拦截类!
首先还是要写一个普通类,不过此类中的方法需要传入参数。 比如

view plain
package chen.hui.log 
 
import org.aspectj.lang.JoinPoint; 
 
public classs MyLog{ 
 
       //在类里面写方法,方法名是可以任意的。此处我用标准的before和after来表示 
 
        //此处的JoinPoint类可以获取,action所有的相关配置信息和request等内置对象。 
 
      public void before(JoinPoint joinpoint){ 
 
                joinpoint.getArgs();//此方法返回的是一个数组,数组中包括request以及ActionCofig等类对象 
 
                System.out.println("被拦截方法调用之前调用此方法,输出此语句"); 
      } 
      public void after(JoinPoint joinpoint){ 
 
                  System.out.println("被拦截方法调用之后调用此方法,输出此语句"); 
      } 

其次我们在写一个action类作为被拦截类(Spring的AOP就是拦截这个类里面的方法)

view plain
package chen.hui.log 
 
public class LoginAction{//此类中方法可以写任意多个。我只写一个 
    public void test(){ 
        Sytem.out.println("测试类的test方法被调用"); 
    } 

最后进行配置文件的编写。在Spring的配置文件中我们需要进行几句话的配置

view plain
<bean id="testLog" class="chen.hui.log.MyLog"></bean> <!--将日志类注入到bean中。--> 
      <aop:config> 
            <aop:aspect id="b" ref="testLog"><!--调用日志类--> 
            <aop:pointcut id="log" expression="execution(* chen.hui.log.*.*(..))"/><!--配置在log包下所有的类在调用之前都会被拦截--> 
            <aop:before pointcut-ref="log" method="before"/><!--在log包下面所有的类的所有方法被调用之前都调用MyLog中的before方法--> 
            <aop:after pointcut-ref="log" method="after"/><!--在log包下面所有的类的所有方法被调用之前都调用MyLog中的after方法--> 
      </aop:aspect> 
</aop:config> 

猜你喜欢

转载自xiaoxuejie.iteye.com/blog/1297187