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 ...
随机推荐
- poj 3281 Dining【最大流】
记得把牛拆掉!拆成两个点i和i'在中间连一条流量为1的边,来限制每头牛只能选一组 一般来讲是一种物品一个消费者各占一边,但是这里有两个物品,所以考虑把牛放在中间,s向所有的食物连流量为1的边,所有食物 ...
- 图的遍历---------开始开始-------o(∩_∩)o 哈哈
图的遍历 深度优先搜索(Depth First Search , DFS) --深度优先搜索--我的理解就是分身术的另一种实现方法---用分身术将所有能看到的路都走一遍----这就是深度搜索--- 下 ...
- c语言程序设计案例教程(第2版)笔记(一)—零散、输入输出、最小公倍数、选择排序、冒泡排序
零散知识点: 非格式化输入输出:getchar().putchar() 格式化输入输出 :scanf().printf() 字符串输入输出 :gets() 或 scanf().puts() 或 ...
- C++中的static修饰的变量和函数
原文地址:http://blog.csdn.net/he3913/archive/2008/09/18/2944737.aspxC++里的静态成员函数(不能用const的原因+static在c++中的 ...
- flask框架模板系统
flask模板引擎 flask默认使用了Jinja2模板引擎,我们在使用模板的时候,需要在同级目录文件夹下 创建一个templates的文件夹,然后这个文件夹内放置我们想要的模板实例即可: 在正常普通 ...
- UVA 10462 Is There A Second Way Left? (次小生成树+kruskal)
题目大意: Nasa应邻居们的要求,决定用一个网络把大家链接在一起.给出v个点,e条可行路线,每条路线分别是x连接到y需要花费w. 1:如果不存在最小生成树,输出“No way”. 2:如果不存在次小 ...
- JEECMSv9.3在金蝶Apusic中间件中无法找到首页的问题处理
在金蝶中间件中启动JEECMS,访问首页显示"页面找不到"信息.而访问后台及其他页面均可正常访问. 经代码查找,发现前台页面的所有地址是通过"com.jeecms.cms ...
- Java中的流(3)字符流-Reader和Writer
java中提供了处理以16位的Unicode码表示的字符流的类,即以Reader和Writer 为基类派生出的一系列类. 1.Reader和Writer 这两个类是抽象类,只是提供了一系列用于字符 ...
- iOS判断输入的字符串是否是纯数字
主要用于判断输入到TextField的内容是不是数字,比如需要输入电话号码的时候. 网上查看了一些资料,一般都是通过协议. 以下内容来自:http://www.2cto.com/kf/201404/2 ...
- 给ambari集群里的kafka安装基于web的kafka管理工具Kafka-manager(图文详解)
不多说,直接上干货! 参考博客 基于Web的Kafka管理器工具之Kafka-manager的编译部署详细安装 (支持kafka0.8.0.9和0.10以后版本)(图文详解)(默认端口或任意自定义端口 ...