applicationContext.xml配置AOP切面编程
Computer.java
package com.wh.aop2;
public class Computer {
public void play01(){
System.out.println("一号玩家!");
}
public void play02(){
System.out.println("二号玩家!");
System.out.println(10/0);
}
public void play03(){
System.out.println("三号玩家!");
}
public void play04(){
System.out.println("四号玩家!");
}
public void play05(){
System.out.println("五号玩家!");
}
public void play06(){
System.out.println("六号玩家!");
}
}
AopProxy.java
package com.wh.aop2; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; public class AopProxy { public void doBefore01() {
System.out.println("无参数前置通知:");
} public void doBefore02() {
System.out.println("有参数前置通知:");
} public void doAround(ProceedingJoinPoint p) {
System.out.println("环绕之前置通知:");
Object obj = null;
try {
obj = p.proceed();
System.out.println("环绕之后置通知: " + obj);
}
catch (Throwable e) {
System.out.println("环绕之异常通知: " + e.getMessage());
}
finally {
System.out.println("环绕之最终通知:");
}
}
}
applicationContext.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <bean id="computer" class="com.wh.aop.Computer"/>
<bean id="testBefore" class="com.wh.aop.TestBefore"/> <aop:config>
<aop:pointcut expression="execution(* com.wh.aop.Computer.add(*,*))" id="computerAddCut"/>
<aop:pointcut expression="execution(* com.wh.aop.Computer.div(*,*))" id="computerDivCut"/>
<!-- AOP前置通知 -->
<aop:aspect ref="testBefore">
<aop:before method="doBefore" pointcut-ref="computerAddCut"/>
</aop:aspect>
<!-- AOP后置通知 -->
<aop:aspect ref="testBefore">
<aop:after-returning method="doAfter" pointcut-ref="computerAddCut" returning="ret"/>
</aop:aspect>
<!-- AOP异常通知 -->
<aop:aspect ref="testBefore">
<aop:after-throwing method="doThrow" pointcut-ref="computerDivCut" throwing="e"/>
</aop:aspect>
</aop:config> <import resource="applicationContext2.xml"/> </beans>
applicationContext2.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <bean id="computer2" class="com.wh.aop2.Computer"/>
<bean id="aopProxy2" class="com.wh.aop2.AopProxy"/>
<aop:config>
<aop:pointcut expression="execution(* com.wh.aop2.Computer.*(..))" id="computerCut2"/>
<aop:aspect ref="aopProxy2">
<aop:around method="doAround" pointcut-ref="computerCut2"/>
</aop:aspect>
</aop:config> </beans>
TestAop.java
package com.wh.aop2; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestAop { @Test
public void test01(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Computer c=(Computer)ac.getBean("computer2");
c.play01();
} @Test
public void testAround(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Computer c=(Computer)ac.getBean("computer2");
c.play01();
}
/**
环绕之前置通知:
一号玩家!
环绕之后置通知: null
环绕之最终通知:
*/ @Test
public void testAround2(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Computer c=(Computer)ac.getBean("computer2");
c.play02();
}
/**
环绕之前置通知:
二号玩家!
环绕之异常通知: / by zero
环绕之最终通知:
*/
//小结:在环绕通知中:正常情况下,四个通知都会出现,若出现异常,只有后置通知不会出现!
//通知顺序为:前置、异常、最终、后置
}
applicationContext.xml配置AOP切面编程的更多相关文章
- 注解配置AOP切面编程
1.导入先关jar包 2.编写applicationContext.xml,配置开启注解扫描和切面注解扫描 <?xml version="1.0" encoding=&quo ...
- SpringBoot2.0 基础案例(11):配置AOP切面编程,解决日志记录业务
本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.AOP切面编程 1.什么是AOP编程 在软件业,AOP为Asp ...
- Spring MVC通过AOP切面编程 来拦截controller 实现日志的写入
首选需要参考的是:[参考]http://www.cnblogs.com/guokai870510826/p/5977948.html http://www.cnblogs.com/guokai8 ...
- Spring 框架基础(04):AOP切面编程概念,几种实现方式演示
本文源码:GitHub·点这里 || GitEE·点这里 一.AOP基础简介 1.切面编程简介 AOP全称:Aspect Oriented Programming,面向切面编程.通过预编译方式和运行期 ...
- Spring AOP 切面编程记录日志和接口执行时间
最近客户现在提出系统访问非常慢,需要优化提升访问速度,在排查了nginx.tomcat内存和服务器负载之后,判断是数据库查询速度慢,进一步排查发现是因为部分视图和表查询特别慢导致了整个系统的响应时间特 ...
- Spring的配置文件ApplicationContext.xml配置头文件解析
Spring的配置文件ApplicationContext.xml配置头文件解析 原创 2016年12月16日 14:22:43 标签: spring配置文件 5446 spring中的applica ...
- springmvc.xml和applicationContext.xml配置的特点
1:springmvc.xml配置要点 一般它主要配置Controller的组件扫描器和视图解析器 下为:springmvc.xml文件 <?xml version="1.0" ...
- AOP切面编程在android上的应用
代码地址如下:http://www.demodashi.com/demo/12563.html 前言 切面编程一直是一个热点的话题,这篇文章讲讲一个第三方aop库在android上的应用.第三方AOP ...
- 注解与AOP切面编程实现redis缓存与数据库查询的解耦
一般缓存与数据库的配合使用是这样的. 1.查询缓存中是否有数据. 2.缓存中无数据,查询数据库. 3.把数据库数据插入到缓存中. 其实我们发现 1,3 都是固定的套路,只有2 是真正的业务代码.我们可 ...
随机推荐
- Jmeter使用基础笔记-认识Jmeter
我在工作过程中接触Jmeter不算特别多,对Jmeter的使用也只是限于基础阶段,不过对付基本的一些需求我想足够使用了.有好几个朋友问我关于Jmeter的问题,在此我将我在工作过程中的使用心得和总结的 ...
- MongoDB 数据文件备份与恢复
备份与恢复数据对于管理任何数据存储系统来说都是非常重要的. 1.冷备份与恢复——创建数据文件的副本(前提是要停止MongoDB服务器),也就是直接copy MongoDB将所有数据都存储在数据目录下, ...
- noip模拟赛 蒜头君的树
分析:这道题问的是树上整体的答案,当然要从整体上去考虑. 一条边对答案的贡献是这条边一端连接的点的个数*另一端连接的点的个数*边权,可以用一次dfs来统计答案,之后每次更改操作在原答案的基础上增减就好 ...
- java多线程编程核心技术(二)--对象及变量的并发访问
1.方法内部的私有变量是线程安全的,实例变量是线程不安全的. 2.A线程先持有object对象的lock锁,B线程可以以异步的方式调用object对象中的非synchronized类型的方法. 3.A ...
- [BZOJ1045][HAOI2008]糖果传递(数学分析)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1045 分析:均分纸牌的环状版本. 先看线性的版本: 设f[i]表示第I位从第i+1位得 ...
- 淘宝手机rem的如何使用
1.主要介绍几个移动端常用的单位rem.vw.vh,配合传统的px.百分比.<viewport>标签,兼容适配移动端的各种分辨率的手机端. rm : 这个单位是以父元素为标准来进行计算 , ...
- 1.7-BGP③
IBGP的水平分隔原则(Split Horizon Rule): IBGP的水平分割原则:by default,routes learned via IBGP are never propagated ...
- [Algorithm] Determine if two strings are an anagram
The anagram test is commonly used to demonstrate how an naive implementation can perform significant ...
- 重学数据结构系列之——平衡树之SB Tree(Size Blanced Tree)
学习来源:计蒜客 平衡树 1.定义 对于每一个结点.左右两个子树的高度差的绝对值不超过1,或者叫深度差不超过1 为什么会出现这样一种树呢? 假如我们依照1-n的顺序插入到二叉排序树中,那么二叉排序树就 ...
- PHP第五课 自己主动类型转换与流程控制
学习概要: 1.了解自己主动类型转换的有哪些 2.了解主要的流程控制语句 3.实例:实现日历表格的写法 自己主动类型转换 1)整型转字符串 echo $num."abc"; 2)字 ...