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);
}
}
- Spring基于xml文件的方式配置AOP
- Spring AOP基于XML配置的管理方式
- Spring中基于XML文件的方式实现AOP
- spring-aop基于xml文件配置
- Spring实现AOP方式之一:基于XML配置的Spring AOP
- 基于xml方式配置aop的配置文件
- 基于XML的spring AOP配置
- Spring的AOP配置(基于xml)
- Spring -- 基于XML的AOP通知配置
- 基于XML的Spring AOP配置
- 配置xml方式实现Spring的aop
- 十五Spring Aop的xml配置方式
- Spring学习-22:Spring的AOP:基于AspectJ的XML配置方式开发
- spring aop的使用(注解方式以及基于xml配置方式)
- spring aop的使用(注解方式以及基于xml配置方式)
- Spring基于XML配置AOP
- Spring(十二)使用Spring的xml文件配置方式实现AOP
- Spring基于xml的方式开发我们的aop程序
- C++ STL 学习笔记 queue
- 运维请注意:”非常危险“的Linux命令大全
- cfq_init
- 【BZOJ 1858】【SCOI 2010】序列操作【区间线段树】
- Android应用中去掉标题栏方法总结(Eclipse+Android Studio)
- Spring基于xml文件的方式配置AOP
- STM32学习系列----STM32之GPIO
- virtualbox安装centos后无法上网解决办法
- 318. Maximum Product of Word Lengths
- android蓝牙控制PC——PC端代码
- 最长回文子串第一部分(leetcode)
- Activity之间startActivityForResult传递和接受数据
- 《谍影重重5》中的黑科技
- ZOJ 1003Crashing Balloon