Spring基于xml文件的方式配置AOP

来源:互联网 发布:万达电商 淘宝别 编辑:程序博客网 时间:2024/04/29 19:53

1:接口

package com.aop.xml;


public interface AtithmeticCalculator {
   int add(int i,int j);
   int  sub(int i,int j);
  int mul(int i,int j);
   int  div(int i,int j);
}


2:实现类

package com.aop.xml;


public class AtithmeticCalculatorImpl implements AtithmeticCalculator {


public int add(int i, int j) {
int result=i+j;
return result;
}


public int sub(int i, int j) {
int result=i-j;
return result;
}


public int mul(int i, int j) {
int result=i*j;
return result;

}


public int div(int i, int j) {
int result=i/j;
return result;

}


}


3:切面类

package com.aop.xml;


import java.util.Arrays;
import java.util.List;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;


/**
 * 将功能相近的组成一个切面
 * @author Administrator
 *  把这个类声明一个切面 :需要把这个类放到IOC容器中,再声明为一个切面
 */
public class LoggingAspect {
//重用切点表达式
//定义一个方法,用于声明切点表达式,一般地,该方法中不需要添加其它代码

public void declareJointPointExpression()
{

}
//表示一个前置通知:在目标方法开始执行之前执行

   public void beforeMethod(JoinPoint joinPoint)
   {
//连接点:执行方法的参数名字
String methodName=joinPoint.getSignature().getName();
List<Object> args=Arrays.asList(joinPoint.getArgs());
  System.out.println("The method "+methodName+" begins with"+args);
   }
//表示一个后置通知:在目标方法执行之后执行(无论是否异常)都会执行

  public void afterMethod(JoinPoint joinPoint)
  {
//连接点:执行方法的参数名字
String methodName=joinPoint.getSignature().getName();
  System.out.println("The method "+methodName+" ends");
  }
//表示一个后置通知:在目标方法返回之前执行(发生异常不执行)

  public void afterReturningMethod(JoinPoint joinPoint,Object result)
  {
//连接点:执行方法的参数名字
String methodName=joinPoint.getSignature().getName();
  System.out.println("The method "+methodName+" ends with "+result);
  }
//表示一个异常通知:在发生一场之后执行(可以访问到异常对象:可以指定在某个异常时才执行)

  public void afterThrowingMethod(JoinPoint joinPoint,Exception ex)
  {
//连接点:执行方法的参数名字
String methodName=joinPoint.getSignature().getName();
  System.out.println("The method "+methodName+" occurs exception: "+ex);
  }
}


4:验证切面类

package com.aop.xml;


import java.util.Arrays;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
/**
 * 验证切面
 * @author Administrator
 *
 */


public class VLidationASpect {
//验证通知(可以指定切面优先级 Order(value)value越小,优先级越高)
  public void validateArgs(JoinPoint joinPoint)
  {
 System.out.println("-->validate:"+Arrays.asList(joinPoint.getArgs()));
  }
}


5: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:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">   
  <!-- 使用XML文件的方式-->
  <bean id="atithmeticCalculator" class="com.aop.xml.AtithmeticCalculatorImpl"></bean>
  <!-- 配置切面的bean -->
  <bean id="loggingAspect" class="com.aop.xml.LoggingAspect"></bean>
   <bean id="vLidationASpect" class="com.aop.xml.VLidationASpect"></bean>
  <!-- 配置AOP -->
  <aop:config>
   <!-- 配置切点表达式 -->
   <aop:pointcut expression="execution(* com.aop.xml.AtithmeticCalculator.*(int , int ))" id="pointcut"/>
   <!-- 配置切面及通知 -->
  <aop:aspect ref="loggingAspect" order="2">
     <aop:before method="beforeMethod" pointcut-ref="pointcut"/>
      <aop:after method="afterMethod" pointcut-ref="pointcut"/>
       <aop:after-throwing method="afterThrowingMethod" pointcut-ref="pointcut" throwing="ex"/>
       <aop:after-returning method="afterReturningMethod" pointcut-ref="pointcut" returning="result"/>
     </aop:aspect>
     <aop:aspect ref="vLidationASpect" order="1">
     <aop:before method="validateArgs" pointcut-ref="pointcut"/>
     </aop:aspect>
  </aop:config>
 
</beans>


6:测试类

package com.aop.xml;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * Spring的AOP(基于ASpectj的方式配置)
 * 1).添加jar包
 * 2). 在配置文件中添加配置<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
 * 3).把横切关注点的代码放到切面类中(切面首先是IOC容器中的一个bean,即添加@Component
 * 4).切面类还需要加入@Aspectj注解
 * 5).在类里声明各种通知(Before,After,AfterReturning,AfterThrowing)
 * execution(ASpectj的表达式:public int com.aop.impl.AtithmeticCalculatorImpl.*(int , int )))
 * 6).joinPoint连接点:在连接方法中添加一个
 * @author Administrator
 *
 */
public class Main {


public static void main(String[] args) {
  //1:创建Spring的IOC容器对象
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext-xml.xml");
 //2:从IOC容器中获取Bean实例
AtithmeticCalculator atithmeticCalculator=(AtithmeticCalculator)ctx.getBean("atithmeticCalculator");
 //3:使用bean
int result=atithmeticCalculator.add(1,2);
System.out.println("result:"+result);
result=atithmeticCalculator.div(12,6);
System.out.println("result:"+result);
result=atithmeticCalculator.div(12,1);
System.out.println("result:"+result);
}


}

0 0
原创粉丝点击