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 ...
随机推荐
- 洛谷 P2593 [ZJOI2006]超级麻将【dp】
设f[i][j][k][0/1]表示选到i时,i-1选j张,i选k张,之前选的所有牌是否选择了对子 然后分情况讨论转移即可 #include<iostream> #include<c ...
- thinkphp 中 dump 函数调试数组时显示不全解决方法
在php.ini里的 xdebug 节点中,加入如下 xdebug.var_display_max_children=128 #子级节点最多显示的的字符数xdebug.var_display_max_ ...
- Android Studio编写运行测试纯java代码可带main()函数
问题 小伙伴们在做安卓项目的时候,是不是有时候会忘记某些api的使用方法,不太确定他们的结果是怎样的,需要写一些测试代码,验证看看我们的写法是否正确.刚开始的时候我是在页面上写一个Button,添加点 ...
- Luogu P1273 有限电视网【树形Dp/树形背包】
题目描述 某收费有线电视网计划转播一场重要的足球比赛.他们的转播网和用户终端构成一棵树状结构,这棵树的根结点位于足球比赛的现场,树叶为各个用户终端,其他中转站为该树的内部节点. 从转播站到转播站以及从 ...
- Manven下载
1.下载地址:http://maven.apache.org/download.cgi 2.点击下载链接 3.解压zip到安装路径 我的:C:\Progr ...
- linux下常用网络操作汇总 专题
centos 更改主机名,需要更改的几个地方: (1) /etc/sysconfig/network //更改主机名(2)/etc/hostname //更改主机名(3) /etc/hosts ...
- 使用 ServerSocket 进行文件上传,以及用Tomcat启动ServerSocket时,会卡死解决
服务器端代码 import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOExcept ...
- 使用iconfont管理项目中的字体图标
先来说说字体图标的好处: 很容易任意地缩放: 很容易地改变颜色: 很容易地产生阴影: 可以拥有透明效果: 一般来说,有先进的浏览器支持: 可以使用CSS来装饰(可以得到CSS很好支持): 可以快速转化 ...
- Android 图片文件和Bitmap之间的转换
String filePath="c:/01.jpg"; Bitmap bitmap=BitmapFactory.decodeFile(filePath); 如果图片过大,可能导致 ...
- Android开发——蓝牙
---恢复内容开始--- 前言 孤芳自赏,一揽芳华: 人情冷暖,自在人心: 登高远眺,望步止前: 喜笑言开,欺人骗己. 上篇文章介绍了基本的蓝牙使用,书写的demo也不是很完善,希望各位大神能够改正. ...