实战Spring中AOP

来源:互联网 发布:公司软件打卡 编辑:程序博客网 时间:2024/05/16 08:01

1.方法之前调用日志Advice:

 package com.lxt008.aop;
 import java.lang.reflect.Method;
 import org.springframework.aop.MethodBeforeAdvice;
 public class LogManager implements MethodBeforeAdvice
 {
   public void before(Method m,Object[] args,Object target)
   {
      System.out.println("before Log!!!");
   }
 }

 

2.方法调用之后做安全检查(实际上一般在调用前做,这里只是演示用法):

 package com.lxt008.aop;import java.lang.reflect.Method;
 import org.springframework.aop.AfterReturningAdvice;
 public class SecrityManager implements AfterReturningAdvice
 {
   public void afterReturning(Object returnValue, Method method,Object[] args,
     Object target) throws Throwable
   {
      System.out.println("Secrity Check!!!");
   }
 }

 

3.方法调用前后执行的环绕Advice,也可以不调用原始方法:

package com.lxt008.aop;import java.lang.reflect.Method;
 import org.aopalliance.intercept.MethodInterceptor;
 import org.aopalliance.intercept.MethodInvocation;
 import org.springframework.util.StopWatch;
 public class PerformanceAroundAdvice implements MethodInterceptor
 {
   public Object invoke(MethodInvocation invocation) throws Throwable
   {
    Object returnValue=null;
    //开始计时
    StopWatch sw = new StopWatch();
    sw.start(invocation.getMethod().getName()); 

    returnValue = invocation.proceed();  //结束计时
    sw.stop();
  
    Method m = invocation.getMethod();
    Object target = invocation.getThis();
    Object[] args = invocation.getArguments();
  
    System.out.println();
    System.out.println("执行方法名: " + m.getName());
    System.out.println("目标类: " + target.getClass().getName()); 

    if(args!=null)
    {
     System.out.println("参数列表:");
     for (int i = 0; i < args.length; i++)
     {
      System.out.print("第" + i + "个参数: " + args[i]);
     }
   }
   System.out.println();
   System.out.println("总供使用时间: " + sw.getTotalTimeMillis() + " ms");
   return returnValue;
  }
 }

4.对抛出的异常也可以拦截:

package com.lxt008.aop;
 import java.lang.reflect.Method;
 import java.rmi.RemoteException;
 import org.springframework.aop.ThrowsAdvice;
 public class MyThrowsAdvice implements ThrowsAdvice
 {
   public void afterThrowing(Method method, Object[] args,Object target, Exception ex)
    {
      System.out.println("捕获其它不明异常");
      System.out.println("异常发生方法: " + method.getName());
      System.out.println();
    }
 }

5.提供接口IAopDemo,Spring的AOP是对接口进行拦截.

package com.lxt008.aop;
 public interface IAopDemo
 {
     public abstract void doSomething() throws Exception;
 }

 

6.实现类AopDemo

package com.lxt008.aop;
 import java.rmi.RemoteException;
 public class AopDemo implements IAopDemo
 {
  /* (non-Javadoc)
   * @see com.lxt008.aop.IAopDemo#doSomething1()
   */
   public void doSomething() throws Exception
   {
    System.out.println("Process Buniess!!!");
    //throw new Exception("unkown exception");
   }
 }

7.客户端Main类:

package com.lxt008.aop;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 public class Main
 { /**
   * @param args
   */
   public static void main(String[] args)
   {
     // TODO Auto-generated method stub
     ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
     IAopDemo demo=(IAopDemo)ctx.getBean("aopDemo");
     try
     {
      demo.doSomething();
     }
     catch(Exception e)
     {
      System.out.println("Found Exception");
     }
   }
 }

8.配置文件:applicationContext.xml

<?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="aopDemoTarget" class="com.lxt008.aop.AopDemo"></bean><bean id="logAdvice" class="com.lxt008.aop.LogManager">

</bean><!-- Advice会增强所有方法,Advisor增强匹配的方法 -->
<!-- NameMatchMethodPointcutAdvisor匹配名字 -->
<bean id="logAdvisor"  class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
  <constructor-arg>
   <ref local="logAdvice" />
  </constructor-arg>
  <property name="mappedNames">
   <list>
    <value>doSomething</value>   
   </list>
  </property>
</bean>

<bean id="secrityAdvice" class="com.lxt008.aop.SecrityManager"></bean>

<!-- RegexpMethodPointcutAdvisor匹配模式patterns -->
<bean id="secrityAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
  <constructor-arg>
   <ref local="secrityAdvice" />
  </constructor-arg>
  <property name="patterns">
   <list>
    <value>.*doSomething.*</value>
   </list>
  </property>
</bean><bean id="performanceAdvice" class="com.lxt008.aop.PerformanceAroundAdvice"></bean>

<bean id="myThrowsAdvice " class="com.lxt008.aop.MyThrowsAdvice"></bean><bean id="aopDemo" class="org.springframework.aop.framework.ProxyFactoryBean">
 <property name="proxyInterfaces">
  <value>com.lxt008.aop.IAopDemo</value>
 </property>
 <property name="interceptorNames">
  <list>
   <value>logAdvisor</value>
   <value>secrityAdvisor</value>
   <value>performanceAdvice</value>
   <value>myThrowsAdvice</value>  
  </list>
 </property>
 <property name="target" ref="aopDemoTarget"></property>
</bean>

</beans>

原创粉丝点击