22Spring基于配置文件的方式配置AOP
直接看代码:
package com.cn.spring.aop.impl; //加减乘除的接口类
public interface ArithmeticCalculator {
int add(int i, int j);
int sub(int i, int j);
int mul(int i, int j);
int div(int i, int j);
}
package com.cn.spring.aop.impl; //实现类
public class ArithmeticCalculatorImpl implements ArithmeticCalculator { @Override
public int add(int i, int j) {
int result = i + j;
return result;
} @Override
public int sub(int i, int j) {
int result = i - j;
return result;
} @Override
public int mul(int i, int j) {
int result = i * j;
return result;
} @Override
public int div(int i, int j) {
int result = i / j;
return result;
}
}
package com.cn.spring.aop.impl; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; import java.util.Arrays;
import java.util.List; public class LoggingAspect { public void declareJoinPointExpression() {} 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();
List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("The method " + methodName + " ends with " + args);
} public void afterReturning(JoinPoint joinPoint, Object result) {
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("The method ends witd " + result);
} public void afterThrowing(JoinPoint joinPoint, Exception ex) {
String methodName = joinPoint.getSignature().getName(); System.out.println("The method " + methodName + " occures exception with: " + ex);
} public Object aroundMethod(ProceedingJoinPoint proceedingJoinPoint) {
Object result = null;
String methodName = proceedingJoinPoint.getSignature().getName();
//执行目标方法
try {
//前置通知
System.out.println("The method " + methodName + " begins with " + Arrays.asList(proceedingJoinPoint.getArgs()));
result = proceedingJoinPoint.proceed();
//返回通知
System.out.println("The method ends with " + result);
} catch (Throwable throwable) {
//异常通知
System.out.println("The method " + methodName + " occures exception with: " + throwable);
throw new RuntimeException(throwable);
}
//后置通知
System.out.println("The method " + methodName + " ends");
return result;
}
}
package com.cn.spring.aop.impl;
import org.aspectj.lang.JoinPoint;
import java.util.Arrays;
public class ValidationAspect {
public void validateArgs(JoinPoint joinPoint) {
System.out.println("validate:" + Arrays.asList(joinPoint.getArgs()));
}
}
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.cn.spring.aop.impl">
</context:component-scan> <!--配置bean-->
<bean class="com.cn.spring.aop.impl.ArithmeticCalculatorImpl"></bean> <!--配置切面的bean-->
<bean id="loggingAspect" class="com.cn.spring.aop.impl.LoggingAspect"></bean>
<bean id="validationAspect" class="com.cn.spring.aop.impl.ValidationAspect"></bean> <!--配置AOP-->
<aop:config>
<!--配置切点表达式-->
<aop:pointcut id="pointcut" expression="execution(* com.cn.spring.aop.impl.ArithmeticCalculator.*(..))"></aop:pointcut>
<!--配置切面及通知-->
<aop:aspect ref="loggingAspect" order="2">
<aop:before method="beforeMethod" pointcut-ref="pointcut"></aop:before>
<aop:after method="afterMethod" pointcut-ref="pointcut"></aop:after>
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="ex"></aop:after-throwing>
<aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"></aop:after-returning> <!--环绕通知-->
<aop:around method="aroundMethod" pointcut-ref="pointcut"></aop:around>
</aop:aspect>
<aop:aspect ref="validationAspect" order="1">
<aop:before pointcut-ref="pointcut" method="validateArgs"></aop:before>
</aop:aspect>
</aop:config>
<!--使AspjectJ注解起作用:自动为匹配的类生成代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
package com.cn.spring.aop.impl; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
//1.创建Spring的IOC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("22-1.xml"); //2.从IOC容器中huo获取bean的实例
ArithmeticCalculator arithmeticCalculator = ctx.getBean(ArithmeticCalculator.class);
//3.使用bean
int result = arithmeticCalculator.add(3, 6);
System.out.println("result:" + result);
//result = arithmeticCalculator.div(3, 0);
// System.out.println("result:" + result);
}
}
22Spring基于配置文件的方式配置AOP的更多相关文章
- 基于配置文件的方式配置AOP
之前说的都是通过注释的方式配置,接下来说说如何使用配置文件配置AOP 还是原来的代码,去掉所有注释,接下来配置最基本的几个bean. 然后使用<aop:config>标签进行配置,然后配切 ...
- Spring_基于配置文件的方式配置AOP
applicationContext-xml.xml <?xml version="1.0" encoding="UTF-8"?> <bean ...
- spring-AOP框架(基于配置文件的方式配置AOP)
.xml: ref-指向,order-指定优先级
- Spring(二十):Spring AOP(四):基于配置文件的方式来配置 AOP
基于配置文件的方式来配置 AOP 前边三个章节<Spring(十七):Spring AOP(一):简介>.<Spring(十八):Spring AOP(二):通知(前置.后置.返回. ...
- Spring4学习笔记-AOP(基于配置文件的方式)
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://shamrock.blog.51cto.com/2079212/1557743 引 ...
- [原创]java WEB学习笔记108:Spring学习---基于配置文件的形式实现AOP
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- 详解AOP——用配置文件的方式实现AOP
AOP概念 1.AOP:面向切面(方面)编程,扩展功能不修改源代码实现 AOP原理 AOP采用横向抽取机制,取代了传统纵向继承体系重复性代码 传统的纵向抽取机制: 横向抽取机制: AOP操作术语 1. ...
- 使用Spring框架入门四:基于注解的方式的AOP的使用
一.简述 前面讲了基于XML配置的方式实现AOP,本文简单讲讲基于注解的方式实现. 基于注解的方式实现前,要先在xml配置中通过配置aop:aspectj-autoproxy来启用注解方式注入. &l ...
- xml的方式配置AOP:Aspect Oriented Programming
在某些类中, 什么时机, 做什么事情 切入点(point-cut): 在某些类中(Class<?>[] itfc = new Class<?>[] { IStudentServ ...
随机推荐
- 安装MySQL需要注意的事项
一.安装MySQL之后,怎样启动MySQL,以及登录.查看数据库操作? 用管理员身份运行cmd或power shell 二.当然有可能会出现不能在cmd或power shell中登录数据库账户 造成这 ...
- js 几秒之后就不断的执行
function url() { $.ajax({ url: "AA.ashx", data: { ID: "gggg ...
- SIFT特征点检测与匹配
SIFT的步骤如下: (1) 尺度空间极值检测(Scale-space Extrema Detection) 也就是在多尺度高斯差分(Difference of Gauss)空间中检测极值点(3x3x ...
- VK Cup 2018 - Round 1 A. Primal Sport
A. Primal Sport time limit per test 1.5 seconds memory limit per test 256 megabytes input standard i ...
- Bryce1010的微机接口课设
8086CPU知识回顾 8086 CPU 中寄存器总共为 14 个,且均为 16 位 . 即 AX,BX,CX,DX,SP,BP,SI,DI,IP,FLAG,CS,DS,SS,ES 共 14 个. 而 ...
- SpringCloud开发学习总结(三)—— 服务治理Eureka
在最初开始构建微服务系统的时候可能服务并不多,我们可以通过做一些静态配置来完成服务的调用.比如,有两个服务A和B,其中服务A需要调用服务B来完成一个业务操作时,为了实现服务B的高可用,不论采用服务端负 ...
- Volley的初步了解
Volley的介绍 Volley是什么? 2013年Google I/O大会上推出的网络请求和图片加载框架 其优点是api简单,性能优秀 非常适合数据量不大但是通信频繁的网络请求,而对于大数据量的操作 ...
- 线程池机制使nginx性能提高9倍
原文标题:Thread Pools in NGINX Boost Performance 9x! 原文官方地址:https://www.nginx.com/blog/thread-pools-boos ...
- Sort排序浅聊
集合是什么?笔者简易描述:数组是不可变的,所以我们使用集合来代替. using.System.Collections; 非泛型集合 using.System.Collections.Gernerc;泛 ...
- Mac 为啥不显示图片尺寸,点了显示简介也不显示~???
这个问题困扰我好几天,然后今天想法子解决,我这个强迫症患者是真的难受,不能直接一目了然的,每次都要ps打开图片去看,真的好心累???? 网上98%的解决方法如下: 在 Finder 中,按快捷键 co ...